UILabel和UITextView 设置描边阴影了下划线了

来源:互联网 发布:linux 文本编辑器 编辑:程序博客网 时间:2024/05/07 06:51



转载:http://blog.csdn.net/jeffasd/article/details/52039786

一:UILabel

  1. 一个简单的例子  
  2.   
  3. 绘制不同颜色不同字体的一个AttributeString,如图   
  4. 这里写图片描述   
  5. 代码  
  6.   
  7.  UILabel * Label = [[UILabel alloc]initWithFrame:CGRectMake(10010020060)];  
  8.     [self.view addSubview:Label];  
  9.     NSMutableAttributedString * firstPart = [[NSMutableAttributedString alloc] initWithString:@"Wenchen"];  
  10.     NSDictionary * firstAttributes = @{ NSFontAttributeName:[UIFont boldSystemFontOfSize:12],NSForegroundColorAttributeName:[UIColor redColor],};  
  11.     [firstPart setAttributes:firstAttributes range:NSMakeRange(0,firstPart.length)];  
  12.     NSMutableAttributedString * secondPart = [[NSMutableAttributedString alloc] initWithString:@" Huang"];  
  13.     NSDictionary * secondAttributes = @{NSFontAttributeName:[UIFont systemFontOfSize:10],NSForegroundColorAttributeName:[UIColor blueColor],};  
  14.     [secondPart setAttributes:secondAttributes range:NSMakeRange(0,secondPart.length)];  
  15.     [firstPart appendAttributedString:secondPart];  
  16.     Label.attributedText = firstPart;  
  17. 1  
  18. 2  
  19. 3  
  20. 4  
  21. 5  
  22. 6  
  23. 7  
  24. 8  
  25. 9  
  26. 10  
  27. 先看看所有的Key  
  28.   
  29. NSFontAttributeName; //字体,value是UIFont对象  
  30. NSParagraphStyleAttributeName;//绘图的风格(居中,换行模式,间距等诸多风格),value是NSParagraphStyle对象  
  31. NSForegroundColorAttributeName;// 文字颜色,value是UIFont对象  
  32. NSBackgroundColorAttributeName;// 背景色,value是UIFont  
  33. NSLigatureAttributeName; //  字符连体,value是NSNumber  
  34. NSKernAttributeName; // 字符间隔  
  35. NSStrikethroughStyleAttributeName;//删除线,value是NSNumber  
  36. NSUnderlineStyleAttributeName;//下划线,value是NSNumber  
  37. NSStrokeColorAttributeName; //描绘边颜色,value是UIColor  
  38. NSStrokeWidthAttributeName; //描边宽度,value是NSNumber  
  39. NSShadowAttributeName; //阴影,value是NSShadow对象  
  40. NSTextEffectAttributeName; //文字效果,value是NSString  
  41. NSAttachmentAttributeName;//附属,value是NSTextAttachment 对象  
  42. NSLinkAttributeName;//链接,value是NSURL or NSString  
  43. NSBaselineOffsetAttributeName;//基础偏移量,value是NSNumber对象  
  44. NSUnderlineColorAttributeName;//下划线颜色,value是UIColor对象  
  45. NSStrikethroughColorAttributeName;//删除线颜色,value是UIColor  
  46. NSObliquenessAttributeName; //字体倾斜  
  47. NSExpansionAttributeName; //字体扁平化  
  48. NSVerticalGlyphFormAttributeName;//垂直或者水平,value是 NSNumber,0表示水平,1垂直  
  49. 1  
  50. 2  
  51. 3  
  52. 4  
  53. 5  
  54. 6  
  55. 7  
  56. 8  
  57. 9  
  58. 10  
  59. 11  
  60. 12  
  61. 13  
  62. 14  
  63. 15  
  64. 16  
  65. 17  
  66. 18  
  67. 19  
  68. 20  
  69. 字体,颜色,背景色  
  70.   
  71. 涉及到的属性   
  72. - NSFontAttributeName   
  73. - NSForegroundColorAttributeName   
  74. - NSBackgroundColorAttributeName  
  75.   
  76. 效果   
  77. 这里写图片描述  
  78.   
  79. 代码  
  80.   
  81. UILabel * Label = [[UILabel alloc]initWithFrame:CGRectMake(10010020060)];  
  82.     [self.view addSubview:Label];  
  83.     NSMutableAttributedString * mutableAttriStr = [[NSMutableAttributedString alloc] initWithString:@"Wenchen"];  
  84.     NSDictionary * attris = @{NSForegroundColorAttributeName:[UIColor whiteColor],NSBackgroundColorAttributeName:[UIColor grayColor],NSFontAttributeName:[UIFont boldSystemFontOfSize:14]};  
  85.     [mutableAttriStr setAttributes:attris range:NSMakeRange(0,mutableAttriStr.length)];  
  86.     Label.attributedText = mutableAttriStr;  
  87. 1  
  88. 2  
  89. 3  
  90. 4  
  91. 5  
  92. 6  
  93. 下划线  
  94.   
  95. 涉及到的两个属性  
  96.   
  97. NSUnderlineStyleAttributeName  
  98. NSUnderlineColorAttributeName  
  99. 效果   
  100. 这里写图片描述  
  101.   
  102. 代码  
  103.   
  104.     UILabel * Label = [[UILabel alloc]initWithFrame:CGRectMake(10010020060)];  
  105.     [self.view addSubview:Label];  
  106.     NSMutableAttributedString * mutableAttriStr = [[NSMutableAttributedString alloc] initWithString:@"Wenchen"];  
  107.     NSDictionary * attris = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:12],NSForegroundColorAttributeName:[UIColor redColor],                          NSUnderlineStyleAttributeName:@(NSUnderlineStyleSingle),                             NSUnderlineColorAttributeName:[UIColor blueColor],};  
  108.     [mutableAttriStr setAttributes:attris range:NSMakeRange(0,mutableAttriStr.length)];  
  109.     Label.attributedText = mutableAttriStr;  
  110. 1  
  111. 2  
  112. 3  
  113. 4  
  114. 5  
  115. 6  
  116. 7  
  117. 描边  
  118.   
  119. 涉及到的两个属性  
  120.   
  121. NSStrokeColorAttributeName  
  122. NSStrokeWidthAttributeName  
  123. 效果   
  124. 这里写图片描述  
  125.   
  126. 代码  
  127.   
  128.     UILabel * Label = [[UILabel alloc]initWithFrame:CGRectMake(10010020060)];  
  129.     [self.view addSubview:Label];  
  130.     NSMutableAttributedString * mutableAttriStr = [[NSMutableAttributedString alloc] initWithString:@"Wenchen"];  
  131.     NSDictionary * attris = @{NSForegroundColorAttributeName:[UIColor whiteColor],NSStrokeColorAttributeName:[UIColor greenColor],NSStrokeWidthAttributeName:@(2)};  
  132.     [mutableAttriStr setAttributes:attris range:NSMakeRange(0,mutableAttriStr.length)];  
  133.     Label.attributedText = mutableAttriStr;  
  134. 1  
  135. 2  
  136. 3  
  137. 4  
  138. 5  
  139. 6  
  140. 附属属性(例如图片)  
  141.   
  142. 涉及到的属性  
  143.   
  144. NSAttachmentAttributeName  
  145. 效果   
  146. 这里写图片描述   
  147. 代码  
  148.   
  149.   UILabel * Label = [[UILabel alloc]initWithFrame:CGRectMake(10010020060)];  
  150.     [self.view addSubview:Label];  
  151.     //创建Attachment Str  
  152.     NSTextAttachment * attach = [[NSTextAttachment alloc] init];  
  153.     attach.image = [UIImage imageNamed:@"memoAccess"];  
  154.     attach.bounds = CGRectMake(002020);  
  155.     NSAttributedString * imageStr = [NSAttributedString attributedStringWithAttachment:attach];  
  156.     //添加  
  157.     NSMutableAttributedString * mutableAttriStr = [[NSMutableAttributedString alloc] initWithString:@"Wenchen"];  
  158.     [mutableAttriStr appendAttributedString:imageStr];  
  159.     Label.attributedText = mutableAttriStr;  
  160. 1  
  161. 2  
  162. 3  
  163. 4  
  164. 5  
  165. 6  
  166. 7  
  167. 8  
  168. 9  
  169. 10  
  170. 11  
  171. 绘制风格  
  172.   
  173. 为什么要红字?因为这个属性真的很重要。  
  174.   
  175. 涉及到的属性  
  176.   
  177. NSMutableParagraphStyle  
  178.   
  179. 效果(看起来很奇怪,仅仅为了展示某些功能)   
  180. 这里写图片描述  
  181.   
  182. 代码  
  183.   
  184. @interface TestView : UIView  
  185.   
  186. @end  
  187.   
  188. @implementation TestView  
  189. -(instancetype)initWithFrame:(CGRect)frame{  
  190.     self = [super initWithFrame:frame];  
  191.     if (!self) {  
  192.         return nil;  
  193.     }  
  194.     self.opaque = NO;  
  195.     return self;  
  196. }  
  197.   
  198. // An empty implementation adversely affects performance during animation.  
  199. - (void)drawRect:(CGRect)rect {  
  200.     NSMutableAttributedString * attributeStr = [[NSMutableAttributedString alloc] initWithString:@"The anthor of this blog is wenchenhuang"];  
  201.     NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];  
  202.     paragraphStyle.alignment = NSTextAlignmentRight;  
  203.     paragraphStyle.headIndent = 4.0;  
  204.     paragraphStyle.lineBreakMode = NSLineBreakByCharWrapping;  
  205.     paragraphStyle.lineSpacing = 2.0;  
  206.     NSDictionary * attributes = @{NSParagraphStyleAttributeName:paragraphStyle};  
  207.     [attributeStr setAttributes:attributes range:NSMakeRange(0, attributeStr.length)];  
  208.     [attributeStr drawInRect:self.bounds];  
  209. }  
  210. 1  
  211. 2  
  212. 3  
  213. 4  
  214. 5  
  215. 6  
  216. 7  
  217. 8  
  218. 9  
  219. 10  
  220. 11  
  221. 12  
  222. 13  
  223. 14  
  224. 15  
  225. 16  
  226. 17  
  227. 18  
  228. 19  
  229. 20  
  230. 21  
  231. 22  
  232. 23  
  233. 24  
  234. 25  
  235. 26  
  236. 27  
  237.     TestView  *test = [[TestView alloc] initWithFrame:CGRectMake(100100,10060)];  
  238.     [self.view addSubview:test];  
  239. 1  
  240. 2  
  241. 阴影  
  242.   
  243. 涉及到的属性  
  244.   
  245. NSShadowAttributeName  
  246. 效果   
  247. 这里写图片描述  
  248.   
  249.   UILabel * Label = [[UILabel alloc]initWithFrame:CGRectMake(10010020060)];  
  250.     [self.view addSubview:Label];  
  251.     NSMutableAttributedString * mutableAttriStr = [[NSMutableAttributedString alloc] initWithString:@"Wenchen"];  
  252.     NSShadow * shadow = [[NSShadow alloc] init];  
  253.     shadow.shadowColor = [UIColor blueColor];  
  254.     shadow.shadowBlurRadius = 2.0;  
  255.     shadow.shadowOffset = CGSizeMake(1.01.0);  
  256.     NSDictionary * attris = @{NSShadowAttributeName:shadow};  
  257.     [mutableAttriStr setAttributes:attris range:NSMakeRange(0,mutableAttriStr.length)];  
  258.     Label.attributedText = mutableAttriStr;  
  259. 1  
  260. 2  
  261. 3  
  262. 4  
  263. 5  
  264. 6  
  265. 7  
  266. 8  
  267. 9  
  268. 10  
  269. 文字效果  
  270.   
  271. 相关属性  
  272.   
  273. NSTextEffectAttributeName   
  274. 这里写图片描述  
  275. 代码  
  276.   
  277.  UILabel * Label = [[UILabel alloc]initWithFrame:CGRectMake(10010020060)];  
  278.     [self.view addSubview:Label];  
  279.     NSMutableAttributedString * mutableAttriStr = [[NSMutableAttributedString alloc] initWithString:@"Wenchen"];  
  280.     NSDictionary * attris = @{NSTextEffectAttributeName:NSTextEffectLetterpressStyle};  
  281.     [mutableAttriStr setAttributes:attris range:NSMakeRange(0,mutableAttriStr.length)];  
  282.     Label.attributedText = mutableAttriStr;  
  283. 1  
  284. 2  
  285. 3  
  286. 4  
  287. 5  
  288. 6  
  289. 链接  
  290.   
  291. 相关属性  
  292.   
  293. NSLinkAttributeName  
  294. 例子  
  295.   
  296. 点击打开链接  
  297.   
  298. @interface ViewController ()<UITextViewDelegate>  
  299.   
  300. @end  
  301.   
  302. @implementation ViewController  
  303.   
  304.   
  305.   
  306. - (void)viewDidLoad {  
  307.     [super viewDidLoad];  
  308.     UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(100100100100)];  
  309.     textView.scrollEnabled = NO;  
  310.     textView.editable = NO;  
  311.     textView.textContainer.lineFragmentPadding = 0;  
  312.     textView.textContainerInset = UIEdgeInsetsMake(0000);  
  313.     textView.delegate = self;  
  314.     [self.view addSubview:textView];  
  315.     NSMutableAttributedString * mutableAttriStr = [[NSMutableAttributedString alloc] initWithString:@"Wenchen"];  
  316.     NSDictionary * attris = @{NSLinkAttributeName:[NSURL URLWithString:@"http://www.baidu.com"]};  
  317.     [mutableAttriStr setAttributes:attris range:NSMakeRange(0,mutableAttriStr.length)];  
  318.     textView.attributedText = mutableAttriStr;  
  319.         // Do any additional setup after loading the view, typically from a nib.  
  320. }  
  321. - (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)url inRange:(NSRange)characterRange  
  322. {  
  323.     return YES;  
  324. }  
  325. 1  
  326. 2  
  327. 3  
  328. 4  
  329. 5  
  330. 6  
  331. 7  
  332. 8  
  333. 9  
  334. 10  
  335. 11  
  336. 12  
  337. 13  
  338. 14  
  339. 15  
  340. 16  
  341. 17  
  342. 18  
  343. 19  
  344. 20  
  345. 21  
  346. 22  
  347. 23  
  348. 24  
  349. 25  
  350. 26  
  351. 27  
  352. 文字连体  
  353.   
  354. 涉及到的属性  
  355.   
  356. NSLigatureAttributeName  
  357. 举例   
  358. NSLigatureAttributeName 属性位@(0) 和@(1)   
  359. 这里写图片描述  
  360.   
  361. 这里写图片描述  
  362.   
  363. 字符间隔  
  364.   
  365. 相关属性  
  366.   
  367. NSKernAttributeName  
  368. 举例 字符间距拉大到4   
  369. 这里写图片描述   
  370. 对比下,默认的   
  371. 这里写图片描述  
  372.   
  373. UILabel * Label = [[UILabel alloc]initWithFrame:CGRectMake(100100200120)];  
  374.     [self.view addSubview:Label];  
  375.     NSMutableAttributedString * mutableAttriStr = [[NSMutableAttributedString alloc] initWithString:@"Wenchen"];  
  376.     NSDictionary * attris = @{NSKernAttributeName:@(4),  
  377.                               NSFontAttributeName:[UIFont systemFontOfSize:30]};  
  378.     [mutableAttriStr setAttributes:attris range:NSMakeRange(0,mutableAttriStr.length)];  
  379.     Label.attributedText = mutableAttriStr;  
  380. 1  
  381. 2  
  382. 3  
  383. 4  
  384. 5  
  385. 6  
  386. 7  
  387. baseline基础偏移量  
  388.   
  389. 相关属性   
  390. - NSBaselineOffsetAttributeName  
  391.   
  392. 效果   
  393. 对比下偏移量为0 和20   
  394.   
  395.   
  396. 代码  
  397.   
  398.   NSDictionary * attris = @{NSBaselineOffsetAttributeName:@(0),  
  399.                               NSFontAttributeName:[UIFont systemFontOfSize:30]};  
  400. 1  
  401. 2  
  402. 字体倾斜  
  403.   
  404. 相关属性  
  405.   
  406. NSObliquenessAttributeName   
  407. 效果   
  408. 这里写图片描述  
  409. 代码  
  410.   
  411.     NSDictionary * attris = @{NSObliquenessAttributeName:@(0.5),  
  412.                               NSFontAttributeName:[UIFont systemFontOfSize:30]};  
  413. 1  
  414. 2  
  415. 字体扁平化  
  416.   
  417. 相关属性  
  418.   
  419. NSExpansionAttributeName  
  420. 效果   
  421. 这里写图片描述  
  422.   
  423. 代码  
  424.   
  425. NSDictionary * attris = @{NSExpansionAttributeName:@(1.0),  
  426.                               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


0 0
原创粉丝点击