cdub
cdub

Reputation: 25701

NSMutableAttributedString text color not working correctly

I have the following swift 4.1 code:

    let timeduration = NSMutableAttributedString.init(string: "a dynamic string of time");

    timeduration.addAttribute(kCTFontAttributeName as NSAttributedStringKey, value: UIFont.systemFont(ofSize: 16), range: NSRange(location: 0, length: timeduration.length));
    timeduration.addAttribute(kCTForegroundColorAttributeName as NSAttributedStringKey, value: UIColor(red: 200/255, green: 200/255, blue: 200/255, alpha: 1.0), range: NSRange(location: 0, length: timeduration.length));
    timeduration.addAttribute(kCTFontAttributeName as NSAttributedStringKey, value: UIFont.systemFont(ofSize: 50), range: NSRange(location: 0, length: timecount));
    timeduration.addAttribute(kCTFontAttributeName as NSAttributedStringKey, value: UIFont.systemFont(ofSize: 25), range: NSRange(location: timecount, length: 2));

I was using Swift 3 and did the migration using Xcode's tool to swift 4.1 but the foreground color doesn't appear to be working. The XCode tool "Fix" added the kCT codes. The font names and sizes seem to be working though.

Upvotes: 1

Views: 5381

Answers (3)

BinaryGuy
BinaryGuy

Reputation: 1286

The fixit has given you the wrong keys. You want to be using something like this instead:

timeduration.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.red, range: NSRange(location: 0, length: timeduration.length))

Same goes for all of the others.

The keys you are using are for lower level core text stuff.

Upvotes: 0

Naresh
Naresh

Reputation: 955

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))

Upvotes: 3

user9483522
user9483522

Reputation: 304

You can do this way, i hope the problem will be solved:-

let label = UILabel()
let labelText = "String Text"

let strokeTextAttributes = [
     NSAttributedStringKey.strokeColor : UIColor.black,
     NSAttributedStringKey.foregroundColor : UIColor.white,
     NSAttributedStringKey.strokeWidth : -2.0,
     NSAttributedStringKey.font : UIFont.boldSystemFont(ofSize: 18)
   ] as [NSAttributedStringKey : Any]

label.attributedText = NSAttributedString(string: labelText, attributes: strokeTextAttributes)

Upvotes: 0

Related Questions