在图像右上角绘制文本

use*_*523 5 uiimage ios swift

我想在 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!
}
}
Run Code Online (Sandbox Code Playgroud)

问题是,如果文本太长或太大,它将超出屏幕。我需要更改原点以从右向左写入文本,因此它只会在屏幕左侧而不是右侧占用空间。我怎么能那样做?

实际问题

我想要的是

Don*_*Mag 5

有一个简单的函数调用来获取字符串的“边界框”。您可以使用它来定位应将文本绘制到图像中的位置:

    // 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)
Run Code Online (Sandbox Code Playgroud)

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

顺便说一句......drawText是一个可怕的变量名称 - 它听起来像一个函数。使用类似textToDraw.


这是一个使用atCorner参数的修改函数,其中值是:

        +-----------+ 
        | 0       1 | 
        |           | 
        |           | 
        | 3       2 |
        +-----------+
Run Code Online (Sandbox Code Playgroud)

编辑:使用右对齐的段落样式(由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!
    }

}
Run Code Online (Sandbox Code Playgroud)