[英]How to change the text color in a CATextLayer in Swift
I want to change the text color of a CATextLayer. 我想更改CATextLayer的文本颜色。
This does not work 这不起作用
myTextLayer.textColor
since there is no such property. 因为没有这样的财产。 I also got no response by setting the foreground color 通过设置前景色我也没有回应
textLayer.foregroundColor = someColor.CGColor
when the text layer is set up as follows 当文本层设置如下
let myAttribute = [ NSFontAttributeName: UIFont(name: mongolFontName, size: fontSize )! ]
let attrString = NSMutableAttributedString(string: textLayer.displayString, attributes: myAttribute )
textLayer.frame = myFrame
textLayer.string = attrString
I have seen the Objective-C question CATextLayer textcolor is always black but the answers there didn't seem to make sense in my situation. 我已经看到了Objective-C问题CATextLayer textcolor总是黑色的,但那里的答案在我的情况下似乎没有意义。
Since I was able to solve my problem by reading the documentation , I am sharing the answer below. 由于我能够通过阅读文档解决我的问题,我将在下面分享答案。
To set the text color of a CATextLayer use 设置CATextLayer使用的文本颜色
myTextLayer.foregroundColor = UIColor.cyan.cgColor
as in 如在
let myTextLayer = CATextLayer()
myTextLayer.string = "My text"
myTextLayer.backgroundColor = UIColor.blue.cgColor
myTextLayer.foregroundColor = UIColor.cyan.cgColor
myTextLayer.frame = myView.bounds
myView.layer.addSublayer(myTextLayer)
If you don't set the color, the default is white for both the background and the foreground. 如果未设置颜色,则背景和前景的默认值均为白色。
According to the documentation , 根据文件 ,
The
foregroundColor
property is only used when thestring
property is not anNSAttributedString
.foregroundColor
属性仅在string
属性不是NSAttributedString
。
That is why you were not able to change the color. 这就是为什么你无法改变颜色的原因。 You need to add the color to the attributed string in this case. 在这种情况下,您需要将颜色添加到属性字符串。
// Attributed string
let myAttributes = [
NSAttributedStringKey.font: UIFont(name: "Chalkduster", size: 30.0)! , // font
NSAttributedStringKey.foregroundColor: UIColor.cyan // text color
]
let myAttributedString = NSAttributedString(string: "My text", attributes: myAttributes )
// Text layer
let myTextLayer = CATextLayer()
myTextLayer.string = myAttributedString
myTextLayer.backgroundColor = UIColor.blue.cgColor
//myTextLayer.foregroundColor = UIColor.cyan.cgColor // no effect
myTextLayer.frame = myView.bounds
myView.layer.addSublayer(myTextLayer)
which gives 这使
Answer updated to Swift 4 答案已更新至Swift 4
Swift 3 solution (but same problem with other languages). Swift 3解决方案 (但与其他语言相同)。
The secret is in adding titleLayer.display(). 秘诀在于添加titleLayer.display()。
let titleLayer = CATextLayer()
titleLayer.string = "My text"
titleLayer.frame = CGRect(x:0, y:O, width:UIScreen.main.bounds.width, height:UIScreen.main.bounds.width.height)
titleLayer.font = CGFont("HelveticaNeue-UltraLight" as CFString)!
titleLayer.fontSize = 100
titleLayer.alignmentMode = kCAAlignmentCenter
titleLayer.backgroundColor = UIColor.blue.cgColor
titleLayer.foregroundColor = UIColor.cyan.cgColor
titleLayer.display()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.