5

I use this code to add attributed strings to a label:

let attrString = NSMutableAttributedString(string: text)
// add some attributes
myLabel.attributedText = attrString

Now is it possible to change only the text of the attributed string myLabel and keeping the attributes?

2
  • Yeah it is possible. But you have to do by attributed string. Commented Aug 21, 2017 at 13:17
  • As far I know. Nope. Each and every time label set attributed text also with the attributed dictionary. If you want to make that previous attribute as function or globe variable. Commented Aug 21, 2017 at 13:19

3 Answers 3

4

Through it's mutableString property

Example:

let astrMessage = NSMutableAttributedString(string: "Hello World")

//set some attributes
astrMessage.addAttribute(NSAttributedStringKey.foregroundColor,
                         value: UIColor.blue,
                         range: NSRange(location: 0, length: 5))
astrMessage.addAttribute(NSAttributedStringKey.foregroundColor,
                         value: UIColor.red,
                         range: NSRange(location: 6, length: 5))

//update
astrMessage.mutableString.setString("World Welcome")

NOTE: Only the first attribute will be applied to the updated text.

Sign up to request clarification or add additional context in comments.

Comments

0

Change the mutable string of your attributed string and reassign the NSMutableAttributedString to your label.

attrString.mutableString.setString("newText")
myLabel.attributedText = attrString

P.S If you do not have the access to your attributed string variable, get it from the label.

let myAttrString = myLabel.attributedText as? NSMutableAttributedString
if let attrString = myAttrString {
    attrString.mutableString.setString("newText")
    myLabel.attributedText = attrString
}

Comments

0

get your label attribute

let linkAttributes = NSMutableAttributedString(attributedString: label.attributedText)

you can give add attribute.

linkAttributes.addAttribute(NSLinkAttributeName, value: "username://marcelofabri_", range: linkRange)
let linkAttributes: [String : Any] = [
    NSForegroundColorAttributeName: UIColor.green,
    NSUnderlineColorAttributeName: UIColor.lightGray,
    NSUnderlineStyleAttributeName: NSUnderlineStyle.styleSingle.rawValue]

Add Attributed text to your label

myLabel.attributedText = linkAttributes 

4 Comments

And how does this answer OP's question? You didn't show how to change the attributed string's string property without changing the attributes...
you can get label attribute and change any one
let newAttributedString = NSMutableAttributedString(attributedString: label.attributedText) use newAttributedString to edit any attribute
Maybe include the relevant code in your answer instead of comments.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.