iOS6之后 NSAttributedString 的福利

@在iOS6之前需要使用NSMutableAttributedString时都需要导入:CoreText.framework框架的,但在iOS6 之后就不在需要了.

[objc]  view plain copy
  1. - (void)testOfNSMutableAttributedStringAndNSAttributedString  
  2. {  
  3.     /** 
  4.      *  - (void)addAttribute:(NSString *)name value:(id)value range:(NSRange)range; 
  5.      *  主要方法 
  6.      *  name   属性名 
  7.      *  value  属性对应效果的值 
  8.      *  range  效果所映射的范围 
  9.      */  
  10.       
  11.     #pragma mark  测试数据0  
  12.     NSString *testString = @"NSMutableAttributed---0";  
  13.     UILabel *testLabel = [[UILabel alloc] initWithFrame:CGRectMake(6010020040)];  
  14.     NSMutableAttributedString * testAttriString = [[NSMutableAttributedString alloc] initWithString:testString];  
  15.     // 添加删除线  
  16.     [testAttriString addAttribute:NSStrikethroughStyleAttributeName value:[NSNumber numberWithInt:NSUnderlineStyleSingle] range:NSMakeRange(0, testAttriString.length)];  
  17.     // 添加下划线  
  18.     [testAttriString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInt:NSUnderlineStyleSingle] range:NSMakeRange(0, testAttriString.length)];  
  19.     // 设置文本的字体以及大小  
  20.     [testAttriString addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Helvetica-Bold" size:15] range:NSMakeRange(0, testAttriString.length)];  
  21.     // 设置笔画的粗细  
  22.     [testAttriString addAttribute:NSStrokeWidthAttributeName value:[NSNumber numberWithInt:NSUnderlineStyleDouble] range:NSMakeRange(0, testAttriString.length)];  
  23.     // label的背景颜色  
  24.     [testAttriString addAttribute:NSBackgroundColorAttributeName value:[UIColor yellowColor] range:NSMakeRange(0, testAttriString.length)];  
  25.     // 目前没测出什么效果.....  
  26.     [testAttriString addAttribute:NSVerticalGlyphFormAttributeName value:[NSNumber numberWithInt:1] range:NSMakeRange(0, testAttriString.length)];  
  27.     // label上文本颜色(也会影响删除线和下划线的颜色)  
  28.     [testAttriString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, testAttriString.length)];  
  29.     testLabel.attributedText = testAttriString;  
  30.       
  31.     #pragma mark 测试数据1  
  32.     NSString *testString1 = @"NSMutableAttributed---1";  
  33.     UILabel *testLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(6020020040)];  
  34.     NSMutableAttributedString * testAttriString1 = [[NSMutableAttributedString alloc] initWithString:testString1];  
  35.     // 实现文本内容颜色和下划线,删除线的颜色不一样  
  36.     // NSStrokeColorAttributeName 单独设置没有效果  
  37.     // 必须与NSStrokeWidthAttributeName一起设置  
  38.     [testAttriString1 addAttribute:NSStrikethroughStyleAttributeName value:[NSNumber numberWithInt:NSUnderlineStyleSingle] range:NSMakeRange(0, testAttriString1.length)];  
  39.     [testAttriString1 addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, testAttriString1.length)];  
  40.     [testAttriString1 addAttribute:NSStrokeColorAttributeName value:[UIColor cyanColor] range:NSMakeRange(0, testAttriString1.length)];  
  41.     [testAttriString1 addAttribute:NSStrokeWidthAttributeName value:[NSNumber numberWithInt:NSUnderlineStyleDouble] range:NSMakeRange(0, testAttriString1.length)];  
  42.     testLabel1.attributedText = testAttriString1;  
  43.       
  44.       
  45.     #pragma mark 测试数据2  
  46.     NSString *testString2 = @"NSMutableAttributed---2";  
  47.     UILabel *testLabel2= [[UILabel alloc] initWithFrame:CGRectMake(6030020040)];  
  48.     NSMutableAttributedString * testAttriString2 = [[NSMutableAttributedString alloc] initWithString:testString2];  
  49.     // 笔画的阴影效果  
  50.     NSShadow *shadow = [[NSShadow alloc] init];  
  51.     [shadow setShadowColor:[UIColor colorWithRed:0.053 green:0.088 blue:0.205 alpha:1.000]];  
  52.     [shadow setShadowBlurRadius:4.0];  
  53.     [shadow setShadowOffset:CGSizeMake(22)];  
  54.     [testAttriString2 addAttribute:NSShadowAttributeName value:shadow range:NSMakeRange(0, [testAttriString2 length])];  
  55.     testLabel2.backgroundColor = [UIColor clearColor];  
  56.     testLabel2.attributedText = testAttriString2;  
  57.       
  58.     [self.view addSubview:testLabel];  
  59.     [self.view addSubview:testLabel1];  
  60.     [self.view addSubview:testLabel2];  
  61. }  

