How do I use the NSString draw functionality to create a UIImage from text

匿名 (未验证) 提交于 2019-12-03 02:54:01

问题:

I would like to draw the content of a NSString variable in a UIImage, but I have absolutely no idea how to do this. I need to write a method that would receive a NSString as parameter and return a UIImage with the text drawn into it.

回答1:

You can try this: (updated for iOS 4)

-(UIImage *)imageFromText:(NSString *)text {     // set the font type and size     UIFont *font = [UIFont systemFontOfSize:20.0];       CGSize size  = [text sizeWithFont:font];      // check if UIGraphicsBeginImageContextWithOptions is available (iOS is 4.0+)     if (UIGraphicsBeginImageContextWithOptions != NULL)         UIGraphicsBeginImageContextWithOptions(size,NO,0.0);     else         // iOS is 

To call it:

UIImage *image = [self imageFromText:@"This is a text"]; 


回答2:

Adding a Swift version, also gives you more options:

class func sizeOfAttributeString(str: NSAttributedString, maxWidth: CGFloat) -> CGSize {     let size = str.boundingRectWithSize(CGSizeMake(maxWidth, 1000), options:(NSStringDrawingOptions.UsesLineFragmentOrigin), context:nil).size     return size }  class func imageFromText(text:NSString, font:UIFont, maxWidth:CGFloat, color:UIColor) -> UIImage {     let paragraph = NSMutableParagraphStyle()     paragraph.lineBreakMode = NSLineBreakMode.ByWordWrapping     paragraph.alignment = .Center // potentially this can be an input param too, but i guess in most use cases we want center align      let attributedString = NSAttributedString(string: text, attributes: [NSFontAttributeName: font, NSForegroundColorAttributeName: color, NSParagraphStyleAttributeName:paragraph])      let size = sizeOfAttributeString(attributedString, maxWidth: maxWidth)     UIGraphicsBeginImageContextWithOptions(size, false , 0.0)     attributedString.drawInRect(CGRectMake(0, 0, size.width, size.height))     let image = UIGraphicsGetImageFromCurrentImageContext()     UIGraphicsEndImageContext()     return image } 


回答3:

Begin an image context with UIGraphicsBeginImageContext, draw to it, get an image with UIGraphicsGetImageFromCurrentImageContext, end it with UIGraphicsEndImageContext



回答4:

Swift 3.1

func sizeOfAttributeString(str: NSAttributedString, maxWidth: CGFloat) -> CGSize {     let size = str.boundingRect(with: CGSize(width: maxWidth, height: 1000), options:(NSStringDrawingOptions.usesLineFragmentOrigin), context:nil).size     return size }  func imageFromText(text:NSString, font:UIFont, maxWidth:CGFloat, color:UIColor) -> UIImage {     let paragraph = NSMutableParagraphStyle()     paragraph.lineBreakMode = NSLineBreakMode.byWordWrapping     paragraph.alignment = .center       let attributedString = NSAttributedString(string: text as String, attributes: [NSFontAttributeName: font, NSForegroundColorAttributeName: color, NSParagraphStyleAttributeName:paragraph])      let size = sizeOfAttributeString(str: attributedString, maxWidth: maxWidth)     UIGraphicsBeginImageContextWithOptions(size, false , 0.0)     attributedString.draw(in: CGRect(x: 0, y: 0, width: size.width, height: size.height))     let image = UIGraphicsGetImageFromCurrentImageContext()     UIGraphicsEndImageContext()     return image! } 

Credit goes to Bao Lei for the beautiful answer.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!