原创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