如何在CGContext中绘制String?

时间:2018-10-18 11:58:45

标签: ios swift calayer cgcontext

我正在尝试在draw的重写CALayer方法中绘制字符串(我正在为iOS编程)。

override func draw(in ctx: CGContext) {
    let font = UIFont.systemFont(ofSize: 30)
    let string = NSAttributedString(string: "23", attributes: [NSAttributedStringKey.font: font])
    string.draw(at: CGPoint(x: 200, y: 200))
}

但是,这没有画任何东西(至少看不到任何东西)。更改填充和描边颜色没有什么不同。

如果我画一条线,它将显示出来,因此正在调用该函数。我知道有一个CATextLayer,但是我需要直接绘制字符串。在Swift 4时代,您应该如何在CGContext中绘制字符串?没有大量的网络搜索可以得出答案。

2 个答案:

答案 0 :(得分:2)

我假设您知道所有其他设置。这里的关键是您尚未将CGContext设置为当前版本。只需添加两行代码即可解决该问题。希望你能得到答案。

   override func draw(in ctx: CGContext) {
   UIGraphicsPushContext(ctx)
    let font = UIFont.systemFont(ofSize: 30)
    let string = NSAttributedString(string: "23", attributes: [NSAttributedString.Key.font: font])
    string.draw(at: CGPoint(x: 200, y: 200))
   UIGraphicsPopContext()
}

答案 1 :(得分:2)

如果焦点集中在视图上,则上述答案有效。

不幸的是,它无法在屏幕外的位图或CALayer上下文中使用。

在任何CGContext中绘制字符串的正确且通用的方法是使用 CoreText API。它可以在所有Apple平台上运行,并且功能强大。

https://developer.apple.com/documentation/coretext

示例:

import Foundation
import Quartz

/// LazyFoxLayer
///
/// Will draw the "The lazy fox…" string in the bottom right corner of the layer,
/// with a margin of 10 pixels.

class LazyFoxLayer: CALayer {

    func draw(ctx: CGContext) {
        ctx.saveGState()
    
        // Parameters

        let margin: CGFloat = 10
        let color = CGColor.black
        let fontSize: CGFloat = 32
        // You can use the Font Book app to find the name
        let fontName = "Chalkboard" as CFString 
        let font = CTFontCreateWithName(fontName, fontSize, nil)

        let attributes = [.font: font, .foregroundColor: color]

        // Text

        let string = "The lazy fox…"
        let attributedString = NSAttributedString(string: string, 
                                                  attributes: attributes)

        // Render

        let line = CTLineCreateWithAttributedString(attributedString)
        let stringRect = CTLineGetImageBounds(line, ctx)

        ctx.textPosition = CGPoint(x: bounds.maxX - stringRect.width - margin, 
                                   y: bounds.minY + margin)

        CTLineDraw(line, ctx)

        ctx.restoreGState()
    }
}

干杯:)