iOS-在画布上写文字
2016-07-01 09:18
453 查看
有时候需要自定义一个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代码
#import <UIKit/UIKit.h>
@interface ZJQView02 : UIView
@property (nonatomic,strong) NSString* text;
@end
ZJQView02.m
C代码
#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
C代码
#import <UIKit/UIKit.h>
#import "ZJQView02.h"
@interface ViewController990 : UIViewController
{
@private
ZJQView02* zjqView02;
}
@end
ViewController990.m
C代码
#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
执行效果如下:
需要注意的是以下几点:
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代码
#import <UIKit/UIKit.h>
@interface ZJQView02 : UIView
@property (nonatomic,strong) NSString* text;
@end
ZJQView02.m
C代码
#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
C代码
#import <UIKit/UIKit.h>
#import "ZJQView02.h"
@interface ViewController990 : UIViewController
{
@private
ZJQView02* zjqView02;
}
@end
ViewController990.m
C代码
#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
执行效果如下:
相关文章推荐
- iOS App中调用iPhone各种感应器的方法总结
- bug 整理
- 3分钟实现iOS语言本地化/国际化(图文详解)
- iOS可执行文件瘦身方法
- iOS创建界面方法的讨论
- 常用iOS-oc工具方法总结
- iOS下的并行开发
- iOS 通过URL获取图片,并保存到本地
- iOS 多种数据类型存入NSMutableArray数组
- iOS 去掉导航栏返回按钮的文字
- iOS 检查本地是否存在某个文件
- iOS Xcode 获取项目版本号
- iOS NSLocale相关方法
- iOS 自定义导航栏 NavigationBar
- iOS 设置Label的行间距
- iOS tableView 自带的下拉刷新
- iOS 设置控件的边框、圆角
- iOS 一个label中显示不同颜色的文字
- iOS 输出CGRect格式的数据
- iOS 给label添加点击事件