# 12 使用圖片,文字,emoji,邊框 & 圓角製作漂亮卡片,HBD My Cat!

Target:模仿 Swift Playground 的 Flashy Photos,在 Xcode playground 製作有趣的卡片送給喜歡的人。

剛好我家的貓”初初”8/18要滿一歲生日了,就把這個卡片送給他,希望他平平安安長大,陪我們很久。

參考彼得潘的文章連結如下

成品

程式碼如下

import UIKit
//建立主要的照片View,並把image放進View中,做各種調整
let chuImageView = UIImageView(image: UIImage(named: "Chu.jpeg"))
//調整view的位置與大小
chuImageView.frame = CGRect(x: 0, y: 0, width: 800, height: 480)
// 調整圖片的填滿方式
chuImageView.contentMode = .scaleAspectFill
// 調整邊框的寬度、顏色、透明度、圓角
chuImageView.layer.borderWidth = 10
chuImageView.layer.borderColor = CGColor(red: 1, green: 0, blue: 0, alpha: 1)
chuImageView.layer.cornerRadius = 50
// 調整圖片的透明度
chuImageView.alpha = 1
// 顯示圓角的時候需要填入下面的程式
chuImageView.clipsToBounds = true
// 輸入想要寫的字,並做各種調整,如旋轉、字體顏色...
let messageLabel = UILabel(frame: CGRect(x: 400, y: 150, width: 400, height: 30))
messageLabel.text = " 20220818 ChuChu Happy Birthday "
messageLabel.textColor = UIColor(red: 255/255, green: 1/255, blue: 1/255, alpha: 1)
messageLabel.font = UIFont.systemFont(ofSize: 25)
// 這邊的程式,是用來旋轉的
messageLabel.transform = CGAffineTransform(rotationAngle: .pi/180*45)
messageLabel.backgroundColor = UIColor(red: 1, green: 1, blue: 1, alpha: 1)
messageLabel.layer.borderWidth = 3
messageLabel.layer.borderColor = CGColor(red: 1, green: 0, blue: 0, alpha: 0.5)
messageLabel.layer.cornerRadius = 5
messageLabel.clipsToBounds = true
chuImageView.addSubview(messageLabel)
// 加入Emoji
var starLabel = UILabel(frame: CGRect(x: 60, y: 60, width: 30, height: 30))
starLabel.text = "⭐️"
starLabel.font = UIFont.systemFont(ofSize: 25)
chuImageView.addSubview(starLabel)
starLabel = UILabel(frame: CGRect(x: 50, y: 30, width: 30, height: 30))
starLabel.text = "⭐️"
starLabel.font = UIFont.systemFont(ofSize: 25)
starLabel.transform = CGAffineTransform(rotationAngle: .pi/180*30)
chuImageView.addSubview(starLabel)
starLabel = UILabel(frame: CGRect(x: 20, y: 20, width: 30, height: 30))
starLabel.text = "⭐️"
starLabel.font = UIFont.systemFont(ofSize: 25)
starLabel.transform = CGAffineTransform(rotationAngle: .pi/180*60)
chuImageView.addSubview(starLabel)

--

--