关于NSAttributedString
来源:互联网 发布:如何优化代码 编辑:程序博客网 时间:2024/05/22 16:39
最开始接触到AttributedString是在IB中对Label、Textfield中的Text选择有Plain和Attributed2种选择,当时也没有怎么留意。
直到后面做项目的时候,需要将几个Label的前面加上一个橙色的‘*’以显示为必填项,想过在各个Label前面都加多一个Label,Label就显示一个星号,但是程序员都有一颗懒惰的心,一次次在XIB文件中加Label实在太辛苦了,所以,就研究起了如何用Label的NSAttributedString来完成加星号的功能。
很多一开始看上去觉得很复杂的东西,其实研究下去了,也不过是那么回事。当初觉得NSAttributedString很复杂,所以只是简单的改变了String的颜色而已,不敢再深入探究下去,这几天产品上线,下一期需求还没出来,空闲几天,就把NSAttributedString又翻出来,写下总结,以防后用。
NSAttributedString有21个属性可以设置。所有需要设置的属性放进一个字典里。
- (void)viewDidLoad { [self p_attributeString:@"Test String \nabcdefghijklmnopqrstuvwxyz\n 0123456789"];}
以下在 - (void)p_attributeString:(NSString *)string
中实现
NSFontAttributeName
字体大小、字体Style
NSDictionary *attributeStringDic1 = @{NSFontAttributeName: [UIFont systemFontOfSize:28]};NSDictionary *attributeStringDic2 = @{NSFontAttributeName: [UIFont systemFontOfSize:18]}; NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:string]; [attributedString setAttributes:attributeStringDic1 range:NSMakeRange(0, [string length] - 37)]; [attributedString setAttributes:attributeStringDic2 range:NSMakeRange(12, [string length] - 12)]; self.attributeStringLabel.attributedText = attributedString;
NSParagraphStyleAttributeName
段落格式(首行缩进、行首行尾缩进等) 首行缩进100,其他行缩进50:
NSDictionary *attributeStringDic1 = @{NSFontAttributeName: [UIFont systemFontOfSize:28]};NSDictionary *attributeStringDic2 = @{NSFontAttributeName: [UIFont systemFontOfSize:18], NSParagraphStyleAttributeName: paragraphStyle };
NSForegroundColorAttributeName
字体颜色
NSDictionary *attributeStringDic1 = @{NSFontAttributeName: [UIFont systemFontOfSize:28], NSForegroundColorAttributeName: [UIColor redColor] };NSDictionary *attributeStringDic2 = @{NSFontAttributeName: [UIFont systemFontOfSize:18], NSForegroundColorAttributeName: [UIColor orangeColor] };
NSBackgroundColorAttributeName
背景颜色
NSDictionary *attributeStringDic1 = @{NSFontAttributeName: [UIFont systemFontOfSize:28], NSForegroundColorAttributeName: [UIColor redColor], NSBackgroundColorAttributeName: [UIColor blackColor] };NSDictionary *attributeStringDic2 = @{NSFontAttributeName: [UIFont systemFontOfSize:18], NSForegroundColorAttributeName: [UIColor orangeColor] };
NSLigatureAttributeName
(NSNumber containing integer, default 1: default ligatures, 0: no ligatures) 连写字符设置,默认为1 有连写字符。设置为0则为无连写字符。(1、0都是NSNumber)
NSDictionary *dic = @{NSLigatureAttributeName: @1, NSFontAttributeName: [UIFont fontWithName:"futura" size:38]};NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:string attributes:dic];
NSKernAttributeName
字符间隔(NSNumber containing floating point value)取值为 NSNumber 对象(整数),正值间距加宽,负值间距变窄
NSDictionary *attributeStringDic1 = @{NSFontAttributeName: [UIFont systemFontOfSize:28], NSForegroundColorAttributeName: [UIColor redColor], NSBackgroundColorAttributeName: [UIColor blackColor], NSKernAttributeName: @5, };NSDictionary *attributeStringDic2 = @{NSFontAttributeName: [UIFont systemFontOfSize:18], NSForegroundColorAttributeName: [UIColor orangeColor], NSKernAttributeName: @10 };NSDictionary *attributeStringDic3 = @{NSFontAttributeName: [UIFont fontWithName:@"Arial-ItalicMT" size:24], NSForegroundColorAttributeName: [UIColor purpleColor], NSKernAttributeName: @-5};
NSStrikethroughStyleAttributeName
删除线 (NSNumber 删除线的宽度)可以用枚举NSUnderlineStyle中的值,@(NSUnderlineStyelXXXXX)。// NSUnderlineStyleNone 不设置删除线 // NSUnderlineStyleSingle 设置删除线为细单实线 // NSUnderlineStyleThick 设置删除线为粗单实线 // NSUnderlineStyleDouble 设置删除线为细双实线也可以用NSNumber,其中1-7为单实线,9-15为双实线,数字越大线越粗。0、8、16、24都是没有线,之后重新按1-7、9-15这样循环下去。也就是17和1的显示结果是一样的。
NSDictionary *attributeStringDic1 = @{NSFontAttributeName: [UIFont systemFontOfSize:28], NSForegroundColorAttributeName: [UIColor redColor], NSBackgroundColorAttributeName: [UIColor blackColor], NSKernAttributeName: @5, NSStrikethroughStyleAttributeName: @(NSUnderlineStyleThick) };NSDictionary *attributeStringDic2 = @{NSFontAttributeName: [UIFont systemFontOfSize:18], NSForegroundColorAttributeName: [UIColor orangeColor], NSKernAttributeName: @10, NSStrikethroughStyleAttributeName: @(NSUnderlineStyleSingle), };NSDictionary *attributeStringDic3 = @{NSFontAttributeName: [UIFont fontWithName:@"Arial-ItalicMT" size:24], NSForegroundColorAttributeName: [UIColor purpleColor], NSKernAttributeName: @0, NSStrikethroughStyleAttributeName: @9 };
NSStrikethroughColorAttributeName
删除线颜色
NSDictionary *attributeStringDic1 = @{NSFontAttributeName: [UIFont systemFontOfSize:28], NSForegroundColorAttributeName: [UIColor redColor], NSBackgroundColorAttributeName: [UIColor blackColor], NSKernAttributeName: @5, NSStrikethroughStyleAttributeName: @17, NSStrikethroughColorAttributeName: [UIColor whiteColor] }; NSDictionary *attributeStringDic2 = @{NSFontAttributeName: [UIFont systemFontOfSize:18], NSForegroundColorAttributeName: [UIColor orangeColor], NSKernAttributeName: @10, NSStrikethroughStyleAttributeName: @(NSUnderlineStyleSingle), NSStrikethroughColorAttributeName: [UIColor blueColor] };NSDictionary *attributeStringDic3 = @{NSFontAttributeName: [UIFont fontWithName:@"Arial-ItalicMT" size:24], NSForegroundColorAttributeName: [UIColor purpleColor], NSKernAttributeName: @0, NSStrikethroughStyleAttributeName: @1, NSStrikethroughColorAttributeName: [UIColor brownColor] };
NSUnderlineStyleAttributeName
下划线 (NSNumber 下划线线的宽度)下划线和删除线是类似的
NSDictionary *attributeStringDic1 = @{NSFontAttributeName: [UIFont systemFontOfSize:28], NSForegroundColorAttributeName: [UIColor redColor], NSBackgroundColorAttributeName: [UIColor blackColor], NSKernAttributeName: @5, NSStrikethroughStyleAttributeName: @17, NSStrikethroughColorAttributeName: [UIColor whiteColor], NSUnderlineStyleAttributeName: @5 };NSDictionary *attributeStringDic2 = @{NSFontAttributeName: [UIFont systemFontOfSize:18], NSForegroundColorAttributeName: [UIColor orangeColor], NSKernAttributeName: @10, NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle) };NSDictionary *attributeStringDic3 = @{NSFontAttributeName: [UIFont fontWithName:@"Arial-ItalicMT" size:24], NSForegroundColorAttributeName: [UIColor purpleColor], NSKernAttributeName: @0, NSUnderlineStyleAttributeName: @(NSUnderlineStyleDouble) };
NSUnderlineColorAttributeName
下划线颜色
NSDictionary *attributeStringDic1 = @{NSFontAttributeName: [UIFont systemFontOfSize:28], NSForegroundColorAttributeName: [UIColor redColor], NSBackgroundColorAttributeName: [UIColor blackColor], NSKernAttributeName: @5, NSUnderlineStyleAttributeName: @5, NSUnderlineColorAttributeName: [UIColor blackColor] };NSDictionary *attributeStringDic2 = @{NSFontAttributeName: [UIFont systemFontOfSize:18], NSForegroundColorAttributeName: [UIColor orangeColor], NSKernAttributeName: @10, NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle), NSUnderlineColorAttributeName: [UIColor blueColor] };NSDictionary *attributeStringDic3 = @{NSFontAttributeName: [UIFont fontWithName:@"Arial-ItalicMT" size:24], NSForegroundColorAttributeName: [UIColor purpleColor], NSKernAttributeName: @0, NSUnderlineStyleAttributeName: @(NSUnderlineStyleDouble), NSUnderlineColorAttributeName: [UIColor colorWithRed:0.3 green:0.4 blue:0.5 alpha:1.0] };
NSStrokeWidthAttributeName
描边线宽度 (正数时不显示字体颜色,负数时显示字体颜色 需要跟NSStrokeColorAttributeName一起使用)负值填充效果,正值中空效果,数字绝对值越大线越粗
NSDictionary *attributeStringDic1 = @{NSFontAttributeName: [UIFont systemFontOfSize:28], NSForegroundColorAttributeName: [UIColor redColor], NSBackgroundColorAttributeName: [UIColor blackColor], NSKernAttributeName: @5, NSUnderlineStyleAttributeName: @5, NSUnderlineColorAttributeName: [UIColor whiteColor], NSStrokeWidthAttributeName: @1, }; NSDictionary *attributeStringDic2 = @{NSFontAttributeName: [UIFont systemFontOfSize:18], NSForegroundColorAttributeName: [UIColor orangeColor], NSKernAttributeName: @10, NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle), NSUnderlineColorAttributeName: [UIColor blueColor], NSStrokeWidthAttributeName: @6, }; NSDictionary *attributeStringDic3 = @{NSFontAttributeName: [UIFont fontWithName:@"Arial-ItalicMT" size:24], NSForegroundColorAttributeName: [UIColor purpleColor], NSKernAttributeName: @0, NSUnderlineStyleAttributeName: @(NSUnderlineStyleDouble), NSUnderlineColorAttributeName: [UIColor colorWithRed:0.3 green:0.4 blue:0.5 alpha:1.0], NSStrokeWidthAttributeName: @-10, };
NSStrokeColorAttributeName
描边线颜色不用NSStrokeColorAttributeName设置时,貌似是默认字体颜色代替,设置之后,就用设置的颜色描边了。
NSShadowAttributeName
阴影设置 (NSShadow)有背景颜色时,阴影显示在背景颜色的下面,被遮挡。
//阴影 3个属性:shadowOffset 偏移量,shadowColor 颜色, shadowBlurRadius 模糊半径 NSShadow *shadow1 = [[NSShadow alloc]init]; shadow1.shadowOffset = CGSizeMake(3, 3); NSShadow *shadow2 = [[NSShadow alloc]init]; shadow2.shadowOffset = CGSizeMake(-2, -4); shadow2.shadowColor = [UIColor redColor]; shadow2.shadowBlurRadius = 0.5; NSShadow *shadow3 = [[NSShadow alloc]init]; shadow3.shadowOffset = CGSizeMake(-2, 4); shadow3.shadowColor = [UIColor colorWithRed:0.6 green:0.1 blue:0.4 alpha:0.5]; shadow3.shadowBlurRadius = 5;NSDictionary *attributeStringDic1 = @{NSFontAttributeName: [UIFont systemFontOfSize:28], NSForegroundColorAttributeName: [UIColor redColor], NSBackgroundColorAttributeName: [UIColor blackColor], NSShadowAttributeName: shadow1, };NSDictionary *attributeStringDic2 = @{NSFontAttributeName: [UIFont systemFontOfSize:18], NSForegroundColorAttributeName: [UIColor orangeColor], NSShadowAttributeName: shadow2, };NSDictionary *attributeStringDic3 = @{NSFontAttributeName: [UIFont fontWithName:@"Arial-ItalicMT" size:48], NSForegroundColorAttributeName: [UIColor purpleColor], NSShadowAttributeName: shadow3, };
NSTextEffectAttributeName
设置文本特殊效果,取值为 NSString 对象,目前只有图版印刷效果可用NSTextEffectLetterpressStyle凸版印刷效果,7.0以上适用. 白色背景色时较难看出。如第三个显示就难看看出效果
NSDictionary *attributeStringDic1 = @{NSFontAttributeName: [UIFont systemFontOfSize:28], NSForegroundColorAttributeName: [UIColor redColor], NSBackgroundColorAttributeName: [UIColor blackColor], NSTextEffectAttributeName: NSTextEffectLetterpressStyle, };NSDictionary *attributeStringDic2 = @{NSFontAttributeName: [UIFont systemFontOfSize:18], NSForegroundColorAttributeName: [UIColor orangeColor], NSBackgroundColorAttributeName: [UIColor blueColor], NSTextEffectAttributeName: NSTextEffectLetterpressStyle, };NSDictionary *attributeStringDic3 = @{NSFontAttributeName: [UIFont fontWithName:@"Arial-ItalicMT" size:48], NSForegroundColorAttributeName: [UIColor purpleColor], NSTextEffectAttributeName: NSTextEffectLetterpressStyle, };
NSAttachmentAttributeName
设置文本附件,取值为NSTextAttachment对象,常用于文字图片混排
//设置文本附件,取值为NSTextAttachenment NSTextAttachment *textAttachment1 = [[NSTextAttachment alloc]init]; textAttachment1.image = [UIImage imageNamed:@"lufy.jpg"]; textAttachment1.bounds = CGRectMake(0, 0, 30, 30); NSAttributedString *attachmentStr = [NSAttributedString attributedStringWithAttachment:textAttachment1]; [attributedString insertAttributedString:attachmentStr atIndex:2]; self.attributeStringLabel.attributedText = attributedString;
NSLinkAttributeName
链接属性(点击跳转)UILabel和UITextField 中无法跳转,只能在TextView中可以跳转
在viewDidLoad中设置
self.textview.delegate = self;self.textview.editable = NO;self.textview.dataDetectorTypes = UIDataDetectorTypeLink;
在方法中写入:
NSMutableAttributedString *textViewString = [[NSMutableAttributedString alloc]initWithString:@"This is a link: www.baidu.com"];NSString *linkStr = @"www.baidu.com";NSDictionary *dic = @{NSLinkAttributeName: [NSURL URLWithString:@"https://www.baidu.com"]};[textViewString setAttributes:dic range:[[textViewString string] rangeOfString:linkStr]];[attributedString setAttributes:dic range:NSMakeRange(0, 10)];self.textview.attributedText = textViewString;
点击后跳转到safari
同时调用代理方法:
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange NS_AVAILABLE_IOS(7_0);
NSBaselineOffsetAttributeName
设置基线偏移值,取值为 NSNumber (float),正值上偏,负值下偏
NSDictionary *attributeStringDic1 = @{NSFontAttributeName: [UIFont systemFontOfSize:20], NSForegroundColorAttributeName: [UIColor redColor], NSBackgroundColorAttributeName: [UIColor blackColor], };NSDictionary *attributeStringDic2 = @{NSFontAttributeName: [UIFont systemFontOfSize:20], NSForegroundColorAttributeName: [UIColor orangeColor], NSBackgroundColorAttributeName: [UIColor blueColor], NSBaselineOffsetAttributeName: @10 };NSDictionary *attributeStringDic3 = @{NSFontAttributeName: [UIFont systemFontOfSize:20], NSForegroundColorAttributeName: [UIColor purpleColor], NSBackgroundColorAttributeName: [UIColor yellowColor], NSBaselineOffsetAttributeName: @-10, };
NSObliquenessAttributeName
设置字形倾斜度,取值为 NSNumber (float),正值右倾,负值左倾
NSDictionary *attributeStringDic1 = @{NSFontAttributeName: [UIFont systemFontOfSize:20], NSForegroundColorAttributeName: [UIColor redColor], NSBackgroundColorAttributeName: [UIColor blackColor], NSObliquenessAttributeName: @2, };NSDictionary *attributeStringDic2 = @{NSFontAttributeName: [UIFont systemFontOfSize:20], NSForegroundColorAttributeName: [UIColor orangeColor], NSBackgroundColorAttributeName: [UIColor blueColor], NSObliquenessAttributeName: @1, };NSDictionary *attributeStringDic3 = @{NSFontAttributeName: [UIFont systemFontOfSize:20], NSForegroundColorAttributeName: [UIColor purpleColor], NSBackgroundColorAttributeName: [UIColor yellowColor], NSObliquenessAttributeName: @(-.2) };
NSExpansionAttributeName
设置文本横向拉伸属性,取值为 NSNumber (float),正值横向拉伸文本,负值横向压缩文本
NSDictionary *attributeStringDic1 = @{NSFontAttributeName: [UIFont systemFontOfSize:20], NSForegroundColorAttributeName: [UIColor redColor], NSBackgroundColorAttributeName: [UIColor blackColor], NSExpansionAttributeName: @0.8, };NSDictionary *attributeStringDic2 = @{NSFontAttributeName: [UIFont systemFontOfSize:20], NSForegroundColorAttributeName: [UIColor orangeColor], NSBackgroundColorAttributeName: [UIColor blueColor], NSExpansionAttributeName: @0, };NSDictionary *attributeStringDic3 = @{NSFontAttributeName: [UIFont systemFontOfSize:20], NSForegroundColorAttributeName: [UIColor purpleColor], NSBackgroundColorAttributeName: [UIColor yellowColor], NSExpansionAttributeName: @-0.5, };
NSWritingDirectionAttributeName
设置文字书写方向,从左向右书写或者从右向左书写
NSDictionary *attributeStringDic1 = @{NSFontAttributeName: [UIFont systemFontOfSize:34], NSForegroundColorAttributeName: [UIColor redColor], NSBackgroundColorAttributeName: [UIColor blackColor], NSWritingDirectionAttributeName: @[@(NSWritingDirectionRightToLeft|NSWritingDirectionOverride)], };NSDictionary *attributeStringDic2 = @{NSFontAttributeName: [UIFont systemFontOfSize:34], NSForegroundColorAttributeName: [UIColor orangeColor], NSBackgroundColorAttributeName: [UIColor blueColor], NSWritingDirectionAttributeName: @[@(NSWritingDirectionRightToLeft|NSWritingDirectionEmbedding)], };NSDictionary *attributeStringDic3 = @{NSFontAttributeName: [UIFont systemFontOfSize:34], NSForegroundColorAttributeName: [UIColor purpleColor], NSBackgroundColorAttributeName: [UIColor yellowColor], NSWritingDirectionAttributeName: @[@(NSWritingDirectionLeftToRight|NSWritingDirectionEmbedding)], };
NSVerticalGlyphFormAttributeName
横竖排版(NSNumber) 0为横排 1为竖排Currently on iOS, it's always horizontal
0 0
- 关于NSAttributedString
- NSAttributedString
- NSAttributedString
- NSAttributedString
- NSAttributedString
- NSAttributedString
- NSAttributedString
- NSAttributedString
- NSAttributedString
- NSAttributedString
- NSAttributedString
- NSAttributedString
- NSAttributedString
- NSAttributedString
- NSAttributedString
- NSAttributedString
- NSAttributedString 详解
- NSAttributedString 详解
- Starting MySQL.Manager of pid-file quit without updating file.[FAILED]解决方法
- 线程中的等待与唤醒
- 苹果IOS开发者账号的区别,企业账号,个人账号,公司团队账号,教育账号
- OC习题 通讯录 (知识点: 字典 封装 字符串 数组 枚举)
- URLConnection和HTTPClient的比较
- 关于NSAttributedString
- 年轻程序员需要学习的五大经验
- 在vsphere client 给esxi上的虚拟机增加U盘识别
- linux curl 或者 命令行 的用法
- Ubuntu系统下,Python的使用和idle的安装使用
- iOS-计算两个日期之间的天数
- 编写一个属于自己的sax解析xml文档
- iOS 对应用程序启动时所有方法的调用顺序分析(转自董铂然博客园)
- 关于JVM个人浅谈(二)