Change the text of an attributed UILabel without losing the formatting?
Asked Answered
F

3

21

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.

Fiden answered 3/10, 2012 at 10:3 Comment(0)
S
4

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:

[[NSAttributedString alloc] initWithString:@"" attributes:attributes range:range];

The only other option is to build the attributes back up again.

Sferics answered 3/10, 2012 at 10:7 Comment(0)
T
29

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]) {...}
Titmouse answered 17/6, 2013 at 0:37 Comment(1)
Actually, iOS should do it by default, anyway we have our way. Thanks, your suggestion helped me.Evieevil
S
4

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:

[[NSAttributedString alloc] initWithString:@"" attributes:attributes range:range];

The only other option is to build the attributes back up again.

Sferics answered 3/10, 2012 at 10:7 Comment(0)
D
4

Although new to iOS programming, I encountered the same problem very quickly. In iOS, my experience is that

  1. Lewis42's problem occurs consistently
  2. josef's suggestion of extracting and reapplying the attributes does not work: a null attributes dictionary is returned.

Having looked around s/o, I came across This Post and followed that recommendation, I ended up using this:

- (NSMutableAttributedString *)SetLabelAttributes:(NSString *)input col:(UIColor *)col size:(Size)size {

NSMutableAttributedString *labelAttributes = [[NSMutableAttributedString alloc] initWithString:input];

UIFont *font=[UIFont fontWithName:@"Helvetica Neue" size:size];

NSMutableParagraphStyle* style = [NSMutableParagraphStyle new];
style.alignment = NSTextAlignmentCenter;

[labelAttributes addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, labelAttributes.length)];
[labelAttributes addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, labelAttributes.length)];
[labelAttributes addAttribute:NSForegroundColorAttributeName value:col range:NSMakeRange(0, labelAttributes.length)];

return labelAttributes;
Dreyfus answered 20/6, 2013 at 12:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.