In the storyboard I layout a set of labels with various formatting options.
Then I do:
label.text = @"Set programmatically";
And all formatting is lost! This works fine in iOS5.
There must be a way of just updating the text string without recoding all the formatting?!
label.attributedText.string
is read only.
Thanks in advance.
Although new to iOS programming, I encountered the same problem very quickly. In iOS, my experience is that
Having looked around s/o, I came across This Post and followed that recommendation, I ended up using this:
You can extract the attributes as a dictionary with:
Then add them back with the new text:
This assumes the label has text in it, otherwise you'll crash so you should probably perform a check on that first with:
An attributedString contains all of its formatting data. The label doesn't know anything about the formats at all.
You could possibly store the attributes as a separate dictionary and then when you change the attributedString you can use:
The only other option is to build the attributes back up again.