2016-11-20 62 views
0

我使用SO POST的答案在图像上绘制文字。我遇到的问题是当CGPoint设置在图像的右边缘附近时,文本从图像延伸出来。我试图找出一种方法来从CGPoint中绘制左侧而不是右侧,或者以某种方式计算将绘制的文本的点的大小,然后移动绘制点以适应该大小。从图像上延伸出的图像上的文字绘制

这是我使用的代码。

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

    let color = UserDefaults.standard.colorForKey(key: "color")! 
    let font = UserDefaults.standard.value(forKey: "font") as! String 
    let size = UserDefaults.standard.value(forKey: "size") as! Int 
    let fontAndSize = UIFont(name: font, size: CGFloat(size))! 

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

    let textFontAttributes = [ 
     NSFontAttributeName: fontAndSize, 
     NSForegroundColorAttributeName: color, 
     ] as [String : Any] 
    image.draw(in: CGRect(origin: CGPoint.zero, size: image.size)) 

    let rect = CGRect(origin: point, size: image.size) 
    text.draw(in: rect, withAttributes: textFontAttributes) 

    let newImage = UIGraphicsGetImageFromCurrentImageContext() 
    UIGraphicsEndImageContext() 

    return newImage! 
    } 

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { 
    if let mediaType = info[UIImagePickerControllerMediaType] as? String { 
     if mediaType.isEqual((kUTTypeImage as String)) { 
     if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage { 
      let width = pickedImage.size.width 
      let height = pickedImage.size.height 
      // this puts the point in the bottom right of the image which then causes the text drawn to extend off of the image 
      let point = CGPoint(x: width - 20, y: height - 50) 
      let timestampText = getTimestampText() as NSString 
      let timestampImage = textToImage(drawText: timestampText, inImage: pickedImage, atPoint: point) 
      if newMedia { 
      UIImageWriteToSavedPhotosAlbum(timestampImage, nil, nil, nil) 
      } 
     } 

回答

2

如果你想画的左边,在文本的点,你其实可以“计算尺寸将被绘制然后移动绘画点适应这个大小“通过使用boundingRectWithSize:options:attributes:context:。例如:

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

    let color = UserDefaults.standard.colorForKey(key: "color")! 
    let font = UserDefaults.standard.value(forKey: "font") as! String 
    let size = UserDefaults.standard.value(forKey: "size") as! Int 
    let fontAndSize = UIFont(name: font, size: CGFloat(size))! 

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

    let textFontAttributes = [ 
     NSFontAttributeName: fontAndSize, 
     NSForegroundColorAttributeName: color, 
     ] as [String : Any] 

    // Calculate text size 
    let rectSize = text.boundingRect(with: CGSize(width:CGFloat(MAXFLOAT), height: CGFloat(MAXFLOAT)), options: NSStringDrawingOptions.usesLineFragmentOrigin, attributes: textFontAttributes, context: nil).size 

    // Subtract the text width from the x origin 
    let rect = CGRect(x:point.x-rectSize.width, y:point.y, width:rectSize.width, height: rectSize.height) 
    text.draw(in: rect, withAttributes: textFontAttributes) 
} 
+0

谢谢!这工作。我也想弄清楚如何在中心画画。为此,我将rect属性修改为:let rect = CGRect(x:point.x-(rectSize.width/2),y:point.y,width:rectSize.width,height:rectSize.height)。这给了我正确的x位置,但是y位置不是完全居中,而是位于图像中央下方。有关为什么它不完全居中的任何建议? – chickenparm

+1

没关系!我改变它让rect = CGRect(x:point.x-(rectSize.width/2),y:point.y-(rectSize.height/2),width:rectSize.width,height:rectSize.height)和这是工作。 – chickenparm

+0

@chickenparm很高兴听到:) –

0

您可以尝试对齐文本向右使用NSParagraphStyleAttributeName

let textStyle = NSMutableParagraphStyle.defaultParagraphStyle().mutableCopy() as NSMutableParagraphStyle 
textStyle.alignment = NSTextAlignment.rightTextAlignment 

let textFontAttributes = [ 
    NSFontAttributeName: fontAndSize, 
    NSForegroundColorAttributeName: color, 
    NSParagraphAttributeStyleName: textStyle 
    ] as [String : Any] 
+0

比你的回应。我无法完全按照您的反应所希望的那样工作,但我能够与Lyndsey的回应一起工作。 – chickenparm