文字绘制在图像上延伸的图像上

时间:2016-11-20 20:45:14

标签: ios swift core-graphics

我正在使用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 个答案:

答案 0 :(得分:3)

如果你想向左画,你实际上可以"计算将要绘制的文本的点的大小,然后移动绘制点以适应这个大小"使用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)
}

答案 1 :(得分:0)

您可以尝试使用NSParagraphStyleAttributeName

将文字对齐
let textStyle = NSMutableParagraphStyle.defaultParagraphStyle().mutableCopy() as NSMutableParagraphStyle
textStyle.alignment = NSTextAlignment.rightTextAlignment

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