CATextLayer NSBackGroundColorAttributeName 有没有办法让它工作?
CATextLayer NSBackGroundColorAttributeName is there a way to make it works?
我正在尝试让 CATextLayer
接受 NSBackGroundColorAttributeName
属性字符串中的属性?
let mutableAttributed = NSMutableAttributedString(string: text , attributes: [
NSForegroundColorAttributeName : UIColor.redColor(),
NSBackgroundColorAttributeName : UIColor.whiteColor(),
NSFontAttributeName :UIFont.systemFontOfSize(12)
])
let textLayer = CATextLayer()
textLayer.needsDisplayOnBoundsChange = true
textLayer.string = mutableAttributed
textLayer.bounds.size = CGSizeMake(200,200)
但它根本行不通,阅读 Stack 我找到了一个合理的 answer 但是已经很老了,而且似乎并不完全清楚,如果从 iOS6 它应该支持还是不支持
您指出的答案是正确的。 CATextLayer 非常原始,不支持此功能。幸运的是,也不需要使用核心文本;您现在可以使用 TextKit 并直接绘制字符串(根本不使用 CATextLayer)。
class MyTextLayer : CALayer {
@NSCopying var string : NSAttributedString?
override func drawInContext(ctx: CGContext) {
super.drawInContext(ctx)
UIGraphicsPushContext(ctx)
string?.drawWithRect(self.bounds,
options: .UsesLineFragmentOrigin, context: nil)
UIGraphicsPopContext()
}
}
我正在尝试让 CATextLayer
接受 NSBackGroundColorAttributeName
属性字符串中的属性?
let mutableAttributed = NSMutableAttributedString(string: text , attributes: [
NSForegroundColorAttributeName : UIColor.redColor(),
NSBackgroundColorAttributeName : UIColor.whiteColor(),
NSFontAttributeName :UIFont.systemFontOfSize(12)
])
let textLayer = CATextLayer()
textLayer.needsDisplayOnBoundsChange = true
textLayer.string = mutableAttributed
textLayer.bounds.size = CGSizeMake(200,200)
但它根本行不通,阅读 Stack 我找到了一个合理的 answer 但是已经很老了,而且似乎并不完全清楚,如果从 iOS6 它应该支持还是不支持
您指出的答案是正确的。 CATextLayer 非常原始,不支持此功能。幸运的是,也不需要使用核心文本;您现在可以使用 TextKit 并直接绘制字符串(根本不使用 CATextLayer)。
class MyTextLayer : CALayer {
@NSCopying var string : NSAttributedString?
override func drawInContext(ctx: CGContext) {
super.drawInContext(ctx)
UIGraphicsPushContext(ctx)
string?.drawWithRect(self.bounds,
options: .UsesLineFragmentOrigin, context: nil)
UIGraphicsPopContext()
}
}