转载:http://blog.csdn.net/jeffasd/article/details/52039786
一:UILabel
- 一个简单的例子
- 绘制不同颜色不同字体的一个AttributeString,如图
- 这里写图片描述
- 代码
- UILabel * Label = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 200, 60)];
- [self.view addSubview:Label];
- NSMutableAttributedString * firstPart = [[NSMutableAttributedString alloc] initWithString:@"Wenchen"];
- NSDictionary * firstAttributes = @{ NSFontAttributeName:[UIFont boldSystemFontOfSize:12],NSForegroundColorAttributeName:[UIColor redColor],};
- [firstPart setAttributes:firstAttributes range:NSMakeRange(0,firstPart.length)];
- NSMutableAttributedString * secondPart = [[NSMutableAttributedString alloc] initWithString:@" Huang"];
- NSDictionary * secondAttributes = @{NSFontAttributeName:[UIFont systemFontOfSize:10],NSForegroundColorAttributeName:[UIColor blueColor],};
- [secondPart setAttributes:secondAttributes range:NSMakeRange(0,secondPart.length)];
- [firstPart appendAttributedString:secondPart];
- Label.attributedText = firstPart;
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 先看看所有的Key
- NSFontAttributeName; //字体,value是UIFont对象
- NSParagraphStyleAttributeName;//绘图的风格(居中,换行模式,间距等诸多风格),value是NSParagraphStyle对象
- NSForegroundColorAttributeName;// 文字颜色,value是UIFont对象
- NSBackgroundColorAttributeName;// 背景色,value是UIFont
- NSLigatureAttributeName; // 字符连体,value是NSNumber
- NSKernAttributeName; // 字符间隔
- NSStrikethroughStyleAttributeName;//删除线,value是NSNumber
- NSUnderlineStyleAttributeName;//下划线,value是NSNumber
- NSStrokeColorAttributeName; //描绘边颜色,value是UIColor
- NSStrokeWidthAttributeName; //描边宽度,value是NSNumber
- NSShadowAttributeName; //阴影,value是NSShadow对象
- NSTextEffectAttributeName; //文字效果,value是NSString
- NSAttachmentAttributeName;//附属,value是NSTextAttachment 对象
- NSLinkAttributeName;//链接,value是NSURL or NSString
- NSBaselineOffsetAttributeName;//基础偏移量,value是NSNumber对象
- NSUnderlineColorAttributeName;//下划线颜色,value是UIColor对象
- NSStrikethroughColorAttributeName;//删除线颜色,value是UIColor
- NSObliquenessAttributeName; //字体倾斜
- NSExpansionAttributeName; //字体扁平化
- NSVerticalGlyphFormAttributeName;//垂直或者水平,value是 NSNumber,0表示水平,1垂直
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 字体,颜色,背景色
- 涉及到的属性
- - NSFontAttributeName
- - NSForegroundColorAttributeName
- - NSBackgroundColorAttributeName
- 效果
- 这里写图片描述
- 代码
- UILabel * Label = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 200, 60)];
- [self.view addSubview:Label];
- NSMutableAttributedString * mutableAttriStr = [[NSMutableAttributedString alloc] initWithString:@"Wenchen"];
- NSDictionary * attris = @{NSForegroundColorAttributeName:[UIColor whiteColor],NSBackgroundColorAttributeName:[UIColor grayColor],NSFontAttributeName:[UIFont boldSystemFontOfSize:14]};
- [mutableAttriStr setAttributes:attris range:NSMakeRange(0,mutableAttriStr.length)];
- Label.attributedText = mutableAttriStr;
- 1
- 2
- 3
- 4
- 5
- 6
- 下划线
- 涉及到的两个属性
- NSUnderlineStyleAttributeName
- NSUnderlineColorAttributeName
- 效果
- 这里写图片描述
- 代码
- UILabel * Label = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 200, 60)];
- [self.view addSubview:Label];
- NSMutableAttributedString * mutableAttriStr = [[NSMutableAttributedString alloc] initWithString:@"Wenchen"];
- NSDictionary * attris = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:12],NSForegroundColorAttributeName:[UIColor redColor], NSUnderlineStyleAttributeName:@(NSUnderlineStyleSingle), NSUnderlineColorAttributeName:[UIColor blueColor],};
- [mutableAttriStr setAttributes:attris range:NSMakeRange(0,mutableAttriStr.length)];
- Label.attributedText = mutableAttriStr;
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 描边
- 涉及到的两个属性
- NSStrokeColorAttributeName
- NSStrokeWidthAttributeName
- 效果
- 这里写图片描述
- 代码
- UILabel * Label = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 200, 60)];
- [self.view addSubview:Label];
- NSMutableAttributedString * mutableAttriStr = [[NSMutableAttributedString alloc] initWithString:@"Wenchen"];
- NSDictionary * attris = @{NSForegroundColorAttributeName:[UIColor whiteColor],NSStrokeColorAttributeName:[UIColor greenColor],NSStrokeWidthAttributeName:@(2)};
- [mutableAttriStr setAttributes:attris range:NSMakeRange(0,mutableAttriStr.length)];
- Label.attributedText = mutableAttriStr;
- 1
- 2
- 3
- 4
- 5
- 6
- 附属属性(例如图片)
- 涉及到的属性
- NSAttachmentAttributeName
- 效果
- 这里写图片描述
- 代码
- UILabel * Label = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 200, 60)];
- [self.view addSubview:Label];
- //创建Attachment Str
- NSTextAttachment * attach = [[NSTextAttachment alloc] init];
- attach.image = [UIImage imageNamed:@"memoAccess"];
- attach.bounds = CGRectMake(0, 0, 20, 20);
- NSAttributedString * imageStr = [NSAttributedString attributedStringWithAttachment:attach];
- //添加
- NSMutableAttributedString * mutableAttriStr = [[NSMutableAttributedString alloc] initWithString:@"Wenchen"];
- [mutableAttriStr appendAttributedString:imageStr];
- Label.attributedText = mutableAttriStr;
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 绘制风格
- 为什么要红字?因为这个属性真的很重要。
- 涉及到的属性
- NSMutableParagraphStyle
- 效果(看起来很奇怪,仅仅为了展示某些功能)
- 这里写图片描述
- 代码
- @interface TestView : UIView
- @end
- @implementation TestView
- -(instancetype)initWithFrame:(CGRect)frame{
- self = [super initWithFrame:frame];
- if (!self) {
- return nil;
- }
- self.opaque = NO;
- return self;
- }
- // An empty implementation adversely affects performance during animation.
- - (void)drawRect:(CGRect)rect {
- NSMutableAttributedString * attributeStr = [[NSMutableAttributedString alloc] initWithString:@"The anthor of this blog is wenchenhuang"];
- NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
- paragraphStyle.alignment = NSTextAlignmentRight;
- paragraphStyle.headIndent = 4.0;
- paragraphStyle.lineBreakMode = NSLineBreakByCharWrapping;
- paragraphStyle.lineSpacing = 2.0;
- NSDictionary * attributes = @{NSParagraphStyleAttributeName:paragraphStyle};
- [attributeStr setAttributes:attributes range:NSMakeRange(0, attributeStr.length)];
- [attributeStr drawInRect:self.bounds];
- }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- TestView *test = [[TestView alloc] initWithFrame:CGRectMake(100, 100,100, 60)];
- [self.view addSubview:test];
- 1
- 2
- 阴影
- 涉及到的属性
- NSShadowAttributeName
- 效果
- 这里写图片描述
- UILabel * Label = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 200, 60)];
- [self.view addSubview:Label];
- NSMutableAttributedString * mutableAttriStr = [[NSMutableAttributedString alloc] initWithString:@"Wenchen"];
- NSShadow * shadow = [[NSShadow alloc] init];
- shadow.shadowColor = [UIColor blueColor];
- shadow.shadowBlurRadius = 2.0;
- shadow.shadowOffset = CGSizeMake(1.0, 1.0);
- NSDictionary * attris = @{NSShadowAttributeName:shadow};
- [mutableAttriStr setAttributes:attris range:NSMakeRange(0,mutableAttriStr.length)];
- Label.attributedText = mutableAttriStr;
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 文字效果
- 相关属性
- NSTextEffectAttributeName
- 这里写图片描述
- 代码
- UILabel * Label = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 200, 60)];
- [self.view addSubview:Label];
- NSMutableAttributedString * mutableAttriStr = [[NSMutableAttributedString alloc] initWithString:@"Wenchen"];
- NSDictionary * attris = @{NSTextEffectAttributeName:NSTextEffectLetterpressStyle};
- [mutableAttriStr setAttributes:attris range:NSMakeRange(0,mutableAttriStr.length)];
- Label.attributedText = mutableAttriStr;
- 1
- 2
- 3
- 4
- 5
- 6
- 链接
- 相关属性
- NSLinkAttributeName
- 例子
- 点击打开链接
- @interface ViewController ()<UITextViewDelegate>
- @end
- @implementation ViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
- UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
- textView.scrollEnabled = NO;
- textView.editable = NO;
- textView.textContainer.lineFragmentPadding = 0;
- textView.textContainerInset = UIEdgeInsetsMake(0, 0, 0, 0);
- textView.delegate = self;
- [self.view addSubview:textView];
- NSMutableAttributedString * mutableAttriStr = [[NSMutableAttributedString alloc] initWithString:@"Wenchen"];
- NSDictionary * attris = @{NSLinkAttributeName:[NSURL URLWithString:@"http://www.baidu.com"]};
- [mutableAttriStr setAttributes:attris range:NSMakeRange(0,mutableAttriStr.length)];
- textView.attributedText = mutableAttriStr;
- // Do any additional setup after loading the view, typically from a nib.
- }
- - (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)url inRange:(NSRange)characterRange
- {
- return YES;
- }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 文字连体
- 涉及到的属性
- NSLigatureAttributeName
- 举例
- NSLigatureAttributeName 属性位@(0) 和@(1)
- 这里写图片描述
- 这里写图片描述
- 字符间隔
- 相关属性
- NSKernAttributeName
- 举例 字符间距拉大到4
- 这里写图片描述
- 对比下,默认的
- 这里写图片描述
- UILabel * Label = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 200, 120)];
- [self.view addSubview:Label];
- NSMutableAttributedString * mutableAttriStr = [[NSMutableAttributedString alloc] initWithString:@"Wenchen"];
- NSDictionary * attris = @{NSKernAttributeName:@(4),
- NSFontAttributeName:[UIFont systemFontOfSize:30]};
- [mutableAttriStr setAttributes:attris range:NSMakeRange(0,mutableAttriStr.length)];
- Label.attributedText = mutableAttriStr;
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- baseline基础偏移量
- 相关属性
- - NSBaselineOffsetAttributeName
- 效果
- 对比下偏移量为0 和20
- 代码
- NSDictionary * attris = @{NSBaselineOffsetAttributeName:@(0),
- NSFontAttributeName:[UIFont systemFontOfSize:30]};
- 1
- 2
- 字体倾斜
- 相关属性
- NSObliquenessAttributeName
- 效果
- 这里写图片描述
- 代码
- NSDictionary * attris = @{NSObliquenessAttributeName:@(0.5),
- NSFontAttributeName:[UIFont systemFontOfSize:30]};
- 1
- 2
- 字体扁平化
- 相关属性
- NSExpansionAttributeName
- 效果
- 这里写图片描述
- 代码
- NSDictionary * attris = @{NSExpansionAttributeName:@(1.0),
- NSFontAttributeName:[UIFont systemFontOfSize:30]};
转自:http://www.cnblogs.com/qingche/p/3574995.html?utm_source=tuicool&utm_medium=referral
二:UITextView
1.NSKernAttributeName: @10 调整字句 kerning 字句调整
2.NSFontAttributeName : [UIFont systemFontOfSize:_fontSize] 设置字体
3.NSForegroundColorAttributeName :[UIColor redColor] 设置文字颜色
4.NSParagraphStyleAttributeName : paragraph 设置段落样式
5.NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
paragraph.alignment = NSTextAlignmentCenter;
6.NSBackgroundColorAttributeName: [UIColor blackColor] 设置背景颜色
7.NSStrokeColorAttributeName设置文字描边颜色,需要和NSStrokeWidthAttributeName设置描边宽度,这样就能使文字空心.
NSStrokeWidthAttributeName这个属性所对应的值是一个 NSNumber 对象(小数)。该值改变描边宽度(相对于字体size 的百分比)。默认为 0,即不改变。正数只改变描边宽度。负数同时改变文字的描边和填充宽度。例如,对于常见的空心字,这个值通常为3.0。
同时设置了空心的两个属性,并且NSStrokeWidthAttributeName属性设置为整数,文字前景色就无效果了
效果:
效果:
8. NSStrikethroughStyleAttributeName 添加删除线, strikethrough删除线
效果:
9. NSUnderlineStyleAttributeName 添加下划线
效果:
10. NSShadowAttributeName 设置阴影,单独设置不好使,必须和其他属性搭配才好使。
和这三个任一个都好使,NSVerticalGlyphFormAttributeName,NSObliquenessAttributeName,NSExpansionAttributeName
11.NSVerticalGlyphFormAttributeName
该属性所对应的值是一个 NSNumber 对象(整数)。0 表示横排文本。1 表示竖排文本。在 iOS 中,总是使用横排文本,0 以外的值都未定义。
效果:
12. NSObliquenessAttributeName设置字体倾斜。Skew 斜
效果:
13. NSExpansionAttributeName 设置文本扁平化
效果:
清澈Saup