有时候需要自定义一个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
- #import <UIKit/UIKit.h>
- @interface ZJQView02 : UIView
- @property (nonatomic,strong) NSString* text;
- @end
ZJQView02.m
- #import "ZJQView02.h"
- #define IOS7 [[[UIDevice currentDevice]systemVersion] floatValue] >= 7.0 //判断SDK版本号是否是7.0或7。0以上
- @implementation ZJQView02
- -(instancetype)initWithFrame:(CGRect)frame{
- self=[super initWithFrame:frame];
- if (self) {}
- return self;
- }
- -(void)drawRect:(CGRect)rect{
- //An opaque type that represents a Quartz 2D drawing environment.
- //一个不透明类型的Quartz 2D绘画环境,相当于一个画布,你可以在上面任意绘画
- CGContextRef context = UIGraphicsGetCurrentContext();
- /*写文字*/
- UIFont *font = [UIFont boldSystemFontOfSize:12.0];//定义默认字体
- if (IOS7) {
- NSMutableParagraphStyle* paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
- paragraphStyle.lineBreakMode = NSLineBreakByCharWrapping;
- paragraphStyle.alignment=NSTextAlignmentCenter;//文字居中:发现只能水平居中,而无法垂直居中
- NSDictionary* attribute = @{
- NSForegroundColorAttributeName:[UIColor redColor],//设置文字颜色
- NSFontAttributeName:font,//设置文字的字体
- NSKernAttributeName:@10,//文字之间的字距
- NSParagraphStyleAttributeName:paragraphStyle,//设置文字的样式
- };
- //计算文字的宽度和高度:支持多行显示
- CGSize sizeText = [_text boundingRectWithSize:self.bounds.size
- options:NSStringDrawingUsesLineFragmentOrigin
- attributes:@{
- NSFontAttributeName:font,//设置文字的字体
- NSKernAttributeName:@10,//文字之间的字距
- }
- context:nil].size;
- CGFloat width = self.bounds.size.width;
- CGFloat height = self.bounds.size.height;
- //为了能够垂直居中,需要计算显示起点坐标x,y
- CGRect rect = CGRectMake((width-sizeText.width)/2, (height-sizeText.height)/2, sizeText.width, sizeText.height);
- [_text drawInRect:rect withAttributes:attribute];
- }else{
- CGContextSetRGBFillColor (context, 1, 0, 0, 1.0);//设置填充颜色:红色
- [_text drawInRect:self.bounds withFont:font];
- }
- }
- @end
ViewController990.h
- #import <UIKit/UIKit.h>
- #import "ZJQView02.h"
- @interface ViewController990 : UIViewController
- {
- @private
- ZJQView02* zjqView02;
- }
- @end
ViewController990.m
- #import "ViewController990.h"
- #import "MyLog.h"
- #define IOS7 [[[UIDevice currentDevice]systemVersion] floatValue] >= 7.0 //判断SDK版本号是否是7.0或7。0以上
- @interface ViewController990 ()
- @end
- @implementation ViewController990
- - (void)viewDidLoad {
- [super viewDidLoad];
- [self doInit];
- }
- - (void)didReceiveMemoryWarning {
- [super didReceiveMemoryWarning];
- }
- -(void)viewDidAppear:(BOOL)animated{
- [super viewDidAppear:animated];
- zjqView02.center=CGPointMake(self.view.center.x, (self.view.bounds.size.height-zjqView02.frame.origin.y)/2);//移到屏幕中心点
- [MyLog logViews:self.view.window];
- }
- -(void) doInit {
- //IOS7版本特殊处理
- if (IOS7) {
- self.edgesForExtendedLayout=UIRectEdgeNone;
- }
- self.view.backgroundColor=[UIColor whiteColor];
- zjqView02 = [[ZJQView02 alloc]initWithFrame:CGRectMake(50, 50, 200, 60)];
- zjqView02.backgroundColor=[UIColor yellowColor];
- zjqView02.text=@"我是测试文字我是测试文字";
- [self.view addSubview:zjqView02];
- }
- @end
执行效果如下: