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.
You can extract the attributes as a dictionary with:
NSDictionary *attributes = [(NSAttributedString *)label.attributedText attributesAtIndex:0 effectiveRange:NULL];
Then add them back with the new text:
label.attributedText = [[NSAttributedString alloc] initWithString:@"Some text" attributes:attributes];
This assumes the label has text in it, otherwise you'll crash so you should probably perform a check on that first with:
if ([self.label.attributedText length]) {...}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With