NSMutableAttributedString的使用

有时候我们写UI的时候经常会遇到这样的情况  就是图片和文字离的较近  或者文字排版在一行但是字体大小或者颜色不一样 这时候如果写不同的Label或者是不同的控件就会很麻烦 。 我们经常会使用NSMutableAttributedString来简化

总结经常使用的NSMutableAttributedString一些样式

     NSFontAttributeName;(字体)

     NSParagraphStyleAttributeName;(段落)

     NSForegroundColorAttributeName;(字体颜色)

     NSBackgroundColorAttributeName;(字体背景色)

     NSLigatureAttributeName;(连字符)

     NSKernAttributeName;(字间距)

     NSStrikethroughStyleAttributeName;(删除线) 

     NSUnderlineStyleAttributeName;(下划线) 

     NSStrokeColorAttributeName;(边线颜色)

     NSStrokeWidthAttributeName;(边线宽度) 

     NSShadowAttributeName;(阴影)(横竖排版)

     NSFontAttributeName;(字体)
     NSParagraphStyleAttributeName;(段落)
     NSForegroundColorAttributeName;(字体颜色)
     NSBackgroundColorAttributeName;(字体背景色)
     NSLigatureAttributeName;(连字符)
     NSKernAttributeName;(字间距)
     NSStrikethroughStyleAttributeName;(删除线) 
     NSUnderlineStyleAttributeName;(下划线) 
     NSStrokeColorAttributeName;(边线颜色)
     NSStrokeWidthAttributeName;(边线宽度) 
     NSShadowAttributeName;(阴影)(横竖排版)

 

//图文混排
    NSMutableAttributedString *attText =[[NSMutableAttributedString alloc] init];
//设置文本样式
    NSMutableDictionary *dic1 =[NSMutableDictionary dictionary];
    dic1[NSUnderlineStyleAttributeName] =@1;
    NSAttributedString *first =[[NSAttributedString alloc] initWithString:@"hello" attributes:dic1];
    [attText appendAttributedString:first];
//设置文本样式
    NSMutableDictionary *dic =[NSMutableDictionary dictionary];
    dic[NSFontAttributeName] =[UIFont systemFontOfSize:20];
    
    dic[NSForegroundColorAttributeName] =[UIColor orangeColor];
    NSAttributedString *third =[[NSAttributedString alloc] initWithString:@"这是一个实验" attributes:dic];
    [attText appendAttributedString:third];
    
    label.attributedText =attText;

 

可以用这个NSTextAttachment类来设置图片和文字的混合添加  

NSTextAttachment *text =[[NSTextAttachment alloc] init];
    text.image =[UIImage imageNamed:@"视频1.png"];

 

posted @ 2017-02-16 22:52  wangxiaoqi  阅读(298)  评论(0编辑  收藏  举报