Quartz2D简单使用(二)

来源:互联网 发布:redis同步到mysql 编辑:程序博客网 时间:2024/05/02 02:25

一:绘制文字

////  YXtextView.m//  Quartz2D简单使用(二)////  Created by yeyuanxiang on 16/6/28.//  Copyright © 2016年 KuYu. All rights reserved.//#import "YXtextView.h"@implementation YXtextView// Only override drawRect: if you perform custom drawing.// An empty implementation adversely affects performance during animation.- (void)drawRect:(CGRect)rect {        //画文字    NSString *str = @"不推荐使用C语言的方法绘制文字, 因为quraz2d中的坐标系和UIkit中的坐标系不一致, 绘制出来的文字是颠倒的, 而且通过C语言的方法绘制文字相当麻烦" ;        //获取上下文    CGContextRef ctx = UIGraphicsGetCurrentContext() ;        //绘图    CGContextAddRect(ctx, CGRectMake(50, 50, 300, 150));        //颜色设置    [[UIColor yellowColor] set];        //渲染//    CGContextStrokePath(ctx);    CGContextFillPath(ctx);            /**     *文字设置     */        NSMutableDictionary *md = [NSMutableDictionary dictionary];    //设置文字的颜色    md[NSForegroundColorAttributeName] = [UIColor redColor];    //设置文字背景颜色//    md[NSBackgroundColorAttributeName] = [UIColor greenColor];    //设置文字大小    md[NSFontAttributeName] = [UIFont systemFontOfSize:20];        //将文字绘制到指定点的位置//    [str drawAtPoint:CGPointMake(10, 10) withAttributes:md];            [str drawInRect:CGRectMake(50, 50, 300, 150) withAttributes:md];                    }@end

二:绘制图片

////  YXimage.m//  Quartz2D简单使用(二)////  Created by yeyuanxiang on 16/6/28.//  Copyright © 2016年 KuYu. All rights reserved.//#import "YXimage.h"@implementation YXimage// Only override drawRect: if you perform custom drawing.// An empty implementation adversely affects performance during animation.- (void)drawRect:(CGRect)rect {    // Drawing code            //1.加载图片到内存中    UIImage *image = [UIImage imageNamed:@"account"];         // 利用drawAsPatternInRec方法绘制图片到layer, 是通过平铺原有图片//    [image drawAsPatternInRect:CGRectMake(0, 0, 375, 666)];        // 利用drawInRect方法绘制图片到layer, 是通过拉伸原有图片//    [image drawInRect:CGRectMake(0, 0, 200, 200)];        // 将图片绘制到指定的位置    [image drawAtPoint:CGPointMake(100, 100)];    }@end

Demo链接: 点击打开链接

0 0
原创粉丝点击