如何解决快速地通过UIImage添加文本-以编程方式
我正在尝试通过UIView添加文本,就像在instagram Stories上一样。 我正在使用此功能,在屏幕尺寸大小的视图上绘制textViews
func passingAndDrawingTextViews(textViews : [UITextView],viewPassed : UIView,inImage : UIImage) -> UIImage{
let scale = UIScreen.main.scale
UIGraphicsBeginImageContextWithOptions(viewPassed.frame.size,false,scale)
viewPassed.draw(CGRect(x: 0,y: 0,width: viewPassed.frame.width,height: viewPassed.frame.height))
//adding each textView
textViews.forEach { (textView) in
let textColor = textView.textColor
let textFont = UIFont(name: textView.font!.fontName,size: textView.font!.pointSize)
let textAlignment = textView.textAlignment
//alignment of the text
let paragraphStyle: NSMutableParagraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = textAlignment
let textFontAttributes = [
NSAttributedString.Key.font: textFont,NSAttributedString.Key.foregroundColor: textColor,NSAttributedString.Key.paragraphStyle : paragraphStyle,]
let rect = CGRect(x: textView.frame.minX,y: textView.frame.minY,width: textView.frame.width,height: textView.frame.height)
textView.text.draw(in: rect,withAttributes: textFontAttributes as [NSAttributedString.Key : Any])
}
let newImage = UIGraphicsGetimageFromCurrentimageContext()
UIGraphicsEndImageContext()
return newImage!
}
已绘制文本,问题在于文本视图不在viewPassed中的确切位置。特别是,相对于原始textView而言,所绘制的文本在左上方。
我想知道为什么会发生这种情况以及如何解决它,我想尽办法,但似乎找不到解决方法。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。