NSView 캔버스에서 텍스트를 렌더링하려고합니다. 나는 3 줄의 글을 쓰고 넘어서는 것을 무시해야한다. String.draw (in : withAttributes)는 제공된 rect로 완벽하게 처리됩니다. 내 코드는 다음과 같습니다 조정없이NSView로 작성하기
func renderText(_ string:String, x:Double, y:Double, numberOfLines: Int, withColor color:Color) -> Double {
let font = NSFont.boldSystemFont(ofSize: 11)
let lineHeight = Double(font.ascender + abs(font.descender) + font.leading)
let textHeight = lineHeight * Double(numberOfLines) + font.leading // three lines
let textRect = NSRect(x: x, y: y, width: 190, height: textHeight)
string.draw(in: textRect, withAttributes: [NSFontAttributeName: font, NSForegroundColorAttributeName: color])
return textHeight
}
renderText("Lorem ipsum...", x: 100, y: 100, numberOfLines: 3, withColor: NSColor.white)
이 텍스트의 두 줄 렌더링, 내가 얻을 : 내가 뭔가를 놓친 거지 https://developer.apple.com/library/content/documentation/TextFonts/Conceptual/CocoaTextArchitecture/FontHandling/FontHandling.html#//apple_ref/doc/uid/TP40009459-CH5-SW18
:
것은 내가이 지침에 따라 무엇입니까?
그건
내가 boundingRectWithSize 제안에 렌더링되는CGContext
의 규칙을 사용하여 텍스트의 경계를 찾으려면 응용 프로그램의 IOS 버전에서 이상한 방법, 동일한 코드 (UIFont 함께) 세 줄을 보여주는 잘 작동하는 것 같다. –