iOS常用技术-Label富文本

 1 //
 2 //  ViewController.m
 3 //  Label富文本
 4 //
 5 //  Created by 大欢 on 16/1/19.
 6 //  Copyright © 2016年 bjsxt. All rights reserved.
 7 //
 8 
 9 #import "ViewController.h"
10 
11 @interface ViewController ()
12 
13 @end
14 
15 @implementation ViewController
16 
17 - (void)viewDidLoad {
18     [super viewDidLoad];
19    
20     
21     NSMutableParagraphStyle * paragraph = [[NSMutableParagraphStyle alloc] init];
22     paragraph.lineSpacing = 10;
23     paragraph.paragraphSpacing = 50;
24     paragraph.firstLineHeadIndent = 50;
25     
26     NSString * exampleString = @"根据名人慈善赛公布的名单,Drake带领的加拿大明星队将包括:NBA两届得分王麦蒂、前湖人队三冠王成员里克·福克斯、网球运动员米洛斯·劳尼克以及演员兼歌手吴亦凡。
凯文·哈特领衔的美国明星队成员将包括:昌西·比卢普斯、“小虫”博格斯、著名主持人尼克·坎农、安东尼·安德森。";
27     
28     NSDictionary * dictA = @{NSFontAttributeName:[UIFont systemFontOfSize:20],
29                              NSForegroundColorAttributeName:[UIColor greenColor],
30                              NSBackgroundColorAttributeName:[UIColor grayColor],
31                              NSParagraphStyleAttributeName:paragraph
32 //                             NSObliquenessAttributeName:@0.5 //斜体
33 //                             NSStrokeColorAttributeName:[UIColor whiteColor],
34 //                             NSStrokeWidthAttributeName:@2,//描边
35 //                             NSKernAttributeName:@20,//字间距
36 //                             NSStrikethroughStyleAttributeName:@2,//删除线
37 //                             NSUnderlineStyleAttributeName:@1,//下划线
38                              };
39     
40     NSAttributedString * attribute = [[NSAttributedString alloc] initWithString:exampleString attributes:dictA];
41     
42     UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(20, 50, CGRectGetWidth(self.view.bounds) - 40, 500)];
43     label.attributedText = attribute;
44     label.backgroundColor = [UIColor blueColor];
45     label.numberOfLines = 0;
46     [self.view addSubview:label]; 
47 }
48 @end


/****************************************************************/

