4

How to set UILabel text attributes only once and then just change text (string)

mTextValue.attributedText = NSAttributedString(string: "STRING", 
       attributes: 
           [NSAttributedStringKey.strokeWidth: -3.0,      
            NSAttributedStringKey.strokeColor: UIColor.black])


mTextValue.text = "NEW STRING" // won't change anything

or to set new string do I have to set NSAttributedString to .attributedText again and again?

7
  • if you want attributedText then you need to create attributed string and set attributedText. Commented Feb 9, 2018 at 9:01
  • @Neel Bhasin it's easier to read code when there is no scrolls
    – user25
    Commented Feb 9, 2018 at 9:02
  • but that was scrolling already with extra spaces Commented Feb 9, 2018 at 9:03
  • @NeelBhasin yes I do it already, but text is going to be changed every 1 sec, why there is no option just to change text after I already set attributes why do I have to set attributes again if I already did
    – user25
    Commented Feb 9, 2018 at 9:03
  • If you are looking for iOS API there might be none, you may need to write a wrapper class/method of your own some where.
    – GoodSp33d
    Commented Feb 9, 2018 at 9:04

1 Answer 1

5

You could declare a mutableAttributed string separat and change the string of it like here:

let yourString = "my string"
let yourAttributes = [NSAttributedStringKey.strokeWidth: -3.0, NSAttributedStringKey.strokeColor: UIColor.black] as [NSAttributedStringKey : Any]
let mutableAttributedString = NSMutableAttributedString(string: yourString, attributes: yourAttributes)

let yourNewString = "my new string"
mutableAttributedString.mutableString.setString(yourNewString)

Full example:

import UIKit

class ViewController: UIViewController {

    var mutableAttributedString = NSMutableAttributedString()

    @IBAction func buttonTapped(_ sender: Any) {

        mutableAttributedString.mutableString.setString("new string")
        mainLabel.attributedText = mutableAttributedString

    }

    @IBOutlet weak var mainLabel: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()

        let yourString = "my string"
        let yourAttributes = [NSAttributedStringKey.strokeWidth: -3.0, NSAttributedStringKey.strokeColor: UIColor.blue] as [NSAttributedStringKey : Any]
        mutableAttributedString = NSMutableAttributedString(string: yourString, attributes: yourAttributes)

        mainLabel.attributedText = mutableAttributedString
    }

}
7
  • doesn't work (swift 4): let mutableAttributedString = NSMutableAttributedString(string: "09:52:35", attributes: [NSAttributedStringKey.strokeWidth: -3.0, NSAttributedStringKey.strokeColor: UIColor.black] as [NSAttributedStringKey : Any]) mTextValue.attributedText = mutableAttributedString mutableAttributedString.mutableString.setString("09:52:36")
    – user25
    Commented Feb 9, 2018 at 9:22
  • text won't change after I set mTextValue.attributedText = mutableAttributedString and trying to set new string after it mutableAttributedString.mutableString.setString("new string")
    – user25
    Commented Feb 9, 2018 at 9:25
  • What is myTextValue? A label, a button ?
    – Kingalione
    Commented Feb 9, 2018 at 9:27
  • it is a UILabel
    – user25
    Commented Feb 9, 2018 at 9:28
  • Ok i'll check it out now
    – Kingalione
    Commented Feb 9, 2018 at 9:29

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.