本文简单介绍一下CGContextRef的文本绘制
CGContextRef文本绘制的大致步奏
- 1 获取CGContextRef
- 2 设置绘制文本的相关属性(字体大小,字体颜色)
- 3 如果需要对文本进行变换,先调用CGContextSetTextMatrix(),再调用CGContextSetTextPosition()和CTLineDraw()绘制文本;如果不需要对文本进行变换则直接调用NSString的drawAtPoint: withAttributes:
下面示例通过Quartz 2D绘制文本
- 1 新建一个工程 ,创建两个UISlider分别控制文字的缩放和旋转
- (void)viewDidLoad {
[super viewDidLoad];
UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(50, self.view.frame.size.height - 100, self.view.frame.size.width - 100, 20)];
slider.minimumValue = 0.1;
slider.maximumValue = 5;
[slider addTarget:self action:@selector(scaleChange:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:slider];
UILabel *textLable = [[UILabel alloc] initWithFrame:CGRectMake(0, slider.frame.origin.y , 50, 20)];
textLable.text = @"缩放";
[self.view addSubview:textLable];
UISlider *slider1 = [[UISlider alloc] initWithFrame:CGRectMake(50, self.view.frame.size.height - 50, self.view.frame.size.width - 100, 20)];
slider1.minimumValue = - 90;
slider1.maximumValue = 90;
[slider1 addTarget:self action:@selector(rotateChange:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:slider1];
UILabel *textLable1 = [[UILabel alloc] initWithFrame:CGRectMake(0, slider1.frame.origin.y , 50, 20)];
textLable1.text = @"旋转";
[self.view addSubview:textLable1];
view = [[CGTextView alloc] initWithFrame:self.view.bounds];
view.backgroundColor = [UIColor clearColor];
view.userInteractionEnabled = NO;
[self.view addSubview:view];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)scaleChange:(UISlider *)sl {
NSLog(@"缩放:%f",sl.value);
view.scaleRate = sl.value;
}
- (void)rotateChange:(UISlider *)sl {
NSLog(@"旋转:%f",sl.value);
view.rotateAngle = sl.value;
}
- 2 CGTextView是文本绘制类
#import "CGTextView.h"
#import <CoreText/CoreText.h>
@implementation CGTextView
- (void)setScaleRate:(CGFloat)scaleRate {
if (_scaleRate != scaleRate) {
_scaleRate = scaleRate;
[self setNeedsDisplay];
}
}
- (void)setRotateAngle:(CGFloat)rotateAngle {
if (_rotateAngle != rotateAngle) {
_rotateAngle = rotateAngle;
[self setNeedsDisplay];
}
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
*/
- (void)drawRect:(CGRect)rect {
CGContextRef ctx = UIGraphicsGetCurrentContext();
//设置字符间距
CGContextSetCharacterSpacing(ctx, 4);
//设置填充颜色
CGContextSetRGBFillColor(ctx, 1, 0, 1, 1);
//设置线条颜色
CGContextSetRGBStrokeColor(ctx, 0, 0, 1, 1);
//设置使用填充模式绘制文字
CGContextSetTextDrawingMode(ctx, kCGTextFill);
//绘制文字
[@"iOS text" drawAtPoint:CGPointMake(10, 20) withAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"Arial Rounded MT Bold" size:45], NSFontAttributeName,[UIColor magentaColor],NSForegroundColorAttributeName, nil]];
//设置描边模式绘制文字
CGContextSetTextDrawingMode(ctx, kCGTextStroke);
//绘制文字
[@"iOS text" drawAtPoint:CGPointMake(10, 80) withAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"Heiti SC" size:40], NSFontAttributeName,[UIColor blueColor],NSForegroundColorAttributeName, nil]];
//使用填充,描边模式绘制文字
CGContextSetTextDrawingMode(ctx, kCGTextFillStroke);
//绘制文字
[@"iOS text" drawAtPoint:CGPointMake(10, 130) withAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"Heiti SC" size:50], NSFontAttributeName,[UIColor magentaColor],NSForegroundColorAttributeName, nil]];
//定义一个垂直镜像的变换矩阵
CGAffineTransform yrevert = CGAffineTransformMake(1, 0, 0, -1, 0, 0);
//为yreveta变换矩阵根据scaleRate添加缩放变换矩阵
CGAffineTransform scale = CGAffineTransformScale(yrevert, self.scaleRate, self.scaleRate);
//为scale变换矩阵根据rotateAngle添加旋转变换矩阵
CGAffineTransform rotate = CGAffineTransformRotate(scale, self.rotateAngle*M_PI/180);
//对cgcontextref绘制文字时应用变换
CGContextSetTextMatrix(ctx, rotate);
//设置绘制文本的字体和字体大小
NSDictionary *attribs = @{NSFontAttributeName:[UIFont fontWithName:@"Courier New" size:40.0]};
//绘制变换
NSAttributedString *fontStr = [[NSAttributedString alloc] initWithString:@"iOS text" attributes:attribs];
CTLineRef displayLine = CTLineCreateWithAttributedString( (__bridge CFAttributedStringRef)fontStr );
CGContextSetTextPosition( ctx, 50, 300 );
CTLineDraw( displayLine, ctx );
CFRelease( displayLine );
}
- 3运行之后如下所示