2010-05-04 46 views

回答

88

結束它你可以試試這個:(更新爲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 < 4.0 
     UIGraphicsBeginImageContext(size); 

    // optional: add a shadow, to avoid clipping the shadow you should make the context size bigger 
    // 
    // CGContextRef ctx = UIGraphicsGetCurrentContext(); 
    // CGContextSetShadowWithColor(ctx, CGSizeMake(1.0, 1.0), 5.0, [[UIColor grayColor] CGColor]); 

    // draw in context, you can use also drawInRect:withFont: 
    [text drawAtPoint:CGPointMake(0.0, 0.0) withFont:font]; 

    // transfer image 
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext();  

    return image; 
} 

要叫它:

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

對於iOS4 +,如果縮放比例不是1.0(視網膜顯示爲2.0),則應創建縮放比例爲0.0的上下文,以獲得主屏幕的縮放比例:\t UIGraphicsBeginImageContextWithOptions(size,NO,0.0); – valexa 2010-10-03 16:02:05

+0

@rpmx轉義字符如\ n新行未被檢測到? – Yadnesh 2012-01-17 09:03:56

+0

@Yadnesh:使用:[text drawInRect:(CGRect)rect withFont:(UIFont *)font]; – rpmx 2012-01-21 23:57:48

2

開始的圖像內容與UIGraphicsBeginImageContext,畫它,與UIGraphicsGetImageFromCurrentImageContext得到的圖像,與UIGraphicsEndImageContext

+0

如何繪製NSString?那正是我所問的。請! – Jaba 2010-05-04 13:29:22

+0

看看NSString UIKit Additions - 有多個可用的nsstring繪圖函數 - 比如'drawAtPoint:withFont:'或'drawInRect:withFont:'等 – Vladimir 2010-05-04 13:41:05

+0

@Jaba:查看'CGContextShowTextAtPoint'及相關函數。 – 2010-05-04 14:20:30

13

添加斯威夫特版本,還爲您提供了更多的選擇:

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 
} 
0

雨燕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! 
} 

幸得鮑蕾爲美麗的答案。