2010-05-04 5 views

Odpowiedz

88

Można spróbować to: (zaktualizowane do 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; 
} 

nazwać :

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

dla iOS4 + powinieneś utworzyć kontekst ze skalowaniem 0.0, aby uzyskać skalowanie ekranu głównego, jeśli skalowanie nie jest równe 1.0 (wyświetlacz siatkówki to 2.0): \t UIGraphicsBeginImageContextWithOptions (rozmiar, NO, 0.0); – valexa

+0

@rpmx znaki ucieczki takie jak \ n nowa linia nie są wykrywane? – Yadnesh

+0

@Jadnesh: Użyj: [text drawInRect: (CGRect) rect withFont: (UIFont *) font]; – rpmx

2

rozpocząć kontekst obrazu z UIGraphicsBeginImageContext, wyciągnąć do niego dostać obraz z UIGraphicsGetImageFromCurrentImageContext, zakończ go UIGraphicsEndImageContext

+0

Jak narysować NSString do niego? Tego właśnie pytałem. PROSZĘ! – Jaba

+0

Zobacz dodatki NSString UIKit - dostępnych jest wiele funkcji rysowania nsstringowych - takich jak "drawAtPoint: withFont:" lub "drawInRect: withFont:" i inne – Vladimir

+0

@Jaba: spójrz na 'CGContextShowTextAtPoint' i funkcje pokrewne. –

13

Dodawanie szybkiej wersji, również daje więcej możliwości:

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

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

zasługa Bao Lei na pięknej odpowiedź.