2016-03-27 10 views
-1

Ich mache eine App, also muss ich wissen, wie ich Text in Bilder einfügen kann.
Ich arbeite mit Xcode 7 und Swift 2Wie man Text in Bilder in Swift einfügt?

ich diesen Code bereits haben, aber es ist in Objective-C, und ich weiß nicht, ob es ... funktioniert

- (UIImage *)burnTextIntoImage:(NSString *)text :(UIImage *)img { 
    UIGraphicsBeginImageContext(img.size); 

    CGRect aRectangle = CGRectMake(0,0, img.size.width, img.size.height); 
    [img drawInRect:aRectangle]; 

    [[UIColor redColor] set]; 
    NSInteger fontSize = 14; 
    if ([text length] > 200) { 
     fontSize = 10; 
    } 
    UIFont *font = [UIFont boldSystemFontOfSize: fontSize]; 

    [text drawInRect : aRectangle 
      withFont : font 
     lineBreakMode : UILineBreakModeTailTruncation 
      alignment : UITextAlignmentCenter ]; 

    UIImage *theImage=UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return theImage; 
} 

Antwort

1

ich übersetzen versucht der Code Swift 2. das ist so nah, wie ich bekam:

extension UIImage { 
    func withText(text: String) -> UIImage { 
     UIGraphicsBeginImageContext(size) 

     let rectangle = CGRect(x: 0, y: 0, width: size.width, height: size.height) 
     drawInRect(rectangle) 

     UIColor.redColor().set() 
     let fontSize = text.characters.count > 200 ? 10 : 14 
     let font = UIFont.boldSystemFontOfSize(CGFloat(fontSize)) 

     let attributes: [String: AnyObject] = [ 
      NSFontAttributeName: font 
     ] 
     (text as NSString).drawInRect(rectangle, withAttributes: attributes) 

     let image = UIGraphicsGetImageFromCurrentImageContext() 
     UIGraphicsEndImageContext() 

     return image 
    } 
} 

wie Sie NSString ‚s drawInRect nimmt nun eine [String: AnyObject]? für seine Attribute zu sehen. Ich konnte die entsprechenden Schlüssel für lineBreakMode und alignment nicht finden.

Als documentation heißt es:

Dies sind die gleichen Attribute, die auf ein NSAttributedString Objekt angewendet werden kann ...


Update:

Ich habe hinzugefügt, die andere Spezifikationen:

extension UIImage { 
    func withText(text: String) -> UIImage { 
     UIGraphicsBeginImageContext(size) 

     let rectangle = CGRect(x: 0, y: 0, width: size.width, height: size.height) 
     drawInRect(rectangle) 

     let fontSize = text.characters.count > 200 ? 10 : 14 
     let font = UIFont.boldSystemFontOfSize(CGFloat(fontSize)) 

     let paragraphAttributes = NSMutableParagraphStyle() 
     paragraphAttributes.lineBreakMode = NSLineBreakMode.ByTruncatingTail 
     paragraphAttributes.alignment = NSTextAlignment.Center 

     let attributes: [String: AnyObject] = [ 
      NSFontAttributeName: font, 
      NSForegroundColorAttributeName: UIColor.redColor(), 
      NSParagraphStyleAttributeName: paragraphAttributes 
     ] 
     (text as NSString).drawInRect(rectangle, withAttributes: attributes) 

     let image = UIGraphicsGetImageFromCurrentImageContext() 
     UIGraphicsEndImageContext() 

     return image 
    } 
} 

Und nur sofern dies war unklar, vor ... Dies ist, wie Sie die Methode verwenden würden:

// `someImage` is an optional `UIImage` (`UIImage?`), as the initializer is failable 
let someImage = UIImage(named: "something.png") 

// `imageWithText` is of type `UIImage` here 
if let image = someImage { 
    let imageWithText = someImage.withText("Hello World") 
} 

// `imageWithText` is of type `UIImage?` here 
let imageWithText = someImage?.withText("Hello World") 
+0

Danke, aber ich versuchte, den Code und es funktioniert nicht ... Was ich bin versuchen zu tun ist ein Text in jedes Bild ... Können Sie mir dabei helfen ??? Danke !!! –

+0

Ein Update hinzugefügt. Ich habe es auf einem Spielplatz versucht und es funktioniert. –

+0

Danke, es hat funktioniert !!!! Und, wie kann ich die Größe des Textes ändern, um größer zu sein ??? –

Verwandte Themen