Carl-
Carl-

Reputation: 241

Storyboard prototype cell attributed text label does not display same as prototype

When using a TableView with Custom Cells, I have 4 labels of different fonts/colors. In Storyboard (Left side of picture), they show up as different fonts/sizes and colors, but when I run the app in Simulator (Right side of picture), they revert back to standard fonts and sizes. (Needed 10 rep points to post screenshot)

I set the label to "Attributed, Picked the font, color, and size in storyboard (Under label) and all looks good in the prototype cell on the storyboard. But when I run the app, these attributes are being ignored. What am I missing?

Upvotes: 1

Views: 1902

Answers (1)

Timothy Moose
Timothy Moose

Reputation: 9915

When you set the label's text in code, the storyboard attributes are overwritten. One way to solve this is to copy the attributes from the label and and then reapply them when setting the text:

UILabel *label = cell.lblAmount;
NSMutableDictionary *existingAttributes = [[NSMutableDictionary alloc] initWithDictionary:[label.attributedText attributesAtIndex:0 effectiveRange:nil]];
label.attributedText = [[NSAttributedString alloc] initWithString:@"12,345.67" attributes:existingAttributes];

Note that you don't need to do this with the built in labels, e.g. textLabel. I think the table view is somehow re-applying the storyboard attributes before displaying the cell.

Upvotes: 0

Related Questions