[objc]  view plain copy
  1. 使用AttributedString的方式通常有两种:  
  2.    
  3. 方式一:  
  4.    
  5.     首先初始化一个NSMutableAttributedString,然后向里面添加文字样式,最后将它赋给控件的AttributedText,该方法适合于文本较少而又需要分段精细控制的情况。  
  6.    
  7.    
  8. NSString *originStr = @"Hello,中秋节!";  
  9.    
  10. //方式一  
  11.    
  12. //创建 NSMutableAttributedString  
  13. NSMutableAttributedString *attributedStr01 = [[NSMutableAttributedString alloc] initWithString: originStr];  
  14.    
  15. //添加属性  
  16.    
  17. //给所有字符设置字体为Zapfino,字体高度为15像素  
  18. [attributedStr01 addAttribute: NSFontAttributeName value: [UIFont fontWithName@"Zapfino" size: 15]  
  19.                                                    range: NSMakeRange(0, originStr.length)];  
  20. //分段控制,最开始4个字符颜色设置成蓝色  
  21. [attributedStr01 addAttribute: NSForegroundColorAttributeName value: [UIColor blueColor] range: NSMakeRange(04)];  
  22. //分段控制,第5个字符开始的3个字符,即第5、6、7字符设置为红色  
  23. [attributedStr01 addAttribute: NSForegroundColorAttributeName value: [UIColor redColor] range: NSMakeRange(43)];  
  24.    
  25. //赋值给显示控件label01的 attributedText  
  26. _label01.attributedText = attributedStr01;  
  27.    
  28.    
  29.    
  30.    
  31. 方式二:  
  32.    
  33.       首先创建属性字典,初始化各种属性,然后和需要控制的文本一起创建并赋值给控件的AttributedText,该方法适合于需要控制的文本较多整体控制的情况,通常是从文件中读取的大段文本控制。  
  34.    
  35.     
  36. //方式二  
  37.    
  38. //创建属性字典  
  39. NSDictionary *attrDict = @{ NSFontAttributeName: [UIFont fontWithName@"Zapfino" size: 15],  
  40.                             NSForegroundColorAttributeName: [UIColor blueColor] };  
  41.    
  42. //创建 NSAttributedString 并赋值  
  43. _label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict];  
  44.    
  45.    
  46.      
  47.    
  48.        通过对比两个例子可以看出,方式一比较容易处理复杂的格式,但是属性设置比较繁多复杂,而方式二的属性设置比较简单明了,却不善于处理复杂多样的格式控制,但是不善于并不等于不能,可以通过属性字符串分段的方式来达到方式一的效果,如下:  
  49.    
  50. //方式二的分段处理  
  51. //第一段  
  52. NSDictionary *attrDict1 = @{ NSFontAttributeName: [UIFont fontWithName@"Zapfino" size: 15],  
  53.                              NSForegroundColorAttributeName: [UIColor blueColor] };  
  54. NSAttributedString *attrStr1 = [[NSAttributedString alloc] initWithString: [originStr substringWithRange: NSMakeRange(04)] attributes: attrDict1];  
  55.    
  56. //第二段  
  57. NSDictionary *attrDict2 = @{ NSFontAttributeName: [UIFont fontWithName@"Zapfino" size: 15],  
  58.                              NSForegroundColorAttributeName: [UIColor redColor] };  
  59. NSAttributedString *attrStr2 = [[NSAttributedString alloc] initWithString: [originStr substringWithRange: NSMakeRange(43)] attributes: attrDict2];  
  60.    
  61. //第三段  
  62. NSDictionary *attrDict3 = @{ NSFontAttributeName: [UIFont fontWithName@"Zapfino" size: 15],  
  63.                              NSForegroundColorAttributeName: [UIColor blackColor] };  
  64. NSAttributedString *attrStr3 = [[NSAttributedString alloc] initWithString: [originStr substringWithRange:  
  65.                                                                             NSMakeRange(7, originStr.length - 4 - 3)] attributes: attrDict3];  
  66. //合并  
  67. NSMutableAttributedString *attributedStr03 = [[NSMutableAttributedString alloc] initWithAttributedString: attrStr1];  
  68. [attributedStr03 appendAttributedString: attrStr2];  
  69. [attributedStr03 appendAttributedString: attrStr3];  
  70.    
  71. _label03.attributedText = attributedStr03;  
  72.    
  73.    
  74.    
  75.    
  76. @AttributedString究竟可以设置哪些属性,具体来说,有以下21个:   
  77. // NSFontAttributeName                设置字体属性,默认值:字体:Helvetica(Neue) 字号:12  
  78. // NSForegroundColorAttributeNam      设置字体颜色,取值为 UIColor对象,默认值为黑色  
  79. // NSBackgroundColorAttributeName     设置字体所在区域背景颜色,取值为 UIColor对象,默认值为nil, 透明色  
  80. // NSLigatureAttributeName            设置连体属性,取值为NSNumber 对象(整数),0 表示没有连体字符,1 表示使用默认的连体字符  
  81. // NSKernAttributeName                设定字符间距,取值为 NSNumber 对象(整数),正值间距加宽,负值间距变窄  
  82. // NSStrikethroughStyleAttributeName  设置删除线,取值为 NSNumber 对象(整数)  
  83. // NSStrikethroughColorAttributeName  设置删除线颜色,取值为 UIColor 对象,默认值为黑色  
  84. // NSUnderlineStyleAttributeName      设置下划线,取值为 NSNumber 对象(整数),枚举常量 NSUnderlineStyle中的值,与删除线类似  
  85. // NSUnderlineColorAttributeName      设置下划线颜色,取值为 UIColor 对象,默认值为黑色  
  86. // NSStrokeWidthAttributeName         设置笔画宽度,取值为 NSNumber 对象(整数),负值填充效果,正值中空效果  
  87. // NSStrokeColorAttributeName         填充部分颜色,不是字体颜色,取值为 UIColor 对象  
  88. // NSShadowAttributeName              设置阴影属性,取值为 NSShadow 对象  
  89. // NSTextEffectAttributeName          设置文本特殊效果,取值为 NSString 对象,目前只有图版印刷效果可用:  
  90. // NSBaselineOffsetAttributeName      设置基线偏移值,取值为 NSNumber (float),正值上偏,负值下偏  
  91. // NSObliquenessAttributeName         设置字形倾斜度,取值为 NSNumber (float),正值右倾,负值左倾  
  92. // NSExpansionAttributeName           设置文本横向拉伸属性,取值为 NSNumber (float),正值横向拉伸文本,负值横向压缩文本  
  93. // NSWritingDirectionAttributeName    设置文字书写方向,从左向右书写或者从右向左书写  
  94. // NSVerticalGlyphFormAttributeName   设置文字排版方向,取值为 NSNumber 对象(整数),0 表示横排文本,1 表示竖排文本  
  95. // NSLinkAttributeName                设置链接属性,点击后调用浏览器打开指定URL地址  
  96. // NSAttachmentAttributeName          设置文本附件,取值为NSTextAttachment对象,常用于文字图片混排  
  97. // NSParagraphStyleAttributeName      设置文本段落排版格式,取值为 NSParagraphStyle 对象   
  98.    
  99.    
  100. 下面就一一举例说明:  
  101.    
  102. 1. NSFontAttributeName  
  103.    
  104.    
  105. //NSForegroundColorAttributeName 设置字体颜色,取值为 UIColor,默认为黑色  
  106.    
  107. NSDictionary *attrDict1 = @{ NSForegroundColorAttributeName: [UIColor redColor] };  
  108. NSDictionary *attrDict2 = @{ NSForegroundColorAttributeName: [UIColor blueColor] };  
  109. NSDictionary *attrDict3 = @{ NSForegroundColorAttributeName: [UIColor orangeColor] };  
  110.    
  111. _label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];  
  112. _label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];  
  113. _label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3];  
  114.    
  115.    
  116.    
  117. 注意:  
  118.    
  119.        NSForegroundColorAttributeName设置的颜色与UILabel的textColor属性设置的颜色在地位上是相等的,谁最后赋值,最终显示的就是谁的颜色。  
  120.    
  121.    
  122.    
  123. 2. NSBackgroundColorAttributeName  
  124.    
  125.    
  126. //NSForegroundColorAttributeName 设置字体颜色,取值为 UIColor,默认为黑色  
  127.    
  128. NSDictionary *attrDict1 = @{ NSForegroundColorAttributeName: [UIColor redColor] };  
  129. NSDictionary *attrDict2 = @{ NSForegroundColorAttributeName: [UIColor blueColor] };  
  130. NSDictionary *attrDict3 = @{ NSForegroundColorAttributeName: [UIColor orangeColor] };  
  131.    
  132. _label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];  
  133. _label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];  
  134. _label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3];  
  135.    
  136.    
  137. //NSBackgroundColorAttributeName 设置字体所在区域背景的颜色,取值为UIColor,默认值为nil  
  138.    
  139. NSDictionary *attrDict4 = @{ NSBackgroundColorAttributeName: [UIColor orangeColor] };  
  140. NSDictionary *attrDict5 = @{ NSBackgroundColorAttributeName: [UIColor redColor] };  
  141. NSDictionary *attrDict6 = @{ NSBackgroundColorAttributeName: [UIColor cyanColor] };  
  142.    
  143. _label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict4];  
  144. _label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict5];  
  145. _label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict6];  
  146.    
  147.    
  148.    
  149.    
  150.        仔细观察会发现个问题,我并没有关闭 NSForegroundColorAttributeName 属性,但是在运行结果中,所有字体的颜色都变成了默认色——黑色,这说明 NSForegroundColorAttributeName 和 NSBackgroundColorAttributeName 的低位是相等的,跟前面介绍的 textColor 一样,哪个属性最后一次赋值,就会冲掉前面的效果,若是我们把属性代码顺序交换一下  
  151.    
  152.    
  153. //NSBackgroundColorAttributeName 设置字体所在区域背景的颜色,取值为UIColor,默认值为nil  
  154.    
  155. NSDictionary *attrDict4 = @{ NSBackgroundColorAttributeName: [UIColor orangeColor] };  
  156. NSDictionary *attrDict5 = @{ NSBackgroundColorAttributeName: [UIColor redColor] };  
  157. NSDictionary *attrDict6 = @{ NSBackgroundColorAttributeName: [UIColor cyanColor] };  
  158.    
  159. _label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict4];  
  160. _label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict5];  
  161. _label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict6];  
  162.    
  163. //NSForegroundColorAttributeName 设置字体颜色,取值为 UIColor,默认为黑色  
  164.    
  165. NSDictionary *attrDict1 = @{ NSForegroundColorAttributeName: [UIColor redColor] };  
  166. NSDictionary *attrDict2 = @{ NSForegroundColorAttributeName: [UIColor blueColor] };  
  167. NSDictionary *attrDict3 = @{ NSForegroundColorAttributeName: [UIColor orangeColor] };  
  168.    
  169. _label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];  
  170. _label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];  
  171. _label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3];  
  172.    
  173.    
  174.    
  175.    
  176. 但是textColor属性可以与 NSBackgroundColorAttributeName 属性叠加  
  177.    
  178.    
  179. _label01.textColor = [UIColor greenColor];  
  180. _label02.textColor = [UIColor yellowColor];  
  181. _label03.textColor = [UIColor blueColor];  
  182.    
  183. //NSForegroundColorAttributeName 设置字体颜色,取值为 UIColor,默认为黑色  
  184.    
  185. NSDictionary *attrDict1 = @{ NSForegroundColorAttributeName: [UIColor redColor] };  
  186. NSDictionary *attrDict2 = @{ NSForegroundColorAttributeName: [UIColor blueColor] };  
  187. NSDictionary *attrDict3 = @{ NSForegroundColorAttributeName: [UIColor orangeColor] };  
  188.    
  189. _label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];  
  190. _label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];  
  191. _label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3];  
  192.    
  193.    
  194. //NSBackgroundColorAttributeName 设置字体所在区域背景的颜色,取值为UIColor,默认值为nil  
  195.    
  196. NSDictionary *attrDict4 = @{ NSBackgroundColorAttributeName: [UIColor orangeColor] };  
  197. NSDictionary *attrDict5 = @{ NSBackgroundColorAttributeName: [UIColor redColor] };  
  198. NSDictionary *attrDict6 = @{ NSBackgroundColorAttributeName: [UIColor cyanColor] };  
  199.    
  200. _label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict4];  
  201. _label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict5];  
  202. _label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict6];  
  203.    
  204.    
  205.    
  206.        虽然 textColor 在 NSFontAttributeName 之前赋值,但是由于 NSFontAttributeName 的属性效果被NSBackgroundColorAttributeName 属性冲掉了,所以最终显示了 textColor 的颜色。  
  207.    
  208.    
  209.    
  210. 3. NSLigatureAttributeName  
  211.    
  212. //NSLigatureAttributeName 设置连体属性,取值为NSNumber 对象(整数),0 表示没有连体字符,1 表示使用默认的连体字符,  
  213. //                        2 表示使用所有连体符号,默认值为 1(iOS 不支持 2)  
  214.    
  215. NSString *ligatureStr = @"flush";  
  216.    
  217. NSDictionary *attrDict1 = @{ NSLigatureAttributeName: [NSNumber numberWithInt: 0],  
  218.                              NSFontAttributeName: [UIFont fontWithName@"futura" size: 30] };  
  219. _label01.attributedText = [[NSAttributedString alloc] initWithString: ligatureStr attributes: attrDict1];  
  220.    
  221. NSDictionary *attrDict2 = @{ NSLigatureAttributeName: @(1),  
  222.                              NSFontAttributeName: [UIFont fontWithName@"futura" size: 30]   
  223.                              };  
  224. _label02.attributedText = [[NSAttributedString alloc] initWithString: ligatureStr attributes: attrDict2];  
  225.        由于要展示连体字符,所以将前面使用的带有中文的字符串换成 flush  
  226.    
  227.        NSLigatureAttributeName的取值为NSNumber对象,所以不能直接将一个整数值赋给它,创建 NSNumber 对象的方法有很多,或者可以简写成 @(int)  
  228.    
  229.    
  230.    
  231.    
  232.    
  233.        注意观察字母f和l之间的变化。  
  234.    
  235.        感觉连写就是一个艺术字功能,当字符f和l组合使用组合符号(所谓的字形(glyph))绘制时,看起来确实更加美观。但是并非所有的字符之间都有组合符号,事实上,只有某些字体中得某些字符的组合(如字符f和l,字符f和i等)才具有美观的组合符号。  
  236.    
  237.    
  238.    
  239. 4. NSKernAttributeName  
  240.    
  241.    
  242. //NSKernAttributeName 设定字符间距,取值为 NSNumber 对象(整数),正值间距加宽,负值间距变窄  
  243.      
  244.      
  245.   NSDictionary *attrDict1 = @{ NSKernAttributeName: @(-3),  
  246.                                NSFontAttributeName: [UIFont systemFontOfSize: 20]  
  247.                                };  
  248.   _label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];  
  249.      
  250.      
  251.   NSDictionary *attrDict2 = @{ NSKernAttributeName: @(0),  
  252.                                NSFontAttributeName: [UIFont systemFontOfSize: 20]  
  253.                                };  
  254.   _label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];  
  255.      
  256.      
  257.   NSDictionary *attrDict3 = @{ NSKernAttributeName: @(10),  
  258.                                NSFontAttributeName: [UIFont systemFontOfSize: 20]  
  259.                                };  
  260.   _label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3];  
  261.    
  262.    
  263.    
  264.    
  265.    
  266.    
  267. 5. NSStrikethroughStyleAttributeName  
  268.    
  269.    
  270. //NSStrikethroughStyleAttributeName 设置删除线,取值为 NSNumber 对象(整数),枚举常量 NSUnderlineStyle中的值  
  271. // NSUnderlineStyleNone   不设置删除线  
  272. // NSUnderlineStyleSingle 设置删除线为细单实线  
  273. // NSUnderlineStyleThick  设置删除线为粗单实线  
  274. // NSUnderlineStyleDouble 设置删除线为细双实线  
  275.    
  276.    
  277. NSDictionary *attrDict1 = @{ NSStrikethroughStyleAttributeName: @(NSUnderlineStyleSingle),  
  278.                              NSFontAttributeName: [UIFont systemFontOfSize:20] };  
  279. _label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];  
  280.    
  281.    
  282. NSDictionary *attrDict2 = @{ NSStrikethroughStyleAttributeName: @(NSUnderlineStyleThick),  
  283.                              NSFontAttributeName: [UIFont systemFontOfSize:20] };  
  284. _label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];  
  285.    
  286.    
  287. NSDictionary *attrDict3 = @{ NSStrikethroughStyleAttributeName: @(NSUnderlineStyleDouble),  
  288.                              NSFontAttributeName: [UIFont systemFontOfSize:20] };  
  289. _label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3];  
  290. 注意:  
  291.    
  292.        虽然使用了枚举常量,但是枚举常量的本质仍为整数,所以同样必须先转化为 NSNumber 才能使用  
  293.    
  294.        删除线和下划线使用相同的枚举常量作为其属性值  
  295.    
  296.        目前iOS中只有上面列出的4中效果,虽然我们能够在头文件中发现其他更多的取值,但是使用后没有任何效果  
  297.    
  298.    
  299.    
  300.    
  301.    
  302.    
  303. 可以看出,中文和英文删除线的位置有所不同  
  304.    
  305.        另外,删除线属性取值除了上面的4种外,其实还可以取其他整数值,有兴趣的可以自行试验,取值为 0 - 7时,效果为单实线,随着值得增加,单实线逐渐变粗,取值为 9 - 15时,效果为双实线,取值越大,双实线越粗。  
  306.    
  307. NSDictionary *attrDict1 = @{ NSStrikethroughStyleAttributeName: @(1),  
  308.                              NSFontAttributeName: [UIFont systemFontOfSize:20] };  
  309. _label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];  
  310.    
  311.    
  312. NSDictionary *attrDict2 = @{ NSStrikethroughStyleAttributeName: @(3),  
  313.                              NSFontAttributeName: [UIFont systemFontOfSize:20] };  
  314. _label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];  
  315.    
  316.    
  317. NSDictionary *attrDict3 = @{ NSStrikethroughStyleAttributeName: @(7),  
  318.                              NSFontAttributeName: [UIFont systemFontOfSize:20] };  
  319. _label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3];  
  320.    
  321.    
  322.    
  323.    
  324.    
  325. 6. NSStrikethroughColorAttributeName  
  326.    
  327.    
  328. //NSStrikethroughColorAttributeName 设置删除线颜色,取值为 UIColor 对象,默认值为黑色  
  329.      
  330.   NSDictionary *attrDict1 = @{ NSStrikethroughColorAttributeName: [UIColor blueColor],  
  331.                                NSStrikethroughStyleAttributeName: @(1),  
  332.                                NSFontAttributeName: [UIFont systemFontOfSize:20] };  
  333.   _label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];  
  334.      
  335.      
  336.   NSDictionary *attrDict2 = @{ NSStrikethroughColorAttributeName: [UIColor orangeColor],  
  337.                                NSStrikethroughStyleAttributeName: @(3),  
  338.                                NSFontAttributeName: [UIFont systemFontOfSize:20] };  
  339.   _label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];  
  340.      
  341.      
  342.   NSDictionary *attrDict3 = @{ NSStrikethroughColorAttributeName: [UIColor greenColor],  
  343.                                NSStrikethroughStyleAttributeName: @(7),  
  344.                                NSFontAttributeName: [UIFont systemFontOfSize:20] };  
  345.   _label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3];  
  346.    
  347.    
  348.    
  349.    
  350.    
  351.    
  352. 7. NSUnderlineStyleAttributeName  
  353.    
  354.     下划线除了线条位置和删除线不同外,其他的都可以完全参照删除线设置。  
  355.    
  356.    
  357. //NSUnderlineStyleAttributeName 设置下划线,取值为 NSNumber 对象(整数),枚举常量 NSUnderlineStyle中的值,与删除线类似  
  358.    
  359. NSDictionary *attrDict1 = @{ NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle),  
  360.                              NSFontAttributeName: [UIFont systemFontOfSize:20] };  
  361. _label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];  
  362.    
  363.    
  364. NSDictionary *attrDict2 = @{ NSUnderlineStyleAttributeName: @(NSUnderlineStyleThick),  
  365.                              NSFontAttributeName: [UIFont systemFontOfSize:20] };  
  366. _label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];  
  367.    
  368.    
  369. NSDictionary *attrDict3 = @{ NSUnderlineStyleAttributeName: @(NSUnderlineStyleDouble),  
  370.                              NSFontAttributeName: [UIFont systemFontOfSize:20] };  
  371. _label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3];  
  372.    
  373.    
  374.    
  375.    
  376.    
  377. 8. NSUnderlineColorAttributeName  
  378.    
  379.     可以完全参照下划线颜色设置  
  380.    
  381.    
  382. //NSUnderlineColorAttributeName 设置下划线颜色,取值为 UIColor 对象,默认值为黑色  
  383.       
  384.    NSDictionary *attrDict1 = @{ NSUnderlineColorAttributeName: [UIColor blueColor],  
  385.                                 NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle),  
  386.                                 NSFontAttributeName: [UIFont systemFontOfSize:20] };  
  387.    _label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];  
  388.       
  389.       
  390.    NSDictionary *attrDict2 = @{ NSUnderlineColorAttributeName: [UIColor orangeColor],  
  391.                                 NSUnderlineStyleAttributeName: @(NSUnderlineStyleThick),  
  392.                                 NSFontAttributeName: [UIFont systemFontOfSize:20] };  
  393.    _label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];  
  394.       
  395.       
  396.    NSDictionary *attrDict3 = @{ NSUnderlineColorAttributeName: [UIColor greenColor],  
  397.                                 NSUnderlineStyleAttributeName: @(NSUnderlineStyleDouble),  
  398.                                 NSFontAttributeName: [UIFont systemFontOfSize:20] };  
  399.    _label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3];  
  400.    
  401.    
  402.    
  403.    
  404.    
  405.    
  406. 9. NSStrokeWidthAttributeName  
  407.    
  408.    
  409. //NSStrokeWidthAttributeName 设置笔画宽度,取值为 NSNumber 对象(整数),负值填充效果,正值中空效果  
  410.      
  411.   NSDictionary *attrDict1 = @{ NSStrokeWidthAttributeName: @(-3),  
  412.                                NSFontAttributeName: [UIFont systemFontOfSize:30] };  
  413.   _label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];  
  414.      
  415.      
  416.   NSDictionary *attrDict2 = @{ NSStrokeWidthAttributeName: @(0),  
  417.                                NSFontAttributeName: [UIFont systemFontOfSize:30] };  
  418.   _label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];  
  419.      
  420.      
  421.   NSDictionary *attrDict3 = @{ NSStrokeWidthAttributeName: @(3),  
  422.                                NSFontAttributeName: [UIFont systemFontOfSize:30] };  
  423.   _label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3];  
  424.    
  425.    
  426.    
  427.    
  428.    
  429.    
  430. 10. NSStrokeColorAttributeName  
  431.    
  432.    
  433. //NSStrokeColorAttributeName 填充部分颜色,不是字体颜色,取值为 UIColor 对象  
  434.       
  435.    NSDictionary *attrDict1 = @{ NSStrokeWidthAttributeName: @(-3),  
  436.                                 NSStrokeColorAttributeName: [UIColor orangeColor],  
  437.                                 NSFontAttributeName: [UIFont systemFontOfSize:30] };  
  438.    _label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];  
  439.       
  440.       
  441.    NSDictionary *attrDict2 = @{ NSStrokeWidthAttributeName: @(0),  
  442.                                 NSStrokeColorAttributeName: [UIColor blueColor],  
  443.                                 NSFontAttributeName: [UIFont systemFontOfSize:30] };  
  444.    _label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];  
  445.       
  446.       
  447.    NSDictionary *attrDict3 = @{ NSStrokeWidthAttributeName: @(3),  
  448.                                 NSStrokeColorAttributeName: [UIColor greenColor],  
  449.                                 NSFontAttributeName: [UIFont systemFontOfSize:30] };  
  450.    _label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3];  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值