zoukankan      html  css  js  c++  java
  • iOS 文字属性字典

    iOS开发过程中相信大家常常遇到当须要给字体,颜色,下划线等属性的时候參数是一个NSDictionary 字典

    可是字典里面究竟有哪些键值对了

    我们把经常使用的总结一下


    首先我们创建一个最简单的。设置一下字体和大小

    我们使用是一个NSString 的方法

    - (void)drawInRect:(CGRect)rect withAttributes:(NSDictionary *)attrs

    来将一个字符串打印到view上

    -(void)drawRect:(CGRect)rect
    {
        self.backgroundColor=[UIColor whiteColor];
        NSString *attrString =@"hello word";
        
        NSDictionary* attrs =@{NSFontAttributeName:[UIFont fontWithName:@"AmericanTypewriter" size:30]
                               }; //在词典中增加文本的字体 大小
        
        [attrString drawInRect:CGRectMake(20,120,320,200)withAttributes:attrs];
    }


    置字体颜色


        NSDictionary* attrs =@{NSFontAttributeName:[UIFont fontWithName:@"AmericanTypewriter" size:30],//文本的颜色 字体 大小
                               NSForegroundColorAttributeName:[UIColor redColor]//文字颜色
                               };




    效果

    二,NSParagraphStyleAttributeName

    段落格式

    -(void)drawRect:(CGRect)rect
    {
        NSString *attrString =@"hello word";
        
        NSMutableParagraphStyle *paragraph=[[NSMutableParagraphStyle alloc]init];
        paragraph.alignment=NSTextAlignmentCenter;//居中
        
        NSDictionary* attrs =@{NSFontAttributeName:[UIFont fontWithName:@"AmericanTypewriter" size:30],//文本的颜色 字体 大小
                               NSForegroundColorAttributeName:[UIColor redColor],//文字颜色
                               NSParagraphStyleAttributeName:paragraph,//段落格式
                               };
        
        [attrString drawInRect:CGRectMake(20,120,320,200)withAttributes:attrs];
    }




    效果 :(居中)

    三,NSBackgroundColorAttributeName

    背景色


     NSDictionary* attrs =@{NSFontAttributeName:[UIFont fontWithName:@"AmericanTypewriter" size:30],//文本的颜色 字体 大小
                               NSForegroundColorAttributeName:[UIColor redColor],//文字颜色
                               NSParagraphStyleAttributeName:paragraph,//段落格式
                               NSBackgroundColorAttributeName:[UIColor blueColor],//背景色
                               };



    效果:




    四,NSStrokeColorAttributeName

    设置描边颜色,须要和NSStrokeWidthAttributeName 一起使用

        NSDictionary* attrs =@{NSFontAttributeName:[UIFont fontWithName:@"AmericanTypewriter" size:30],//文本的颜色 字体 大小
                               NSForegroundColorAttributeName:[UIColor redColor],//文字颜色
                               NSParagraphStyleAttributeName:paragraph,//段落格式
                               //NSBackgroundColorAttributeName:[UIColor blueColor],//背景色
                               NSStrokeWidthAttributeName:@3, //描边宽度
                               NSStrokeColorAttributeName:[UIColor greenColor],//设置 描边颜色。和NSStrokeWidthAttributeName配合使用,设置了这个NSForegroundColorAttributeName就失效了
                               };



    效果:

    五。NSStrikethroughStyleAttributeName

    删除线

        NSDictionary* attrs =@{NSFontAttributeName:[UIFont fontWithName:@"AmericanTypewriter" size:30],//文本的颜色 字体 大小
                               NSForegroundColorAttributeName:[UIColor redColor],//文字颜色
                               NSParagraphStyleAttributeName:paragraph,//段落格式
    //                           NSBackgroundColorAttributeName:[UIColor blueColor],//背景色
                               NSStrokeWidthAttributeName:@3, //描边宽度
                               NSStrokeColorAttributeName:[UIColor greenColor],//设置 描边颜色,和NSStrokeWidthAttributeName配合使用,设置了这个NSForegroundColorAttributeName就失效了
                            
                               NSStrikethroughStyleAttributeName:@1,//删除线,数字代表线条宽度
                               };



    效果:


    六,NSUnderlineStyleAttributeName

    下划线

     NSDictionary* attrs =@{NSFontAttributeName:[UIFont fontWithName:@"AmericanTypewriter" size:30],//文本的颜色 字体 大小
                               NSForegroundColorAttributeName:[UIColor redColor],//文字颜色
                               NSParagraphStyleAttributeName:paragraph,//段落格式
    //                           NSBackgroundColorAttributeName:[UIColor blueColor],//背景色
                               NSStrokeWidthAttributeName:@3, //描边宽度
                               NSStrokeColorAttributeName:[UIColor greenColor],//设置 描边颜色。和NSStrokeWidthAttributeName配合使用,设置了这个NSForegroundColorAttributeName就失效了
                            
    //                           NSStrikethroughStyleAttributeName:@1,//删除线,数字代表线条宽度
                               NSUnderlineStyleAttributeName:@(NSUnderlineStyleSingle),//下划线,值为一个枚举类型,大家能够分别试试
                               };


    效果:

    七,NSShadowAttributeName

    设置阴影。他的对象是一个NSShadow的对象


        NSDictionary* attrs =@{NSFontAttributeName:[UIFont fontWithName:@"AmericanTypewriter" size:30],//文本的颜色 字体 大小
                               NSForegroundColorAttributeName:[UIColor redColor],//文字颜色
                               NSParagraphStyleAttributeName:paragraph,//段落格式
    //                           NSBackgroundColorAttributeName:[UIColor blueColor],//背景色
                               NSStrokeWidthAttributeName:@3, //描边宽度
                               NSStrokeColorAttributeName:[UIColor greenColor],//设置 描边颜色。和NSStrokeWidthAttributeName配合使用,设置了这个NSForegroundColorAttributeName就失效了
                            
    //                           NSStrikethroughStyleAttributeName:@1,//删除线,数字代表线条宽度
                               NSUnderlineStyleAttributeName:@(NSUnderlineStyleSingle),//下划线,值为一个枚举类型,大家能够分别试试
                               NSShadowAttributeName:shadow,//设置阴影。复制为一个NSShadow 的对象
                               
                               };


    NSShadow

        NSShadow *shadow=[[NSShadow alloc]init];
        shadow.shadowBlurRadius=5;//阴影的模糊程度
        shadow.shadowColor=[UIColor blueColor];//阴影颜色
        shadow.shadowOffset=CGSizeMake(6, 6);//阴影相对原来的偏移




    效果:


    八,NSObliquenessAttributeName

    倾斜

        NSDictionary* attrs =@{NSFontAttributeName:[UIFont fontWithName:@"AmericanTypewriter" size:30],//文本的颜色 字体 大小
                               NSForegroundColorAttributeName:[UIColor redColor],//文字颜色
                               NSParagraphStyleAttributeName:paragraph,//段落格式
    //                           NSBackgroundColorAttributeName:[UIColor blueColor],//背景色
                               NSStrokeWidthAttributeName:@3, //描边宽度
                               NSStrokeColorAttributeName:[UIColor greenColor],//设置 描边颜色,和NSStrokeWidthAttributeName配合使用,设置了这个NSForegroundColorAttributeName就失效了
                            
    //                           NSStrikethroughStyleAttributeName:@1,//删除线。数字代表线条宽度
                               NSUnderlineStyleAttributeName:@(NSUnderlineStyleSingle),//下划线。值为一个枚举类型。大家能够分别试试
                               NSShadowAttributeName:shadow,//设置阴影,复制为一个NSShadow 的对象
                               NSObliquenessAttributeName:@1//倾斜程度
                               };



    效果:


    这些经常使用的我们做了整理,另一些没做整理。大家有兴趣能够研究,欢迎加群和大家讨论。

    苹果开发群 :414319235  欢迎增加  欢迎讨论问题

  • 相关阅读:
    剑指Offer-11.二进制中1的个数(C++/Java)
    剑指Offer-10.矩形覆盖(C++/Java)
    剑指Offer-9.变态跳台阶(C++/Java)
    UVA 1608 Non-boring sequence 不无聊的序列(分治,中途相遇)
    UVA1607 Gates 与非门电路 (二分)
    UVA 1451 Average平均值 (数形结合,斜率优化)
    UVA 1471 Defense Lines 防线 (LIS变形)
    UVA 1606 Amphiphilic Carbon Molecules 两亲性分子 (极角排序或叉积,扫描法)
    UVA 11134 FabledRooks 传说中的车 (问题分解)
    UVA 1152 4 Values Whose Sum is Zero 和为0的4个值 (中途相遇)
  • 原文地址:https://www.cnblogs.com/yjbjingcha/p/6912370.html
Copyright © 2011-2022 走看看