Swift 3 - NSString.draw(in: rect, withAttributes:) - 文本未在预期点绘制 [英] Swift 3 - NSString.draw(in: rect, withAttributes:) -- Text not being drawn at expected point

查看:79
本文介绍了Swift 3 - NSString.draw(in: rect, withAttributes:) - 文本未在预期点绘制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

开始这篇 Stackoverflow 帖子,这非常有帮助,我已经能够成功地将文本绘制到全屏图像上(我用预装的短字符串标记图像,例如垃圾箱").但是,文本没有出现在我想要的地方,它以用户点击的确切点为中心.这是我的代码,基于上面帖子中的一些代码,但针对 Swift3 进行了更新 --

Teeing off of this Stackoverflow post, which was very helpful, I've been able to successfully draw text onto a full-screen image (I'm tagging the image with pre-canned, short strings, e.g., "Trash"). However, the text isn't appearing where I want, which is centered at the exact point the user has tapped. Here's my code, based on some code from the above post but updated for Swift3 --

    func addTextToImage(text: NSString, inImage: UIImage, atPoint:CGPoint)     -> UIImage{

    // Setup the font specific variables
    let textColor: UIColor = UIColor.red
    let textFont: UIFont = UIFont(name: "Helvetica Bold", size: 80)!

    //Setups up the font attributes that will be later used to dictate how the text should be drawn
    let textFontAttributes = [
        NSFontAttributeName: textFont,
        NSForegroundColorAttributeName: textColor,
        ]

    // Create bitmap based graphics context
    UIGraphicsBeginImageContextWithOptions(inImage.size, false, 0.0)

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

    // Create the rectangle where the text will be written
    let rect: CGRect = CGRect(x:atPoint.x, y:atPoint.y, width:inImage.size.width, height: inImage.size.height)

    // Draft the text in the rectangle
    text.draw(in: rect, withAttributes: textFontAttributes)

    // Get the image from the graphics context
    let newImag = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    return newImag!
}

在上面,atPoint 是用户点击的位置.这是我想要绘制文本的地方.但是,文本始终写在图像的左上角.例如,在附加的图像中,我点击了瀑布的一半,因为这是我想要写入文本字符串垃圾箱"的地方.但是,您可以看到它写在左上角.我已经尝试了很多东西,但无法得到解决方案.我很感激任何帮助.在此处输入图片描述

In the above, atPoint is the location of the user's tap. This is where I want the text to be drawn. However, the text is always written toward the upper left corner of the image. For example, in the attached image, I have tapped half way down the waterfall as that is where I want the text string "Trash" to be written. But instead, you can see that it is written way up in the left-hand corner. I've tried a bunch of stuff but can't get a solution. I appreciate any help. enter image description here

TrashShouldBeInMiddleOfWaterfallButIsNot

推荐答案

你如何设置 atPoint?如果您使用与屏幕相同的坐标空间,那将不起作用……我怀疑这就是正在发生的事情.

How are you setting atPoint? If you are using the same coordinate space as the screen, that won't work... which is what I suspect is happening.

假设您的图像是 1000 x 2000,并且您在 100 x 200UIImageView 中显示它.如果您点击视图中的 x: 50 y: 100,然后将该点发送到您的函数,它将在 x: 50 y: 100 处绘制文本 图像 -- 将在左上角,而不是在中心.

Suppose your image is 1000 x 2000, and you are showing it in a UIImageView that is 100 x 200. If you tap at x: 50 y: 100 in the view (at the center), and then send that point to your function, it will draw the text at x: 50 y: 100 of the image -- which will be in the upper-left corner, instead of in the center.

因此,您需要在调用函数之前或通过修改函数来处理它之前,将点从图像视图大小转换为实际图像大小.

So, you need to convert your point from the Image View size to the actual image size.. either before you call your function, or by modifying your function to handle it.

一个例子(不一定是最好的方法):

An example (not necessarily the best way to do it):

    // assume:
    //    View Size is 100 x 200
    //    Image Size is 1000 x 2000
    //    tapPoint is CGPoint(x: 50, y: 100)

    let xFactor = image.size.width / imageView.frame.size.width
    // xFactor now equals 10

    let yFactor = image.size.height / imageView.frame.size.height
    // yFactor now equals 10

    let convertedPoint = CGPoint(x: tapPoint.x * xFactor, y: tapPoint.y * yFactor)

convertedPoint 现在等于 CGPoint(x: 500, y: 1000),您可以在调用 addTextToImage 时将其作为 atPoint 值发送代码>.

convertedPoint now equals CGPoint(x: 500, y: 1000), and you can send that as the atPoint value in your call to addTextToImage.

这篇关于Swift 3 - NSString.draw(in: rect, withAttributes:) - 文本未在预期点绘制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