iOS中使用NSAttributedString、NSMutableAttributedString 实现富文本

时间:2023-02-01 00:25:13

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)];

 

//富文本 下划线的几种样式
typedef NS_ENUM(NSInteger, NSUnderlineStyle) {
    NSUnderlineStyleNone                                   
    NSUnderlineStyleSingle         //单下划线                        
    NSUnderlineStyleThick          //加粗效果下划线
    NSUnderlineStyleDouble        //双下划綫

    NSUnderlinePatternSolid 
    NSUnderlinePatternDot 
    NSUnderlinePatternDash 
    NSUnderlinePatternDashDot 
    NSUnderlinePatternDashDotDot 

    NSUnderlineByWord 
} 

 

 

 

 

看完了是不是 很有感觉呀!这样你就学会了给一行字设置不同的格式了!