NSAttributedString绘制多色镂空字符串

NSAttributeString顾名思义,就是可以携带各种属性的字符穿,提供了一种管理字符串中单个字符属性(字体,颜色,下划线等等)属性的方式。

继承自NSObject,所以其不具有UIKit的特性。

首先看下目标效果图


然后

第一步 设计接口(这是一个很好的习惯,由接口驱动编程)

创建一个UIView的子类DrawStringView

然后,这个类的接口有两个

1 显示的String内容

2 初始化的方法

这样,DrawStringView.h的完整代码如下

[objc]  view plain copy
  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface DrawStringView : UIView  
  4. @property (strong,nonatomic)NSString * contentString;  
  5. -(id)initWithContent:(NSString *)content Frame:(CGRect)frame;  
  6. @end  
第二步 完成实现

具体的过程见代码注释

这样,完整代码如下

[objc]  view plain copy
  1. </pre><pre name="code" class="objc">//  
  2. //  DrawStringView.m  
  3. //  ProjectForCSDN  
  4. //  
  5. //  Created by huangwenchen on 14/12/25.  
  6. //  Copyright (c) 2014年 huangwenchen. All rights reserved.  
  7. //  
  8.   
  9. #import "DrawStringView.h"  
  10.   
  11. @implementation DrawStringView  
  12.   
  13. #pragma mark - property  
  14. //每次修改contentString,都应该重新绘制  
  15. -(void)setContentString:(NSString *)contentString{  
  16.     _contentString = contentString;  
  17.     [self setNeedsDisplay];//重新绘制,调用drawRect  
  18. }  
  19. #pragma mark - redraw  
  20. -(void)drawRect:(CGRect)rect{  
  21.     NSMutableParagraphStyle * style = [[NSMutableParagraphStyle alloc] init];  
  22.     style.alignment = NSTextAlignmentCenter;//居中  
  23.     style.lineBreakMode = NSLineBreakByWordWrapping;//以词为单位换行  
  24.     UIFont * font = [UIFont fontWithName:@"Helvetica" size:30];  
  25.     NSMutableAttributedString * attributeStirng = [[NSMutableAttributedString alloc]initWithString:self.contentString];  
  26.     NSDictionary *  firstPartAttributes = @{ NSFontAttributeName:font,// 字体  
  27.                                             NSParagraphStyleAttributeName:style,//绘制样式  
  28.                                             NSForegroundColorAttributeName:[UIColor whiteColor],//填充色  
  29.                                             NSStrokeColorAttributeName:[UIColor blueColor],//描边色  
  30.                                             NSStrokeWidthAttributeName:@(3)//描边线宽  
  31.                                             };  
  32.     NSDictionary * secondPartAttributes = @{ NSFontAttributeName:font,  
  33.                                              NSParagraphStyleAttributeName:style,  
  34.                                              NSForegroundColorAttributeName:[UIColor whiteColor],  
  35.                                              NSStrokeColorAttributeName:[UIColor redColor],  
  36.                                              NSStrokeWidthAttributeName:@(3)  
  37.                                              };  
  38.     NSUInteger halfLocation = (int)attributeStirng.length/2;  
  39.     [attributeStirng addAttributes:firstPartAttributes range:NSMakeRange(0,halfLocation)];//前一半的样式  
  40.     [attributeStirng addAttributes:secondPartAttributes range:NSMakeRange(halfLocation,attributeStirng.length - halfLocation)];//后一半的样式  
  41.     [attributeStirng drawInRect:self.bounds];  
  42. }  
  43. #pragma mark - init method  
  44. -(id)initWithContent:(NSString *)content Frame:(CGRect)frame{  
  45.     if (self = [super initWithFrame:frame]) {  
  46.         self.contentString = content;  
  47.         self.opaque = NO;//不透明  
  48.     }  
  49.     return self;  
  50. }  
  51. @end  
当然,NSAttributeString还有更多的属性,以上是常用的几种,更多的属性参照XCode中的文档,很详细

三 使用这个类

[objc]  view plain copy
  1. UIView * viewWithString = [[DrawStringView alloc] initWithContent:@"This is test string" Frame:CGRectMake(100100200200)];  
  2. [self.view addSubview:viewWithString];  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值