How can I set the color and alignment of attributed text in a UITextView in iOS 7?
Asked Answered
S

1

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];
Selmore answered 12/10, 2013 at 22:33 Comment(0)
B
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];
Braun answered 12/10, 2013 at 23:21 Comment(5)
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'.Selmore
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.Braun
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?Selmore
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.Braun
Oh ok. Are you not getting the font error I described in the first comment?Selmore

© 2022 - 2024 — McMap. All rights reserved.