_textView = [[UITextView alloc]initWithFrame:CGRectMake(20, 50, 200, 200)];
UILabel * myLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 300, 200, 200)];
myLabel.backgroundColor = [UIColor yellowColor];
_textView.backgroundColor = [UIColor yellowColor];
[self.view addSubview:myLabel];
[self.view addSubview:_textView];
// 定义一个可变属性字符串对象
NSMutableAttributedString * str = [[NSMutableAttributedString alloc]initWithString:@"缓缓飘落的枫叶像思念我点燃烛火温暖岁末的秋天激光掠过天边被风掠过想你的思念"];
// 设置字体大小 range是设置范围,下同
[str addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15] range:NSMakeRange(0, 5)];
// 设置字体颜色
[str addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(2, 5)];
// 设置下划线
[str addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleSingle) range:NSMakeRange(3, 7)];
// 设置字体样式
[str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Geeza Pro" size:25] range:NSMakeRange(5, 5)];
//NSLog(@"字体集合%@",[UIFont familyNames]);
// 删除线 常用于划掉原价
[str addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlinePatternSolid | NSUnderlineStyleSingle) range:NSMakeRange(8, 5)];
// 删除线的颜色(先设置删除线再设置颜色)
[str addAttribute:NSStrikethroughColorAttributeName value:[UIColor redColor] range:NSMakeRange(8, 5)];
// 设置空心字
[str addAttribute:NSStrokeWidthAttributeName value:@1 range:NSMakeRange(18, 5)];
// 插入图片
NSTextAttachment * att = [[NSTextAttachment alloc]init];
att.image = [UIImage imageNamed:@"2"];
NSAttributedString * attStr = [NSAttributedString attributedStringWithAttachment:att];
[str insertAttributedString:attStr atIndex:25];
// 添加链接
[str addAttribute:NSLinkAttributeName value:[NSURL URLWithString:@"http://www.baidu.com"] range:NSMakeRange(30, 6)];
// 创建字体段落 行间距 格式
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = 50;
paragraphStyle.firstLineHeadIndent = 30;// 设置为字体大小大两倍
[str addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, str.string.length)];
// 这句不能写前面,不然没效果
_textView.attributedText = [str copy];
_textView.editable = NO;
_textView.delegate = self;
myLabel.attributedText = str;
myLabel.numberOfLines = 0;
UITextView富文本
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
相关阅读更多精彩内容
- 由于最近遇到的项目需要用到富文本开发,主要的也就是这些,有些属性可以按着command键指着对应属性一枪进去看看其...
- 最近在做一个富文本编辑器功能,碰到一个问题:textview后面输入的文字他的attribute会和他前一个字符的...