iOS-在画布上写文字

有时候需要自定义一个UIView,然后在自定义的UIView上输出一段文字。如下面的例子和演示图。

需要注意的是以下几点:

1. ios7 采用方法 [_text drawInRect:rect withAttributes:attribute]; 而ios7之前是采用[_text drawInRect:self.bounds withFont:font]。

2.CGSize sizeText = [_text boundingRectWithSize:self.bounds.size
                                              options:NSStringDrawingUsesLineFragmentOrigin
                                           attributes:@{
                                                        NSFontAttributeName:font,//设置文字的字体
                                                        NSKernAttributeName:@10,//文字之间的字距
                                                        }
                                              context:nil].size 用来计算单行或多行文字的高度和宽度

 

 

ZJQView02.h

C代码   收藏代码
  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface ZJQView02 : UIView  
  4.   
  5. @property (nonatomic,strong) NSString* text;  
  6.   
  7. @end  

 

ZJQView02.m

C代码   收藏代码
  1. #import "ZJQView02.h"  
  2. #define IOS7 [[[UIDevice currentDevice]systemVersion] floatValue] >= 7.0 //判断SDK版本号是否是7.0或7。0以上  
  3.   
  4. @implementation ZJQView02  
  5.   
  6. -(instancetype)initWithFrame:(CGRect)frame{  
  7.     self=[super initWithFrame:frame];  
  8.     if (self) {}  
  9.     return self;  
  10. }  
  11.   
  12. -(void)drawRect:(CGRect)rect{  
  13.     //An opaque type that represents a Quartz 2D drawing environment.  
  14.     //一个不透明类型的Quartz 2D绘画环境,相当于一个画布,你可以在上面任意绘画  
  15.     CGContextRef context = UIGraphicsGetCurrentContext();  
  16.       
  17.     /*写文字*/  
  18.     UIFont  *font = [UIFont boldSystemFontOfSize:12.0];//定义默认字体  
  19.     if (IOS7) {  
  20.         NSMutableParagraphStyle* paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];  
  21.         paragraphStyle.lineBreakMode = NSLineBreakByCharWrapping;  
  22.         paragraphStyle.alignment=NSTextAlignmentCenter;//文字居中:发现只能水平居中,而无法垂直居中  
  23.         NSDictionary* attribute = @{  
  24.                                     NSForegroundColorAttributeName:[UIColor redColor],//设置文字颜色  
  25.                                     NSFontAttributeName:font,//设置文字的字体  
  26.                                     NSKernAttributeName:@10,//文字之间的字距  
  27.                                     NSParagraphStyleAttributeName:paragraphStyle,//设置文字的样式  
  28.                                     };  
  29.           
  30.         //计算文字的宽度和高度:支持多行显示  
  31.         CGSize sizeText = [_text boundingRectWithSize:self.bounds.size  
  32.                                               options:NSStringDrawingUsesLineFragmentOrigin  
  33.                                            attributes:@{  
  34.                                                         NSFontAttributeName:font,//设置文字的字体  
  35.                                                         NSKernAttributeName:@10,//文字之间的字距  
  36.                                                         }  
  37.                                               context:nil].size;  
  38.           
  39.         CGFloat width = self.bounds.size.width;  
  40.         CGFloat height = self.bounds.size.height;  
  41.           
  42.         //为了能够垂直居中,需要计算显示起点坐标x,y  
  43.         CGRect rect = CGRectMake((width-sizeText.width)/2, (height-sizeText.height)/2, sizeText.width, sizeText.height);  
  44.         [_text drawInRect:rect withAttributes:attribute];  
  45.     }else{  
  46.         CGContextSetRGBFillColor (context,  1, 0, 0, 1.0);//设置填充颜色:红色  
  47.         [_text drawInRect:self.bounds withFont:font];  
  48.     }  
  49. }  
  50.   
  51. @end  

 

 

 

ViewController990.h

C代码   收藏代码
  1. #import <UIKit/UIKit.h>  
  2. #import "ZJQView02.h"  
  3.   
  4. @interface ViewController990 : UIViewController  
  5. {  
  6. @private  
  7.       
  8.     ZJQView02* zjqView02;  
  9. }  
  10. @end  

 

ViewController990.m

C代码   收藏代码
  1. #import "ViewController990.h"  
  2. #import "MyLog.h"  
  3. #define IOS7 [[[UIDevice currentDevice]systemVersion] floatValue] >= 7.0 //判断SDK版本号是否是7.0或7。0以上  
  4.   
  5.   
  6. @interface ViewController990 ()  
  7.   
  8. @end  
  9.   
  10. @implementation ViewController990  
  11.   
  12. - (void)viewDidLoad {  
  13.     [super viewDidLoad];  
  14.     [self doInit];  
  15. }  
  16.   
  17. - (void)didReceiveMemoryWarning {  
  18.     [super didReceiveMemoryWarning];  
  19. }  
  20.   
  21. -(void)viewDidAppear:(BOOL)animated{  
  22.     [super viewDidAppear:animated];  
  23.     zjqView02.center=CGPointMake(self.view.center.x, (self.view.bounds.size.height-zjqView02.frame.origin.y)/2);//移到屏幕中心点  
  24.     [MyLog logViews:self.view.window];  
  25. }  
  26.   
  27. -(void) doInit {  
  28.     //IOS7版本特殊处理  
  29.     if (IOS7) {  
  30.         self.edgesForExtendedLayout=UIRectEdgeNone;  
  31.     }  
  32.     self.view.backgroundColor=[UIColor whiteColor];  
  33.     zjqView02 = [[ZJQView02 alloc]initWithFrame:CGRectMake(50, 50, 200, 60)];  
  34.     zjqView02.backgroundColor=[UIColor yellowColor];  
  35.     zjqView02.text=@"我是测试文字我是测试文字";  
  36.       
  37.     [self.view addSubview:zjqView02];  
  38. }  
  39.   
  40. @end  

 

执行效果如下:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值