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]//文字颜色
};

效果

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

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

效果 :(居中)

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

三,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,//删除线,数字代表线条宽度
};

效果:

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

六,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//倾斜程度
};

效果:

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

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

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

iOS 文字属性字典的更多相关文章

  1. IOS文字属性备注

    // Predefined character attributes for text. If the key is not in the dictionary, then use the defau ...

  2. [转] iOS文字排版(CoreText)那些事儿

    文章转载自 http://www.cocoachina.com/applenews/devnews/2014/0521/8504.html iOS文字排版(CoreText)那些事儿 转自阿毛的蛋疼地 ...

  3. [BS-02] iOS数组、字典、NSNumber 新写法—— @[]、@{}

    IOS数组.字典.NSNumber 新写法—— @[].@{}   //标准写法 NSNumber * number = [NSNumber numberWithInt:]; NSArray * ar ...

  4. html5文本框提示文字属性为placeholder

    html5文本框提示文字属性为placeholder 例子:  <textarea id="comment" class="commentCont"  n ...

  5. 学习笔记TF023:下载、缓存、属性字典、惰性属性、覆盖数据流图、资源

    确保目录结构存在.每次创建文件,确保父目录已经存在.确保指定路径全部或部分目录已经存在.创建沿指定路径上不存在目录. 下载函数,如果文件名未指定,从URL解析.下载文件,返回本地文件系统文件名.如果文 ...

  6. CSS3:文字属性

    文字属性注意的细节: <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...

  7. css的基本操作学习--css样式,选择器,hover,文字属性,文本属性,背景

    什么是css? 通配符选择器 <head> /* *通配符选择器 匹配任何元素 */ *{ margin: 0; padding: 0; } </head> css样式有三种 ...

  8. day67-CSS字体属性、文字属性、背景属性、css盒子模型

    1. 字体属性 1.1 文字字体:font-family可以把多个字体名称作为一个“回退”系统来保存.如果浏览器不支持第一个字体,则会尝试下一个.浏览器会使用它可识别的第一个值. * {font-fa ...

  9. 伪元素选择器,选择器优先级,CSS修改文字属性,CSS修改字体属性,CSS修改其他属性

    伪元素选择器 未使用元素选择器的效果 第一行:伪元素选择器:选择部分内容 第二行:伪元素选择器:选择部分内容 伪元素选择器:选择部分内容 伪元素选择器:选择部分内容 ::selection:选择指定元 ...

随机推荐

  1. 用PHP开发自己的独立博客(一)——概述

    开篇废话:因为重新回归朝九晚五的生活,于是就想开始写技术博客,当是做技术文档了.于是试用了各类博客,CSDN.cnblogs都还不错.简单试用了一下,说说各自的特点. CSDN的界面不能定制,使用默认 ...

  2. LeetCode第63题--不同路径

    一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为“Start” ).机器人每次只能向下或者向右移动一步.机器人试图达到网格的右下角(在下图中标记为“Finish”).现在考虑网格中 ...

  3. Jenkins系列之Jenkins的安装(一)

    自动化测试的时候通常我们都会进行持续集成,下面是持续集成工具Jenkins的安装 Jenkins优点: 开源免费 跨平台,支持所有的平台 web形式的可视化的管理页面 安装配置超级简单 tips及时快 ...

  4. Java基础(一)--操作符

    Java底层都是使用操作符来操作Java中的数据 常见的操作符:+.-.*./.= 优先级: 当一个表达式存在多个操作符时,操作符的优先级决定了计算顺序,这点在我们刚开始学习数学的时候就会了解到 如果 ...

  5. 04JavaScript程序语句

    JavaScript程序语句 2.6程序控制流程 2.6.1选择结构 if <逻辑表达式> 语句 else 语句 if <逻辑表达式> { 语句组 } else { 语句组} ...

  6. (转)Oracle数据库DBA必备基本技能

    [Oracle数据库DBA必备基本技能] shutdown Normal 需要等待所有的用户断开连接 Immediate 等待用户完成当前的语句 Transactional    等待用户完成当前的事 ...

  7. 洛谷——P2094 运输

    P2094 运输 题目描述 现在已知N件商品,和搬运它们其中每一件的费用.现在搬家公司老板Mr.sb决定让我们每次任意选取2件商品.然后这2件商品只算一件商品的费用.但是这个商品的搬运费用是将选出的2 ...

  8. (C/C++学习)20.基于C++改进的单目标遗传算法

    说明:在学习生活中,经常会遇到各种各样的最优问题,其中最常见的就是求某个多维(多个自变量)函数在各个自变量各取何值时的最大值或最小值:例如求函数 f(x) = (x-5)2+(y-6)2+(z-7)2 ...

  9. Gym - 101670B Pond Cascade(CTU Open Contest 2017 贪心,二分)

    题目: The cascade of water slides has been installed in the park recently and it has to be tested. The ...

  10. 爬虫之pyquery库

    官方文档:https://pyquery.readthedocs.io/en/latest/ PyQuery是一个强大又灵活的网页解析库.如果你觉得正则写起来太麻烦.BeautifulSoup语法太难 ...