/****************************************************************/

  1 //
  2 //  ViewController.m
  3 //  Label富文本2
  4 //
  5 //  Created by ma c on 16/1/19.
  6 //  Copyright © 2016年 bjsxt. All rights reserved.
  7 //
  8 
  9 #import "ViewController.h"
 10 
 11 @interface ViewController ()
 12 
 13 @end
 14 
 15 @implementation ViewController
 16 
 17 - (void)viewDidLoad {
 18     [super viewDidLoad];
 19 //    [self example1];
 20 //    [self example2];
 21     [self example4];
 22 }
 23 
 24 - (void)didReceiveMemoryWarning {
 25     [super didReceiveMemoryWarning];
 26     // Dispose of any resources that can be recreated.
 27 }
 28 
 29 - (void)example2 {
 30     NSString * string = @"@大欢,出来玩啊!";
 31     
 32     NSMutableAttributedString * attributeString = [[NSMutableAttributedString alloc]initWithString:string];
 33     
 34     NSRange range1 = {0, 3};
 35     
 36     NSDictionary *dict1 = @{
 37                            NSForegroundColorAttributeName:[UIColor blueColor],
 38                            NSFontAttributeName:[UIFont systemFontOfSize:25],
 39                            };
 40     
 41     [attributeString addAttributes:dict1 range:range1];
 42     
 43     NSDictionary *dict2 = @{
 44                             NSForegroundColorAttributeName:[UIColor greenColor],
 45                             NSFontAttributeName:[UIFont systemFontOfSize:22],
 46                             };
 47     
 48     NSAttributedString * string2 = [[NSAttributedString alloc] initWithString:@"好哒!" attributes:dict2];
 49     
 50     [attributeString appendAttributedString:string2];
 51     UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(10, 100, CGRectGetWidth(self.view.frame)-20, 30)];
 52     
 53     label.attributedText = attributeString;
 54     label.textAlignment = NSTextAlignmentCenter;
 55     label.backgroundColor = [UIColor yellowColor];
 56     [self.view addSubview:label];
 57 }
 58 /*
 59  使用方法:
 60  
 61  为某一范围内文字设置多个属性
 62  - (void)setAttributes:(NSDictionary *)attrs range:(NSRange)range;
 63  
 64  为某一范围内文字添加某个属性
 65  - (void)addAttribute:(NSString *)name value:(id)value range:(NSRange)range;
 66  
 67  为某一范围内文字添加多个属性
 68  - (void)addAttributes:(NSDictionary *)attrs range:(NSRange)range;
 69  
 70  移除某范围内的某个属性
 71  - (void)removeAttribute:(NSString *)name range:(NSRange)range;
 72  
 73  常见的属性及说明
 74  NSFontAttributeName  字体
 75  
 76  NSParagraphStyleAttributeName       段落格式
 77  
 78  NSForegroundColorAttributeName     字体颜色
 79  
 80  NSBackgroundColorAttributeName    背景颜色
 81  
 82  NSStrikethroughStyleAttributeName  删除线格式
 83  
 84  NSUnderlineStyleAttributeName       下划线格式
 85  
 86  NSStrokeColorAttributeName            删除线颜色
 87  
 88  NSStrokeWidthAttributeName           删除线宽度
 89  
 90  NSShadowAttributeName                 阴影
 91  */
 92 - (void)example1 {
 93     
 94     NSString * exampleString = @"乔布斯---apple";
 95     
 96     NSMutableAttributedString * mutableAttribute = [[NSMutableAttributedString alloc]initWithString:exampleString];
 97     
 98     NSRange range1 = {0, 3};
 99     NSRange range2 = {6, 5};
100     
101     [mutableAttribute addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:50] range:range1];
102     
103     NSDictionary * dict = @{
104                             NSForegroundColorAttributeName:[UIColor magentaColor],
105                             NSBackgroundColorAttributeName:[UIColor cyanColor]
106                             };
107     [mutableAttribute addAttributes:dict range:range2];
108     
109     UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(10, 30, self.view.frame.size.width-20, 60)];
110     
111     label.attributedText = mutableAttribute;
112     label.backgroundColor = [UIColor grayColor];
113     label.textAlignment = NSTextAlignmentCenter;
114     [self.view addSubview:label];
115 }
116 
117 - (void)example3 {
118     NSString * string = @"一二三四五";
119     NSMutableAttributedString * mutableAttibute = [[NSMutableAttributedString alloc]initWithString:string];
120     NSRange range1 = NSMakeRange(0, 3);
121     [mutableAttibute addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:23] range:range1];
122     UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(10, 20, CGRectGetWidth(self.view.frame), 50)];
123     label.textColor = [UIColor redColor];
124     label.textAlignment = NSTextAlignmentCenter;
125     label.attributedText = mutableAttibute;
126     [self.view addSubview:label];
127 }
128 
129 - (void)example4 {
130     NSString * string = @"还记得你说家是唯一的城堡随着稻香河流继续奔跑微微笑 小时候的梦我知道不要哭让萤火虫带著你逃跑乡间的歌谣永远的依靠回家吧 回到最初的美好";
131     NSMutableParagraphStyle * paragraph = [[NSMutableParagraphStyle alloc]init];
132     paragraph.lineSpacing = 4;
133     paragraph.paragraphSpacing = 7;
134     paragraph.firstLineHeadIndent = 25;
135     
136     NSDictionary * dict = @{
137                             NSFontAttributeName:[UIFont systemFontOfSize:20],
138                             NSParagraphStyleAttributeName:paragraph,
139                             };
140     
141     NSMutableAttributedString * mutableAttribute = [[NSMutableAttributedString alloc]initWithString:string attributes:dict];
142     
143     UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(10, 20, CGRectGetWidth(self.view.frame)-20, 0)];
144     label.attributedText = mutableAttribute;
145     label.numberOfLines = 0;
146     [label sizeToFit];
147     [self.view addSubview:label];
148 }
149 
150 @end





原文地址:https://www.cnblogs.com/MrWuYindi/p/5146572.html