34

The formatting of my textViews worked fine in iOS 6, but no longer in iOS 7. I understand with Text Kit much of the under the hood stuff has changed. It's become really quite confusing, and I'm hoping someone can help straighten it out a bit by helping me with something as simple as this.

My static UITextView originally was assigned a value for it's textColor and textAlignment properties. Then I made a NSMutableAttributedString, assigned it an attributes, then assigned it to the textView's attributedText property. The alignment and color no longer take effect in iOS 7.

How can I fix this? If these properties take no effect, than why do they exist anymore? Here's the creation of the textView:

UITextView *titleView = [[UITextView alloc]initWithFrame:CGRectMake(0, 90, 1024, 150)];
titleView.textAlignment = NSTextAlignmentCenter;
titleView.textColor = [UIColor whiteColor];

NSMutableAttributedString *title = [[NSMutableAttributedString alloc]initWithString:@"Welcome"];
UIFont *font = [UIFont fontWithName:@"Avenir-Light" size:60];
[title addAttribute:NSParagraphStyleAttributeName value:font range:NSMakeRange(0, title.length)];
titleView.attributedText = title;

[self.view addSubview:titleView];
Joe
  • 854
  • 3
  • 11
  • 21

1 Answers1

67

Curious, the properties are taken into account for UILabel but not for UITextView

Why don't you just add attributes for color and alignment to the attributed string similar to the way you are doing with the font?

Something like:

NSMutableAttributedString *title = [[NSMutableAttributedString alloc]initWithString:@"Welcome"];
UIFont *font = [UIFont fontWithName:@"Avenir-Light" size:60];
[title addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, title.length)];

//add color
[title addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:NSMakeRange(0, title.length)];

//add alignment
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setAlignment:NSTextAlignmentCenter];
[title addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, title.length)];

titleView.attributedText = title;

Edit: Assign the text first, then change the properties and this way it works.

UITextView *titleView = [[UITextView alloc]initWithFrame:CGRectMake(0, 90, 1024, 150)];

//create attributed string and change font
NSMutableAttributedString *title = [[NSMutableAttributedString alloc]initWithString:@"Welcome"];
UIFont *font = [UIFont fontWithName:@"Avenir-Light" size:60];
[title addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, title.length)];

//assign text first, then customize properties
titleView.attributedText = title;
titleView.textAlignment = NSTextAlignmentCenter;
titleView.textColor = [UIColor whiteColor];
jlhuertas
  • 1,477
  • 10
  • 9
  • When I do this, the NSFontAttributeName attribute seems to no longer be take into effect. It centers fine, the color is fine, but the font size seems to be at system size. I tried applying the font attribute last after assigning all the other attributes first, and strangely the app crashes with `Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UICTFont textBlocks]: unrecognized selector sent to instance 0x8b1d1c0'`. – Joe Oct 12 '13 at 23:49
  • I suppose you are referring to the second alternative (assigning the text first, then modifying the properties), but in that example I didn't include the font customization, I will modify it to include the full sample. – jlhuertas Oct 13 '13 at 00:48
  • Both code samples yield the same result for me. So assigning the UITextView's properties doesn't seem to be necessary. Was there a reason you put them in? – Joe Oct 13 '13 at 01:34
  • Yeah, both samples should produce the same result. I just added the second way (customizing UITextView's properties) because you were asking why they weren't taken into account in your example. In my opinion, I find my first example clearer to understand (working only with attributed string attributes) instead of mixing attributes and `UITextView` properties. – jlhuertas Oct 13 '13 at 21:38
  • Oh ok. Are you not getting the font error I described in the first comment? – Joe Oct 13 '13 at 23:23
  • Thanks for the terrific example. – John Sep 20 '15 at 00:08