2016-11-16 3 views
0

Ich möchte meinen Text Ausverkauft in der Mitte in UIimage. aber es ist nicht in der Mitte.Wie zeichne Text in der Mitte zu uiimage?

func textToImage(drawText text: NSString, inImage image: UIImage, atPoint point: CGPoint) -> UIImage { 

    let textColor = UIColor.white 
    let textFont = UIFont(name: "Helvetica Bold", size: 32)! 

    let scale = UIScreen.main.scale 
    UIGraphicsBeginImageContextWithOptions(image.size, false, scale) 

    let textFontAttributes = [ 
     NSFontAttributeName: textFont, 
     NSForegroundColorAttributeName: textColor, 
     ] as [String : Any] 

    image.draw(in: CGRect(origin: CGPoint.zero, size: image.size)) 
    // let rect = CGRect(origin: point , size: image.size) 
    let rect = CGRect(origin: CGPoint(x:(image.size.width/2), y: (image.size.height/2)), size: image.size) 

    text.draw(in: rect, withAttributes: textFontAttributes) 

    let newImage = UIGraphicsGetImageFromCurrentImageContext() 
    UIGraphicsEndImageContext() 

    return newImage! 
} 

CLIP-Funktion

imageViewSoldStatus.image = textToImage(drawText: "Sold out", inImage: UIImage(named:"soldout.png")!, atPoint: CGPoint(x: 5.0, y: 5.0)) 

Ich habe sehr lange Zeit damit verbracht, dieses Problem zu lösen, aber ich konnte keine Lösung nicht finden.

Rote Fahne mein Bild

enter image description here

oben ist der Bildschirm I erhalten. unten ist das Design, das ich erreichen möchte.

enter image description here

Update

Marie Dm answerd verwendet! es ist Mitte Text align aber nicht Bildmitte

enter image description here

Antwort

3

Try this:

func textToImage(drawText text: NSString, inImage image: UIImage) -> UIImage { 
    //draw image first 
    UIGraphicsBeginImageContext(image.size) 
    image.draw(in: CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height)) 

    //text attributes 
    let font=UIFont(name: "Helvetica-Bold", size: 32)! 
    let text_style=NSMutableParagraphStyle() 
    text_style.alignment=NSTextAlignment.center 
    let text_color=UIColor.white 
    let attributes=[NSFontAttributeName:font, NSParagraphStyleAttributeName:text_style, NSForegroundColorAttributeName:text_color] 

    //vertically center (depending on font) 
    let text_h=font.lineHeight 
    let text_y=(image.size.height-text_h)/2 
    let text_rect=CGRect(x: 0, y: text_y, width: image.size.width, height: text_h) 
    text.draw(in: text_rect.integral, withAttributes: attributes) 
    let result=UIGraphicsGetImageFromCurrentImageContext() 
    UIGraphicsEndImageContext() 
} 
+0

wo i Größe gefunden? –

+0

Sie sollten wahrscheinlich image.size verwenden. Ich werde meine Antwort bearbeiten. –

+0

nicht funktioniert .. das gleiche Ergebnis –

0

Marie Antwort für eine schnelle Aktualisierung 4

func textToImage(drawText text: NSString, inImage image: UIImage) -> UIImage { 
     UIGraphicsBeginImageContext(image.size) 
     image.draw(in: CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height)) 
     let font=UIFont(name: "Helvetica-Bold", size: 14)! 
     let text_style=NSMutableParagraphStyle() 
     text_style.alignment=NSTextAlignment.center 
     let text_color=UIColor.white 
     let attributes=[NSAttributedStringKey.font:font, NSAttributedStringKey.paragraphStyle:text_style, NSAttributedStringKey.foregroundColor:text_color] 
     let text_h=font.lineHeight 
     let text_y=(image.size.height-text_h)/2 
     let text_rect=CGRect(x: 0, y: text_y, width: image.size.width, height: text_h) 
     text.draw(in: text_rect.integral, withAttributes: attributes) 
     let result=UIGraphicsGetImageFromCurrentImageContext() 
     UIGraphicsEndImageContext() 
     return result! 
    } 
Verwandte Themen