NSViewのキャンバスにテキストを描画しようとしています。私は3行のテキストを書いて、それを超えて無視する必要があります。 String.draw(in:withAttributes)は、指定された矩形で完璧に見えるでしょう。私のコードは次のようになります調整なし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)
、私はレンダリングされたテキストの2つだけの行を取得:
を私はこれらのガイドラインに従っています:https://developer.apple.com/library/content/documentation/TextFonts/Conceptual/CocoaTextArchitecture/FontHandling/FontHandling.html#//apple_ref/doc/uid/TP40009459-CH5-SW18
私は何かが足りないのですか?
それはですアプリケーションのiOS版では、同じコード(UIFontで)がうまく動作しているように思われ、3行を示しています。 –