2017-06-01 136 views
3

我想在UIImage的右上角或右下角绘制文字。 我有一个扩展功能,可以很好地绘制文本,但问题是将文本放置在屏幕右侧(因为文本被剪切)。在图像右上角绘制文字

这里是扩展:

extension UIImage { 

func addText(drawText: NSString, atPoint: CGPoint, textColor: UIColor?, textFont: UIFont?) -> UIImage{ 

    // Setup the font specific variables 
    var _textColor: UIColor 
    if textColor == nil { 
     _textColor = UIColor.white 
    } else { 
     _textColor = textColor! 
    } 

    var _textFont: UIFont 
    if textFont == nil { 
     _textFont = UIFont.systemFont(ofSize: 50) 
    } else { 
     _textFont = textFont! 
    } 

    // Setup the image context using the passed image 
    UIGraphicsBeginImageContext(size) 

    // Setup the font attributes that will be later used to dictate how the text should be drawn 
    let textFontAttributes = [ 
     NSFontAttributeName: _textFont, 
     NSForegroundColorAttributeName: _textColor, 
     ] as [String : Any] 

    // Put the image into a rectangle as large as the original image 
    draw(in: CGRect(x: 0, y: 0, width: size.width, height: size.height)) 

    // Create a point within the space that is as bit as the image 
    let rect = CGRect(x: atPoint.x, y: atPoint.y, width: size.width, height: size.height) 


    // Draw the text into an image 
    drawText.draw(in: rect, withAttributes: textFontAttributes) 

    // Create a new image out of the images we have created 
    let newImage = UIGraphicsGetImageFromCurrentImageContext() 

    // End the context now that we have the image we need 
    UIGraphicsEndImageContext() 

    //Pass the image back up to the caller 
    return newImage! 
} 
} 

问题是,如果文本太长或太大,它会在屏幕中。我需要改变原点,将文本从右侧写入左侧,这样只会在屏幕左侧和右侧留出空间。我怎么能这样做?

actual issue

what I want

+0

将标签添加为子视图并创建imageView的“sceenshot”可能更容易。 [这是如何](https://stackoverflow.com/q/2214957/1457385)。 – shallowThought

回答

2

有一个简单的函数调用来获取 “边框” 的字符串。你可以用它来位置的文字应绘制成图像:

// get the bounding-box for the string 
    let stringSize = drawText.size(attributes: textFontAttributes) 

    // position the bounding-box at the bottom-right corner of the image 
    let x = self.size.width - ceil(stringSize.width) 
    let y = self.size.height - ceil(stringSize.height) 

    let rect = CGRect(x: x, y: y, width: stringSize.width, height: stringSize.height) 

    // Draw the text into an image 
    drawText.draw(in: rect, withAttributes: textFontAttributes) 

请注意,此示例代码位置在右下角的文本 - 忽略atPoint参数。您可能会将其更改为whichCorner类型参数,然后适当计算xy的位置。

顺便说一下... drawText是一个可怕的变量名 - 它听起来像一个函数。使用诸如textToDraw之类的东西更具可读性。


这里是一个修饰的功能,采用了atCorner参数,其中,所述值包括:

 +-----------+ 
     | 0  1 | 
     |   | 
     |   | 
     | 3  2 | 
     +-----------+ 

编辑:使用右对齐段落样式(由Thilina Chamin所建议Hewagama)有一些优点。此编辑版本甚至可以处理带有"\n"嵌入式换行符的文本。

extension UIImage { 

    func addText(textToDraw: NSString, atCorner: Int, textColor: UIColor?, textFont: UIFont?) -> UIImage { 

     // Setup the font specific variables 
     var _textColor: UIColor 
     if textColor == nil { 
      _textColor = UIColor.white 
     } else { 
      _textColor = textColor! 
     } 

     var _textFont: UIFont 
     if textFont == nil { 
      _textFont = UIFont.systemFont(ofSize: 50) 
     } else { 
      _textFont = textFont! 
     } 

     // Setup the image context using the passed image 
     UIGraphicsBeginImageContext(size) 

     // Put the image into a rectangle as large as the original image 
     draw(in: CGRect(x: 0, y: 0, width: size.width, height: size.height)) 

     let titleParagraphStyle = NSMutableParagraphStyle() 

     // Setup the font attributes that will be later used to dictate how the text should be drawn 
     let textFontAttributes = [ 
      NSFontAttributeName: _textFont, 
      NSForegroundColorAttributeName: _textColor, 
      NSParagraphStyleAttributeName: titleParagraphStyle 
      ] as [String : Any] 

     // get the bounding-box for the string 
     var stringSize = textToDraw.size(attributes: textFontAttributes) 

     // draw in rect functions like whole numbers 
     stringSize.width = ceil(stringSize.width) 
     stringSize.height = ceil(stringSize.height) 

     var rect = CGRect(origin: CGPoint.zero, size: self.size) 

     switch atCorner { 

     case 1: 
      // top-right 
      titleParagraphStyle.alignment = .right 

     case 2: 
      // bottom-right 
      rect.origin.y = self.size.height - stringSize.height 
      titleParagraphStyle.alignment = .right 

     case 3: 
      // bottom-left 
      rect.origin.y = self.size.height - stringSize.height 

     default: 
      // top-left 
      // don't need to change anything here 
      break 

     } 

     // Draw the text into an image 
     textToDraw.draw(in: rect, withAttributes: textFontAttributes) 

     // Create a new image out of the images we have created 
     let newImage = UIGraphicsGetImageFromCurrentImageContext() 

     // End the context now that we have the image we need 
     UIGraphicsEndImageContext() 

     //Pass the image back up to the caller 
     return newImage! 
    } 

} 
+0

感谢“边框”功能,这就是我所搜索的。问题解决了! – user3722523

+0

嗨我试过你的解决方案,文本大小位置正确的位置 - 这是辉煌的。 但文字字体大小因图像大小而异。 我可以缩放图像到一个特定的大小和绘制文本,也许这将解决问题,但图像质量不会是一样的.... 任何建议,将不胜感激。 – kamyFC

+0

@kamyFC - 不太清楚你在问什么...... *“文字字体大小因图像大小而异”* ---你是否在创建和传递字体?或者只是使用“默认”? – DonMag