在Swift 3中,如何在Core Graphics上绘制文本?

18
背景:我正在使用核心图形在UIImageView上绘制。我最终想在核心图形绘制的上面绘制文本字符串。 这个(hackingwithswift.com)方法是我用来将文本绘制到图像上,将我的imageView设置为该图像,然后用核心图形进行绘制。但是我希望文本覆盖核心图形绘制。
现在我已经转向了https://dev59.com/XmEi5IYBdhLWcg3wd8EF#35574171,但它仍然不允许我在我的绘图上面写字。
class MapViewController: UIViewController {

    @IBOutlet var imageView: UIImageView!

    func drawScale() {
        // establish 6 points to draw a little bar with
        let length = CGFloat(80.0)
        let bottom = imageView.bounds.size.height - 15.0
        let left = CGFloat(20.0)
        let top = bottom - 20.0
        let middle = top + 10.0
        let right = left + length
        let topLeft = CGPoint(x: left, y: top)
        let topRight = CGPoint(x: right, y: top)
        let bottomRight = CGPoint(x: right, y: bottom)
        let bottomLeft = CGPoint(x: left, y: bottom)
        let middleLeft = CGPoint(x: left, y: middle)
        let middleRight = CGPoint(x: right, y: middle)

        // draw the bar
        drawLineFrom(fromPoint: topLeft, toPoint: bottomLeft, color: UIColor.red.cgColor, width: 1.0, alias: false)
        drawLineFrom(fromPoint: topRight, toPoint: bottomRight, color: UIColor.red.cgColor, width: 1.0, alias: false)
        drawLineFrom(fromPoint: middleLeft, toPoint: middleRight, color: UIColor.red.cgColor, width: 1.0, alias: false)

        // draw a text string
        UIGraphicsBeginImageContext(self.imageView.frame.size)

        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.alignment = .center

        let attrs = [
            NSFontAttributeName: UIFont.systemFont(ofSize: 12),
            NSParagraphStyleAttributeName: paragraphStyle,
            NSForegroundColorAttributeName: UIColor.red]

        let scaleString = "45 feet"
        scaleString.draw(at: CGPoint(x: 0, y: 50), withAttributes: attrs)

        let newImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
    }

    /* Stroke a line between two CGPoints. Color, width, anti-alias options. */
    func drawLineFrom(fromPoint: CGPoint, toPoint: CGPoint, color: CGColor, width: CGFloat, alias: Bool) {
        // 1
        UIGraphicsBeginImageContext(self.imageView.frame.size)
        let context = UIGraphicsGetCurrentContext()!
        context.setShouldAntialias(alias)
        self.imageView.image?.draw(in: CGRect(x: 0, y: 0, width: self.imageView.frame.size.width, height: self.imageView.frame.size.height))

        // 2
        context.move(to: fromPoint)
        context.addLine(to: toPoint)

        // 3
        context.setLineWidth(width)
        context.setStrokeColor(color)

        // 4
        context.strokePath()

        // 5
        self.imageView.image = UIGraphicsGetImageFromCurrentImageContext()
        self.imageView.alpha = 1.0
        UIGraphicsEndImageContext()
    }
}

无法正常工作。绘制文本字符串会完全擦除该条。如果将该条绘制代码移至字符串绘制代码下方,则两者均可见,但当然该条会覆盖字符串。

你能否将文本创建为CATextLayer并添加到图像视图中? - user7014451
你所说的“我正在使用核心图形在UIImageView上绘图”,是什么意思?你的代码运行在哪个类下面? - El Tomato
我添加了一些上下文代码,希望能有所帮助。我认为答案可能在Core Text中,我将尝试自己找到解决方案。像链接这样的东西看起来很有前途。 - Harry Netzer
2个回答

24

使用Swift 4:

let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .center

let attributes = [
    NSAttributedStringKey.paragraphStyle: paragraphStyle,
    NSAttributedStringKey.font: UIFont.systemFont(ofSize: 12.0),
    NSAttributedStringKey.foregroundColor: UIColor.blue
]

let myText = "HELLO"
let attributedString = NSAttributedString(string: myText, attributes: attributes)

let stringRect = CGRect(x: W/2 - 50, y: border, width: 100, height: H)
attributedString.draw(in: stringRect)

对于Swift 4.2,将上述attributes更改为:

let attributes: [NSAttributedString.Key : Any] = [
    .paragraphStyle: paragraphStyle,
    .font: UIFont.systemFont(ofSize: 12.0),
    .foregroundColor: UIColor.blue
]

Swift 4.2 的 NSAttributedString.Key.xxx - William Hu
我可以问一下相对于使用UILabel,绘制文本的优势是什么吗?抱歉,我刚接触Swift。 - Kevvv
如果您正在使用自定义视图,则无法使用UIlabel。 - ingconti

4
Swift 3.0:绘制文本
   func drawMyText(myText:String,textColor:UIColor, FontName:String, FontSize:CGFloat, inRect:CGRect){

     let textFont = UIFont(name: FontName, size: FontSize)!
    let textFontAttributes = [
        NSFontAttributeName: textFont,
        NSForegroundColorAttributeName: textColor,
        ] as [String : Any]

    myText.draw(in: inRect, withAttributes: textFontAttributes)
}

调用这个函数,下面是一个例子。
let _rect = CGRect(x: 40, y: 100, width: 40, height: 40)

 drawMyText(myText: "Hello", textColor: UIColor.black, FontName: "Helvetica Bold", FontSize: 11 , inRect:_rect)

那只是在您的代码中删除了 UIGraphicsBeginImageContext(self.imageView.frame.size)。您的代码在我的情况下没有绘制任何文本。 - Harry Netzer
将drawMyText函数放在UIView子类中,并从drawRect函数中调用它,如果需要进一步帮助,请告诉我。 - Devesh.452
1
我的问题是绘制文本会擦除之前绘制的内容。在我上面的示例中,文本将出现,但它完全擦除了我之前绘制的线条。据我所知,您只建议我在矩形中绘制文本而不是在点上,这对我没有任何帮助。 - Harry Netzer
小文本。我想将文本绘制为图像大小。 - shinriyo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接