XMG Quartz2D 绘制文字和图片

1.

label的sizeToFit 只可以用于普通文本,如果是富文本的话则达不到要求

2.

-(void)drawRect:(CGRect)rect{

    

    NSString*str=@"eersagaa";

    

    NSMutableDictionary*dict=[NSMutableDictionary dictionary];

    dict[NSForegroundColorAttributeName]=[UIColor redColor];

    dict[NSFontAttributeName]=[UIFont systemFontOfSize:30];

    dict[NSUnderlineStyleAttributeName]=@1;

    //绘制边的颜色

    dict[NSStrokeColorAttributeName]=[UIColor yellowColor];

    dict[NSStrokeWidthAttributeName]=@3;

    NSShadow*shadow=[[NSShadow alloc]init];

    shadow.shadowOffset=CGSizeMake(20, 20);

    shadow.shadowColor=[UIColor greenColor];

    //模糊区域

    shadow.shadowBlurRadius=3;

    dict[NSShadowAttributeName]=shadow;

    

    

    [str drawAtPoint:CGPointZero withAttributes:dict];

    

}


3.绘制图片相关

    UIImage *image = [UIImage imageNamed:@"001"];

  
      //默认绘制的尺寸和原图一样大

//    [image drawAtPoint:CGPointZero];

 

  //在指定的范围内绘图  

//    [image drawInRect:rect];


 // 绘图 平铺

    [image drawAsPatternInRect:rect];


 // 超出裁剪区域的内容全部裁剪掉

    // 注意:裁剪必须放在绘制之前

    UIRectClip(CGRectMake(0, 0, 50, 50));







源码如下

#import "DrawView.h"


@implementation DrawView



// Only override drawRect: if you perform custom drawing.

// An empty implementation adversely affects performance during animation.

- (void)drawRect:(CGRect)rect {

    // Drawing code

    

    // 超出裁剪区域的内容全部裁剪掉

    // 注意:裁剪必须放在绘制之前

    UIRectClip(CGRectMake(0, 0, 50, 50));

    

    UIImage *image = [UIImage imageNamed:@"001"];

    

    // 默认绘制的内容尺寸跟图片尺寸一样大

//    [image drawAtPoint:CGPointZero];

      //绘制在指定的范围内

//    [image drawInRect:rect];

    // 绘图 平铺

    [image drawAsPatternInRect:rect];

    

}


- (void)awakeFromNib{

//    UIImage *image = [UIImage imageNamed:@"001"];

//    

//    // 默认绘制的内容尺寸跟图片尺寸一样大

//    //    [image drawAtPoint:CGPointZero];

//    

//    

//    //    [image drawInRect:rect];

//    // 绘图 平铺

//    [image drawAsPatternInRect:self.bounds];

}


- (void)drawText

{

    // 绘制文字

    

    NSString *str = @"asfdsfsdfasfdsfsdfasfdsfsdfasfdsfsdfasfdsfsdfasfdsfsdfasfdsfsdfasfdsfsdfasfdsfsdfasfdsfsdfasfdsfsdfasfdsfsdfasfdsfsdf";

    // 不会换行

    //    [str drawAtPoint:CGPointZero withAttributes:nil];

    

    [str drawInRect:self.bounds withAttributes:nil];


}

- (void)attrText

{

    

    // 绘制文字

    

    NSString *str = @"asfdsfsdf";

    

    

    // 文字的起点

    // Attributes:文本属性


    NSMutableDictionary *textDict = [NSMutableDictionary dictionary];


    // 设置文字颜色

    textDict[NSForegroundColorAttributeName] = [UIColor redColor];


    // 设置文字字体

    textDict[NSFontAttributeName] = [UIFont systemFontOfSize:30];


    // 设置文字的空心颜色和宽度


    textDict[NSStrokeWidthAttributeName] = @3;


    textDict[NSStrokeColorAttributeName] = [UIColor yellowColor];


    // 创建阴影对象

    NSShadow *shadow = [[NSShadow alloc] init];

    shadow.shadowColor = [UIColor greenColor];

    shadow.shadowOffset = CGSizeMake(4, 4);

    shadow.shadowBlurRadius = 3;

    textDict[NSShadowAttributeName] = shadow;


    // 富文本:给普通的文字添加颜色,字体大小

    [str drawAtPoint:CGPointZero withAttributes:textDict];

    }



@end







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值