iOS NSAttributedString所有文本属性详解(多图)

本文是关于iOS中NSAttributedString的全面解析,涵盖字体、颜色、下划线、描边、图片附件、绘制风格、阴影、文字效果、链接、文字连体、字符间隔、基线偏移、字体倾斜和扁平化等多个属性的使用,通过实例代码展示各种效果。
摘要由CSDN通过智能技术生成

原创Blog,转载请注明出处
blog.csdn.net/hello_hwc
欢迎关注我的iOS SDK详解专栏
http://blog.csdn.net/column/details/huangwenchen-ios-sdk.html


前言:这篇文章算是整理吧,因为最近逛Stackoverflow的时候,发现自己这部分掌握的不是很好。


一个简单的例子

绘制不同颜色不同字体的一个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;

先看看所有的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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值