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];
}
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