使用UITextView自带属性textAlignment无法在使用富文本时生效,需要使用段落来做居中。
textView.textAlignment = NSTextAlignmentCenter; // 不生效!!!!
生效代码:
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(100, 100, 200, 30)];
[self addSubview: textView];
NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:@"* 注册后需返回本页刷新数据,信息可能有延迟,请耐心等待!" attributes:@{NSForegroundColorAttributeName : kColor999999, NSFontAttributeName : PingFangRegularFont(10)}];
[attr addAttribute:NSForegroundColorAttributeName value:kColor007AFF range:NSMakeRange(10, 4)];
// 让文字居中
NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
paragraph.alignment = NSTextAlignmentCenter;
[attr addAttribute:NSParagraphStyleAttributeName
value:paragraph
range:NSMakeRange(0, [attr length])];
textView .attributedText = attr;
网友评论