drawInRect: withAttributes: 等新方法的使用

新的方法采用的是 attributes,attributes参数需要的是一个数组.

我们常用的有几个:

NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];    

paragraphStyle.lineBreakMode =NSLineBreakByCharWrapping;

NSDictionary *dic=@{

NSFontAttributeName:font,

NSParagraphStyleAttributeName:paragraphStyle,

NSForegroundColorAttributeName:[UIColorlightGrayColor]

};

通过设置 NSParagraphStyleAttributeName,我们可以设置string的段落风格,比如 :

lineBreakMode 段落风格

NSForegroundColorAttributeName 字体颜色

eg:

原先可以这样写:

CGSizesize = [text sizeWithFont:font constrainedToSize:CGSizeMake(300, MAXFLOAT) lineBreakMode:NSLineBreakByCharWrapping];

[text drawInRect:CGRectMake(10,10,300,size.height) withFont:font lineBreakMode:NSLineBreakByCharWrapping];

iOS7 之后需要切换新方法,如下:

NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];    

paragraphStyle.lineBreakMode = NSLineBreakByCharWrapping;    

NSDictionary *attribute=@{

NSFontAttributeName:font,

NSParagraphStyleAttributeName:paragraphStyle

};    

CGSizesize= [text boundingRectWithSize:CGSizeMake(300, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:attribute context:nil].size;    

[text drawWithRect:CGRectMake(10,10,300,size.height) options:NSStringDrawingUsesLineFragmentOrigin attributes:attribute context:nil];

你可能感兴趣的:(drawInRect: withAttributes: 等新方法的使用)