[iOS]#7 使用圖片,文字,emoji,邊框 & 圓角製作漂亮卡

今天來開始寫點Swift程式囉~~~

因為沒有什麼好的想法,就先依照作業需求一步步來,首先從電腦隨便找兩張圖片利用addSubview組合起來:

let lakeImageView = UIImageView(image: UIImage(named: "lake.jpeg"))let chihuaImageView = UIImageView(image: UIImage(named: "chihua.png"))//調整吉娃大小位置透明度chihuaImageView.frame = CGRect(x: 0, y: 0, width: 80, height: 80)chihuaImageView.center = CGPoint(x: 1200, y: 800)chihuaImageView.alpha = 0.5lakeImageView.addSubview(chihuaImageView)//設定背景大小lakeImageView.frame = CGRect(x: 0, y: 0, width: 1600, height: 1200)lakeImageView.contentMode = .scaleAspectFill

原本單純只是要營造有隻吉娃在雪地裡很冷的照片,但因為吉娃原本就無底色加上雪地的純白,其實沒那麼好找,所以決定臨時把主題改為「吉娃在哪裡?」,順便修改圖片邊框。

//UILabellet messageLabel = UILabel(frame: CGRect(x: 200, y: 200, width: 420, height: 60))messageLabel.text = "吉娃在哪裡?"messageLabel.textColor = UIColor(red: 0, green: 0.5, blue: 1, alpha: 1)messageLabel.font = UIFont.systemFont(ofSize: 32)lakeImageView.addSubview(messageLabel)//borderlakeImageView.layer.borderWidth = 5lakeImageView.layer.borderColor = CGColor(red: 0, green: 0, blue: 0, alpha: 0)lakeImageView.layer.cornerRadius = 45lakeImageView.clipsToBounds = true

最後加上用function搭配random產生的Emoji邊框,這邊加上第二道謎題「照片場景在哪國呢?(有出現的Emoji)」

let emojis = ["🇨🇦","🇯🇵","🇬🇧","🇺🇸","🇸🇪","🇳🇴","🇸🇬","🇷🇴","🇨🇳","🇹🇼"]for i in 0...29 {let topEmoji = UILabel(frame: CGRect(x: i*50+50, y: 50, width: 50, height: 50))topEmoji.text = emojis.randomElement()topEmoji.font = UIFont.systemFont(ofSize: CGFloat.random(in: 25...45))topEmoji.transform = CGAffineTransform(rotationAngle: .pi/180*45*CGFloat(i))lakeImageView.addSubview(topEmoji)let bottomEmoji = UILabel(frame: CGRect(x: i*50+50, y: 1150, width: 50, height: 50))bottomEmoji.text = emojis.randomElement()bottomEmoji.font = UIFont.systemFont(ofSize: CGFloat.random(in: 25...45))bottomEmoji.transform = CGAffineTransform(rotationAngle: .pi/180*45*CGFloat(i))lakeImageView.addSubview(bottomEmoji)}for i in 0...21 {let leftEmoji = UILabel(frame: CGRect(x: 50, y: i*50+50, width: 50, height: 50))leftEmoji.text = emojis.randomElement()leftEmoji.font = UIFont.systemFont(ofSize: CGFloat.random(in: 25...45))leftEmoji.transform = CGAffineTransform(rotationAngle: .pi/180*(-45)*CGFloat(i))lakeImageView.addSubview(leftEmoji)let rightEmoji = UILabel(frame: CGRect(x: 1550, y: i*50+50, width: 50, height: 50))rightEmoji.text = emojis.randomElement()rightEmoji.font = UIFont.systemFont(ofSize: CGFloat.random(in: 25...45))rightEmoji.transform = CGAffineTransform(rotationAngle: .pi/180*(-45)*CGFloat(i))lakeImageView.addSubview(rightEmoji)}

謝謝大家!

完整程式碼:

--

--