Search code examples
iosobjective-cios6ios6.1

NSMutableAttributedString font size changes by accident


I made a NSMutableAttributedString object which consists of 2 different NSMutableAttributedString objects:

NSMutableAttributedString * ob1 = [[NSMutableAttributedString alloc] initWithString:@"Headline"];
    [ob1 addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Arial" size:25.0] range:NSMakeRange(0,8)];
    [ob1 addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:1.0] range:NSMakeRange(0,8)];

NSMutableAttributedString * ob2 = [[NSMutableAttributedString alloc] initWithString:@"Test"];
[ob2 addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Arial" size:25.0] range:NSMakeRange(0,4)];
    [ob2 addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:0.3 green:0.3 blue:0.0 alpha:1.0] range:NSMakeRange(0,4)];

[ob1 appendAttributedString:ob2];

self.aUITextViewObj.attributedText = ob1;

My Problem is: The text is displayed accurate almost everytime in the UITextView. But sometimes its font size becomes bigger. It's totally by accident, there seems to be no rule. The same text which is displayed correct is displayed too big the next time. What i figured out:

  • Problem doesn't appear in the Simulator.
  • On an iPhone 4(iOS 6.1.3) it appears about 90% of the time. On iPhone 5 (iOS 6.1.4)/IPad (iOS 6.1.3) it happens about 10% of the time.

Solution

  • I didn't find out the problem but i found a solution.

    At the beginning i set the font size in the viewDidLoad method. It seems that something changed the font size after this method again.

    But the font size wasn't touched after the viewWillAppear method. So i set the font size in this method again.

    Now the text is displayed correct.

    I hope this helps others with this problem, but I'm still interested in the reason why this happend. Could be a memory problem.