Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the text color in a CATextLayer in Swift

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.

like image 898
Suragch Avatar asked Aug 08 '16 17:08

Suragch


People also ask

How do I change the color of my Uilabel Swift?

The easiest workaround is create dummy labels in IB, give them the text the color you like and set to hidden. You can then reference this color in your code to set your label to the desired color. The only way I could change the text color programmatically was by using the standard colors, UIColor.


2 Answers

General Case

To set the text color of a CATextLayer use

myTextLayer.foregroundColor = UIColor.cyan.cgColor

as in

enter image description here

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

enter image description here

Answer updated to Swift 4

like image 102
Suragch Avatar answered Sep 21 '22 10:09

Suragch


Swift 3 solution (but same problem with other languages).

The secret is in adding 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()
like image 23
Joce Avatar answered Sep 24 '22 10:09

Joce