如何在Swift中更改CATextLayer中的文本颜色 [英] How to change the text color in a CATextLayer in Swift

查看:851
本文介绍了如何在Swift中更改CATextLayer中的文本颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更改CATextLayer的文本颜色。



这不起作用

  myTextLayer.textColor 

因为没有这样的财产。设置前景色我也没有回应

  textLayer.foregroundColor = someColor.CGColor 

当文本图层设置如下时

  let myAttribute = [NSFontAttributeName:UIFont(name:mongolFontName,size:fontSize)! ] 
let attrString = NSMutableAttributedString(string:textLayer.displayString,attributes:myAttribute)
textLayer.frame = myFrame
textLayer.string = attrString

我见过Objective-C问题

  let myTextLayer = CATextLayer()
myTextLayer.string =我的文字
myTextLayer.backgroundColor = UIColor.blue.cgColor
myTextLayer.foregroundColor = UIColor.cyan.cgColor
myTextLayer.frame = myView.bounds
myView.layer.addSub layer(myTextLayer)

如果未设置颜色,则背景的默认值均为白色和前景。



使用归因字符串



根据



答案更新为Swift 4


I want to change the text color of a 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.

Since I was able to solve my problem by reading the documentation, I am sharing the answer below.

解决方案

General Case

To set the text color of a CATextLayer use

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.

Using an Attributed String

According to the documentation,

The foregroundColor property is only used when the string property is not an 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中更改CATextLayer中的文本颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