美文网首页
drawInRect: withAttributes: 等新方法

drawInRect: withAttributes: 等新方法

作者: DestinyFighter_ | 来源:发表于2015-11-27 16:30 被阅读2070次

    新的方法采用的是 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: 等新方法

          本文链接:https://www.haomeiwen.com/subject/atvdhttx.html