如何在iOS Swift中為圖像添加文本?我已經環顧四周,并沒有成功地弄清楚如何拍攝文字,將其疊加在圖像上,然后將兩者合并為一個UIImage。我使用我能想到的搜索字詞已經讓Google筋疲力盡了,所以如果有人有解決方案或者至少提示他們可以指出它會非常感激。
3 回答
茅侃侃
TA貢獻1842條經驗 獲得超22個贊
我的簡單方案:
func generateImageWithText(text: String) -> UIImage{
let image = UIImage(named: "imageWithoutText")!
let imageView = UIImageView(image: image)
imageView.backgroundColor = UIColor.clearColor()
imageView.frame = CGRectMake(0, 0, image.size.width, image.size.height)
let label = UILabel(frame: CGRectMake(0, 0, image.size.width, image.size.height))
label.backgroundColor = UIColor.clearColor()
label.textAlignment = .Center
label.textColor = UIColor.whiteColor()
label.text = text UIGraphicsBeginImageContextWithOptions(label.bounds.size, false, 0);
imageView.layer.renderInContext(UIGraphicsGetCurrentContext()!)
label.layer.renderInContext(UIGraphicsGetCurrentContext()!)
let imageWithText = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext();
return imageWithText}- 3 回答
- 0 關注
- 1035 瀏覽
添加回答
舉報
0/150
提交
取消
