Change string color with NSAttributedString?
There is no need for using NSAttributedString
. All you need is a simple label with the proper textColor
. Plus this simple solution will work with all versions of iOS, not just iOS 6.
But if you needlessly wish to use NSAttributedString
, you can do something like this:
UIColor *color = [UIColor redColor]; // select needed color
NSString *string = ... // the string to colorize
NSDictionary *attrs = @{ NSForegroundColorAttributeName : color };
NSAttributedString *attrStr = [[NSAttributedString alloc] initWithString:string attributes:attrs];
self.scanLabel.attributedText = attrStr;
How to set NSAttributedString text color?
Your problem is on the strokeWidth
property, because you are using a positive number only the stroke is affected. You need to use a negative number to change stroke and fill the text, as stated on the documentation of the strokeWidth property:
Specify positive values to change the stroke width alone. Specify negative values to stroke and fill the text.
let strokeTextAttributes: [NSAttributedString.Key : Any] = [
.strokeColor : UIColor.white,
.foregroundColor : UIColor.red,
.font : UIFont.init(name: "Raleway-ExtraBold", size: 26)!,
.strokeWidth : -0.5,
]
In my opinion, it's also better to specify the data type of the list instead of casting a list to that type specific type.
Swift -How to change the color of a UIButton's NSMutableAttributedString after it has been set
You should insert UIView.animate
function inside of DispatchQueue.main.async {}
to make a smooth transition. Making any kind of animation of the UI asynchronously on the main thread is always recommended.
Swift title change color of attributed string remains default blue color
Use this for the NSMutableAttributedString
attributes
parameter:
attributes: [NSAttributedStringKey.paragraphStyle: style]
and change your `addAttribute code to this:
let forgotCodeAttributedTitle = NSMutableAttributedString(string: "forgot_code_2_lines", attributes: [NSAttributedStringKey.paragraphStyle: style])
Or, even simpler, use this code to set paragraph styling and foreground color in a single statement:
let forgotCodeAttributedTitle = NSMutableAttributedString(string: "forgot_code_2_lines", attributes:
[NSAttributedStringKey.paragraphStyle: style,
NSAttributedStringKey.foregroundColor: UIColor.red])
NSMutableAttributedString text color not working correctly
Here is the solution to your problem. In Swift 4.0 and later, there is struct called NSAttributedStringKey which define the keys for string attributes.
let timeduration = NSMutableAttributedString.init(string: "a dynamic string of time")
timeduration.addAttribute(NSAttributedStringKey.font, value: UIFont.systemFont(ofSize: 16), range: NSRange(location: 0, length: timeduration.length))
timeduration.addAttribute(NSAttributedStringKey.foregroundColor, value:
UIColor(red: 200/255, green: 200/255, blue: 200/255, alpha: 1.0), range:
NSRange(location: 0, length: timeduration.length))
timeduration.addAttribute(NSAttributedStringKey.font, value: UIFont.systemFont(ofSize: 50), range: NSRange(location: 0, length: timecount))
timeduration.addAttribute(NSAttributedStringKey.font, value: UIFont.systemFont(ofSize: 25), range: NSRange(location: timecount, length: 2))
Related Topics
Getting iOS System Uptime, That Doesn't Pause When Asleep
How to Test Apple Push Notification Service Without an Iphone
Can Afnetworking Return Data Synchronously (Inside a Block)
Detecting Sheet Was Dismissed on iOS 13
How to Compare Two Uiimage Objects
How to Crop a Uiimageview to a New Uiimage in 'Aspect Fill' Mode
Swift - Stored Values Order Is Completely Changed in Dictionary
How to Use Uivisualeffectview to Blur Image
How to Share an Image on Instagram in Ios
Move a View Up Only When the Keyboard Covers an Input Field
Check For Internet Connection Availability in Swift
How to Scale Down a Uiimage and Make It Crispy/Sharp At the Same Time Instead of Blurry
Get a List of All Contacts on Ios
Making Text Bold Using Attributed String in Swift
Create Thread Safe Array in Swift