Change String Color with Nsattributedstring

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.

how to Replace color in attributed string in objective c

You need to enumerate the NSAttributedString and change the value of the attribute when condition is met.

Text Color attribute is done with NSForegroundColorAttributeName.

attr is the NSMutableAttributedString corresponding to yours.

//Retrieve the current attributedText. You need to make it mutable.
NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithAttributedString:yourLabelOrTextView.attributedText];

[attr enumerateAttribute:NSForegroundColorAttributeName
inRange:NSMakeRange(0, [attr length]) options:0
usingBlock:^(id _Nullable value, NSRange range, BOOL * _Nonnull stop) {
if ([value isKindOfClass:[UIColor class]]) //Check just in case that the value is really a UIColor
{
UIColor *currentColor = (UIColor *)value;
if ([currentColor isEqual:[UIColor redColor]]) //Condition
{
[attr addAttribute:NSForegroundColorAttributeName
value:[UIColor yellowColor]
range:range];
}
}
}];
[yourLabelOrTextView setAttributedText:attr]; //Replace the previous attributedText on UI with the new one.

You could remove the previous color (red) and then apply the new one, but since there is a unicity you don't have to, it will replace it.

Change NSAttributedString html links color

I found an answer for this in swift 4.0

termsAndPolicyTextView.linkTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.red]

Full code:
Note: I can't set multiple color in a single textView.

    let attributedString = NSMutableAttributedString(string: termsAndPolicyText)

attributedString.addAttribute(NSAttributedString.Key.link,
value: "https://google.co.in",
range: (termsAndPolicyText as NSString).range(of: "Terms or service")
)

attributedString.addAttribute(NSAttributedString.Key.link,
value: "https://google.co.in", // Todo set our terms and policy link here
range: (termsAndPolicyText as NSString).range(of: "Privacy & Legal Policy")
)

attributedString.addAttributes([NSAttributedString.Key.foregroundColor: UIColor.NMSTextColor(with: 0.6)],
range: NSRange(location: 0, length: termsAndPolicyText.count))

termsAndPolicyTextView.linkTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.termstextViewTextColor()]
termsAndPolicyTextView.attributedText = attributedString
termsAndPolicyTextView.textAlignment = .center

}

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

NSAttributedString background color is not solid when other attributes are applied

There is an attribute called expansion that adjusts the font's expansion factor using a float value. The problem only occurs where two ranges with different font sizes meet, and this is a graphical rendering issue that cannot be overridden. But if you explicitly zero out the expansion factor, or even apply a small negative value, the gaps will not be noticeable.

attributedString.addAttribute(.expansion, value: NSNumber(value: 0), range: nsText.range(of: "Hello + World!"))

https://developer.apple.com/documentation/foundation/nsattributedstring/key/1524652-expansion?language=objc__5



Related Topics



Leave a reply



Submit