CATextlayer に問題があります。私は CATextlayer にラップされた == YES を設定し、これに対して複数行を自動設定しました。しかし、行間が非常に小さく、非常に見栄えが悪いです。CATextlayer の行間隔を設定する方法はありますか? ありがとう。
2184 次
4 に答える
4
CATextLayer
軽量な用途のみを目的としていると思います。string
プロパティを に設定して、NSAttributedString
フォントやサイズなどの一部の属性を変更できますが、行の高さや間隔など、他の多くの属性は無視されます。
レイヤー内のテキストのレンダリング方法をより細かく制御するには、通常のCALayer
および NSAttributedString
の描画メソッドを使用できます。例えば:
- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx {
[NSGraphicsContext saveGraphicsState];
[NSGraphicsContext setCurrentContext:[NSGraphicsContext graphicsContextWithGraphicsPort:ctx flipped:YES]];
[self.someAttributedString drawInRect:layer.bounds];
[NSGraphicsContext restoreGraphicsState];
}
于 2014-07-07T11:41:13.687 に答える
2
@spinacherCATextLayer
のように、行間などの段落スタイルを無視していることに気付きました。CALayer をサブクラス化し、属性付きの文字列をより直接的に描画することでこれを解決しました。コードの多くはこの記事からコピーされています
class CAAttributedTextLayer: CALayer {
public var attrString = NSAttributedString(string: "")
override func draw(in ctx: CGContext) {
// Flip the coordinate system
ctx.textMatrix = .identity
ctx.translateBy(x: 0, y: bounds.size.height)
ctx.scaleBy(x: 1.0, y: -1.0)
let path = CGMutablePath()
path.addRect(bounds)
let framesetter = CTFramesetterCreateWithAttributedString(attrString as CFAttributedString)
let frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, attrString.length), path, nil)
CTFrameDraw(frame, ctx)
}
}
于 2021-01-06T13:23:22.683 に答える
-2
次のチュートリアルを参照できます。
于 2013-07-22T04:19:26.727 に答える