【发布时间】:2023-08-25 02:58:01
【问题描述】:
我正在尝试实现在CAShapeLayer 内绘制的文本。为此,我需要从文本中创建 UIBezierPath。基于text path solution 应该一切正常,但遗憾的是我找不到问题所在。
CTFontCreatePathForGlyph(runFont, glyph, nil) 创建的路径从反面画了字母。
转换成Swift 4
的代码 func initializeTextLayer() {
let fontName: CFString = "Noteworthy-Light" as CFString
let letters = CGMutablePath()
let font = CTFontCreateWithName(fontName, 50, nil)
let attrString = NSAttributedString(string: "TEST", attributes: [kCTFontAttributeName as NSAttributedStringKey : font])
let line = CTLineCreateWithAttributedString(attrString)
let runArray = CTLineGetGlyphRuns(line)
for runIndex in 0..<CFArrayGetCount(runArray) {
let run : CTRun = unsafeBitCast(CFArrayGetValueAtIndex(runArray, runIndex), to: CTRun.self)
let dictRef : CFDictionary = CTRunGetAttributes(run)
let dict : NSDictionary = dictRef as NSDictionary
let runFont = dict[kCTFontAttributeName as String] as! CTFont
for runGlyphIndex in 0..<CTRunGetGlyphCount(run) {
let thisGlyphRange = CFRangeMake(runGlyphIndex, 1)
var glyph = CGGlyph()
var position = CGPoint.zero
CTRunGetGlyphs(run, thisGlyphRange, &glyph)
CTRunGetPositions(run, thisGlyphRange, &position)
let letter = CTFontCreatePathForGlyph(runFont, glyph, nil)
let t = CGAffineTransform(translationX: position.x, y: position.y)
if let letter = letter {
letters.addPath(letter, transform: t)
}
}
}
let path = UIBezierPath()
path.move(to: CGPoint.zero)
path.append(UIBezierPath(cgPath: letters))
let pathLayer = CAShapeLayer()
pathLayer.path = path.cgPath
self.layer.addSublayer(pathLayer)
}
任何想法我可以做些什么来镜像它?我找到了 mirror path 的东西,但没有得到任何结果。
【问题讨论】:
-
几乎可以肯定是因为 iOS 的 y 轴与 Mac-OS 的相反。尝试在渲染前通过 (+1, -1) 保存图形上下文和缩放上下文,然后恢复上下文。
-
您无法在
draw(in rect: CGRect)之外获取当前图形上下文。我只使用CAShapeLayer's 为此。我错了吗 ?请给我一些启示。 -
道歉 - 这是一个 CALayer。解决方案仍然有效,但您需要在创建路径时使用
CGTransform(无论如何您都在为翻译执行此操作)来反转 y 轴。 -
我明白你的意思,正在研究解决方案!
-
干得好 - 你打败了我!您实际上应该将您的答案作为实际答案发布,而不是作为对问题的编辑。这样您就可以 A)将您的答案标记为“正确”,B)也可以对答案进行投票......
标签: swift uibezierpath