UIKit绘图

绘制文字

- (void)drawString:(NSString*)str rect:(CGRect)rect
{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];

    //设置字体大小
    dict[NSFontAttributeName] = [UIFont systemFontOfSize:30];

    //设置颜色
    dict[NSForegroundColorAttributeName] = [UIColor redColor];

    //设置笔触
    dict[NSStrokeColorAttributeName] = [UIColor greenColor];
    dict[NSStrokeWidthAttributeName] = @2;

    //设置阴影
    NSShadow *shadow = [[NSShadow alloc]init];

    //设置阴影颜色
    shadow.shadowColor = [UIColor blackColor];

    //设置阴影偏移量
    shadow.shadowOffset = CGSizeMake(2, 2);

    //设置阴影模糊
    shadow.shadowBlurRadius = 2;

    dict[NSShadowAttributeName] = shadow;

    //[str drawAtPoint:CGPointZero withAttributes:dict];//不支持自动换行

    [str drawInRect:rect withAttributes:dict];//支持自动换行
}

绘制图片

- (void)drawImage:(UIImage *)image rect:(CGRect)rect
{
    UIImage *drawImage = image;

    //可以用如下方法对图片进行裁剪,但一定要在绘制之前执行
    //UIRectClip(CGRectMake(0, 0, 50, 50));

    [drawImage drawAtPoint:CGPointZero];//以原图片的尺寸绘制填充rect

    //[drawImage drawInRect:rect];//自适应rect绘制

    //[drawImage drawAsPatternInRect:rect];//以平铺的方式绘制
}

results matching ""

    No results matching ""