iOS 文本绘制

这篇博客探讨了如何使用CGContextRef在iOS中进行文本绘制,详细介绍了绘制的步骤,包括获取CGContextRef、设置字体属性以及利用Quartz 2D进行文本变换和绘制。文中还给出了通过UISlider控制文本缩放和旋转的实践示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文简单介绍一下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运行之后如下所示
    这里写图片描述
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值