Posts mit dem Label UIImage werden angezeigt. Alle Posts anzeigen
Posts mit dem Label UIImage werden angezeigt. Alle Posts anzeigen

Dienstag, 27. Januar 2015

Create an UIImage from String in Swift / Erstelle ein UIImage aus einem String in Swift


Swift Source Code:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
func imageFromText(#text:String,font:UIFont,color:UIColor)->UIImage
    {
        let size : CGSize = (text as NSString).sizeWithAttributes([NSFontAttributeName: font])
        
        UIGraphicsBeginImageContextWithOptions(size, false, 0.0)
        (text as NSString).drawAtPoint(CGPointMake(0.0, 0.0), withAttributes: [NSFontAttributeName: font, NSForegroundColorAttributeName:color])
        
        let image : UIImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        
        return image
    }