iOS开发之UITextView使用富文本让文字居中显示

使用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;
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容