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

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


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 alignmentNSMutableParagraphStyle *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 fontNSMutableAttributedString *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 propertiestitleView.attributedText = title;titleView.textAlignment = NSTextAlignmentCenter;titleView.textColor = [UIColor whiteColor];