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
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.
Solutions Collect From Internet About “How to change the text color in a CATextLayer in Swift”
To set the text color of a CATextLayer use
myTextLayer.foregroundColor = UIColor.cyan.cgColor
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,
foregroundColorproperty is only used when the
stringproperty is not an
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 = [ NSFontAttributeName: UIFont(name: "Chalkduster", size: 30.0)! , // font NSForegroundColorAttributeName: 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)
- How do I get the day of the week with Cocoa Touch?
- How do I capture the point initially tapped in a UIPanGestureRecognizer?
- Firebase Cloud Messaging – Check existing or available Topics
- Masking an image in Swift using CALayer and UIImage
- Is it possible to assign an accessibility action to a UILabel?
- Is there any way to code the LaunchScreen programmatically
- Searching NSArray of NSDictionary objects
- Cast a Swift struct to UnsafeMutablePointer<Void>
- Runtime error when using CoreFoundation objects in a swift NSObject subclass
- In Xcode how do I add a breakpoint inside a block?
- Instantiating split view controller by identifier in OS X storyboard does not load view hierarchy
- Swift Continuous Rotation Animation not so continuous
- What goes into your .gitignore if you're using CocoaPods?
- Xcode not copying latest resource file to iPhone
- UnsafeMutablePointer<UInt8> to without memory copy