iOS中使用NSAttributedString、NSMutableAttributedString 实现富文本
NSMutableAttributedString 实现富文本
1.实例化方法和使用方法
实例化方法:
使用字符串初始化
- (id)initWithString:(NSString *)str;
例:
NSMutableAttributedString *AttributedStr = [[NSMutableAttributedStringalloc]initWithString:@"今天天气不错呀"];
使用字符串和富文本格式的初始化
- (id)initWithString:(NSString *)str attributes:(NSDictionary *)attrs;
字典中存放一些属性名和属性值,如:
NSDictionary *attributeDict = [NSDictionarydictionaryWithObjectsAndKeys:
[UIFontsystemFontOfSize:15.0],NSFontAttributeName,
[UIColor redColor],NSForegroundColorAttributeName,
NSUnderlineStyleAttributeName,NSUnderlineStyleSingle,nil];
NSMutableAttributedString *AttributedStr = [[NSMutableAttributedStringalloc]initWithString:@"今天天气不错呀" attributes:attributeDict];
使用NSAttributedString初始化,跟NSMutableString,NSString类似
- (id)initWithAttributedString:(NSAttributedString *)attester;
1 // initWithString: 2 NSAttributedString *attributedString_str = [[NSAttributedString alloc] initWithString:@"attributedString"]; 3 NSLog(@"%@", attributedString_str); 4 // textView.attributedText = attributedString_str; 5 6 7 // initWithAttributedString: 8 NSAttributedString *attributedString_atts = [[NSAttributedString alloc] initWithAttributedString:attributedString_str]; 9 NSLog(@"%@", attributedString_atts); 10 // textView.attributedText = attributedString_atts; 11 12 // initWithString:attributes: 13 UIColor *backgroundColor = [UIColor blackColor]; 14 NSNumber *baseLineOffset = [NSNumber numberWithFloat:20.0]; 15 UIColor *foregroundColor = [UIColor whiteColor]; 16 NSNumber *kern = [NSNumber numberWithFloat:5.0]; 17 NSNumber *ligature = [NSNumber numberWithFloat:3.0]; 18 NSURL *linkURL = [NSURL URLWithString:@"http://www.baidu.com"]; 19 NSNumber *underline = [NSNumber numberWithInt:NSUnderlineStyleSingle]; 20 NSDictionary *attrsDic = @{NSForegroundColorAttributeName: foregroundColor, 21 NSBackgroundColorAttributeName: backgroundColor, 22 NSBaselineOffsetAttributeName: baseLineOffset, 23 NSKernAttributeName: kern, 24 NSLigatureAttributeName: ligature, 25 NSLinkAttributeName: linkURL, 26 NSUnderlineStyleAttributeName: underline 27 }; 28 NSAttributedString *attributedString_str_atts = [[NSAttributedString alloc] initWithString:@"http://www.baidu.com" attributes:attrsDic]; 29 NSLog(@"%@", attributedString_str_atts); 30 // textView.attributedText = attributedString_str_atts; 31 32 33 // initWithFileURL:options:documentAttributes:error: 34 NSURL *fileURL = nil; 35 fileURL = [[NSBundle mainBundle] URLForResource:@"Dynamic Coloring" withExtension:@"rtf"]; 36 NSAttributedString *attributedString_fileURL = [[NSAttributedString alloc] initWithFileURL:fileURL options:@{} documentAttributes:nil error:nil]; 37 NSLog(@"%@", attributedString_fileURL); 38 // textView.attributedText = attributedString_fileURL; 39 40 41 // initWithData:options:documentAttributes:error: 42 fileURL = nil; 43 fileURL = [[NSBundle mainBundle] URLForResource:@"View Layout" withExtension:@"rtf"]; 44 NSData *data = [[NSData alloc] initWithContentsOfURL:fileURL]; 45 NSAttributedString *attributedString_data = [[NSAttributedString alloc] initWithData:data options:@{} documentAttributes:nil error:nil]; 46 NSLog(@"%@", attributedString_data); 47 // textView.attributedText = attributedString_data; 48 49 50 // initWithAttributedString: 51 NSMutableAttributedString *mutableAttributedString_attrs = [[NSMutableAttributedString alloc] initWithAttributedString:attributedString_fileURL];
1 //使用方法: 2 3 //为某一范围内文字设置多个属性 4 5 - (void)setAttributes:(NSDictionary *)attrs range:(NSRange)range; 6 7 //为某一范围内文字添加某个属性 8 9 - (void)addAttribute:(NSString *)name value:(id)value range:(NSRange)range; 10 11 //为某一范围内文字添加多个属性 12 13 - (void)addAttributes:(NSDictionary *)attrs range:(NSRange)range; 14 15 //移除某范围内的某个属性 16 17 - (void)removeAttribute:(NSString *)name range:(NSRange)range;
2. 常见的属性及说明
//字体 NSFontAttributeName //段落格式 NSParagraphStyleAttributeName //字体颜色 NSForegroundColorAttributeName //背景颜色 NSBackgroundColorAttributeName //删除线格式 NSStrikethroughStyleAttributeName //下划线格式 NSUnderlineStyleAttributeName 删除线颜色 NSStrokeColorAttributeName 删除线宽度 NSStrokeWidthAttributeName 阴影 NSShadowAttributeName
3.应用实例
1 //应用实例 2 3 NSString *label_text1 = @"大家好我是一个Label,教大家实现富文本"; 4 5 NSMutableAttributedString *attributedString1 = [[NSMutableAttributedString alloc]initWithString:label_text1]; 6 7 8 9 [attributedString1 addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20] range:NSMakeRange(0, label_text1.length)]; 10 11 [attributedString1 addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(12, 2)]; 12 13 [attributedString1 addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(17, 2)]; 14 15 //最好设置一下行高(每行字之间的距离(行间距)),不设的话默认是0 16 17 NSMutableParagraphStyle *sty = [[NSMutableParagraphStyle alloc] init]; 18 19 sty.lineSpacing = 5; 20 21 [attributedString1 addAttribute:NSParagraphStyleAttributeName value:sty range:NSMakeRange(0, label_text1.length)]; 22 23 24 25 UILabel *ybLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(10, 100, self.view.bounds.size.width - 20, 60)]; 26 27 ybLabel1.backgroundColor = [UIColor yellowColor]; 28 29 ybLabel1.numberOfLines = 2; 30 31 ybLabel1.attributedText = attributedString1;
1 应用实例 2 //下划线格式 3 NSUnderlineStyleAttributeName 4 NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc]initWithString:_forgetPasswordLabel.text]; 5 6 [attributedStr addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:NSMakeRange(0, _forgetPasswordLabel.text.length)];
7 8 //删除线格式 9 NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc]initWithString:_marketPriceLabel.text]; 10 11 [attributedStr addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlinePatternSolid | NSUnderlineStyleSingle) range:NSMakeRange(0, _marketPriceLabel.text.length)]; 12 13 //删除线颜色 14 [attributedStr addAttribute:NSStrokeColorAttributeName value:[UIColor darkGrayColor] range:NSMakeRange(0, _marketPriceLabel.text.length)];
//设置行间距
NSMutableParagraphStyle *sty = [[NSMutableParagraphStyle alloc] init];
sty.lineSpacing = 5;
[attributedString1 addAttribute:NSParagraphStyleAttributeName value:sty range:NSMakeRange(0, label_text1.length)];
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | //富文本 下划线的几种样式 typedef NS_ENUM ( NSInteger , NSUnderlineStyle ) { NSUnderlineStyleNone NSUnderlineStyleSingle //单下划线 NSUnderlineStyleThick //加粗效果下划线 NSUnderlineStyleDouble //双下划綫 NSUnderlinePatternSolid NSUnderlinePatternDot NSUnderlinePatternDash NSUnderlinePatternDashDot NSUnderlinePatternDashDotDot NSUnderlineByWord } |
看完了是不是 很有感觉呀!这样你就学会了给一行字设置不同的格式了!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· 使用C#创建一个MCP客户端
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· Windows编程----内核对象竟然如此简单?