ios - 在图像上绘制的文本延伸出图像

标签 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)
          }
        }

最佳答案

如果您想向左绘制,您实际上可以使用 boundingRectWithSize:options:attributes “计算将要绘制的文本的大小,然后移动绘制点以适应该大小” :上下文:。例如:

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)
}

关于ios - 在图像上绘制的文本延伸出图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40709181/

相关文章:

ios - 如何从 iOS 中的 YTPlayer View 中删除标题和 channel 图标

ios - 在iOS中检查蓝牙状态

iphone - 仿射变换与关键帧

iphone - 使用CGPath画一条线

ios - collectionView 中的图像在滚动时发生变化

ios - 外围 BLE 设备的唯一标识符

swift - 全屏来电推送通知 - iOS

arrays - 快速计算数组数字输入?

ios - 如何防止 UITableView 向下滚动?

macos - 调整大小时 NSWindow 重画问题/闪烁