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

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

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

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

我们使用是一个NSString 的方法

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

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

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

置字体颜色

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

效果

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

二,NSParagraphStyleAttributeName

段落格式

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

效果 :(居中)

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

三,NSBackgroundColorAttributeName

背景色

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

效果:

四,NSStrokeColorAttributeName

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

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

效果:

五。NSStrikethroughStyleAttributeName

删除线

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

效果:

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

六,NSUnderlineStyleAttributeName

下划线

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

效果:

七,NSShadowAttributeName

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

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

NSShadow

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

效果:

八,NSObliquenessAttributeName

倾斜

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

效果:

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. ajax怎么理解?

    Ajix是创建交互式网页的前端网页开发技术,不是一种语言,ajax是基于http来传输数据的,他是利用浏览器提供操作http的接口(XMLHttpRequest或者activeXobject),来操作 ...

  2. 动态调用链接库(dll) 续

    20141118 最近一周做了一个关于仓库管理,拣货任务分配的模块,其中涉及到刷卡自动打印领取任务的功能点. 技术点: C#调用C++.delphi的动态链接库.动态链接库的调用方法不同.效果也不相同 ...

  3. select count(1) 和 select count(*)的区别

    统计一个表T有多少行数据,通常写法是: 查询A:select count(*) from T 但也可以采用下面语句来查: 查询B:select count(1) from T 结果通常是一样的.那么二 ...

  4. Linux 学习(二)

    Linux相关命令 命令 说明 startx 当前用户界面切换至图形界面 init5 切换至另一用户的图形化界面 init3 从图形界面切换回文本界面 pwd 显示当前用户路径 logout 注销 s ...

  5. MFC_2.1使用单选和多选框

    使用单选和多选框 单选 1.拖控件 设置名字,CTRL+D设置顺序,属性设置第一个GROUP为TRUE: 2.设置第一个按钮绑定变量为 值 INT型 名称m_RadioIndxe; 3.设置单击响应内 ...

  6. CAD得到所有组名(网页版)

    主要用到函数说明: _DMxDrawX::GetAllGroupName 得到所有组名. js代码实现如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 ...

  7. 浅谈es6 promise

    本文是借鉴于ac黄的博客. 接触es6也有几个月了,貌似没有系统的去学习过它,总是用到什么,查查什么.今天就说下es6中的promise对象. 先说说promise解决了什么问题? 写前端的同学都经常 ...

  8. 深入分析同步工具类之AbstractQueuedSynchronizer

      概览: AQS(简称)依赖内部维护的一个FIFO(先进先出)队列,可以很好的实现阻塞.同步:volatile修饰的属性state,哪个线程先改变这个状态值,那么这个线程就获得了优先权,可以做任何事 ...

  9. keep

    简介 什么是keepalived呢?keepalived是实现高可用的一种轻量级的技术手段,主要用来防止单点故障(单点故障是指一旦某一点出现故障就会导致整个系统架构的不可用)的发生.之所以说keepa ...

  10. 一篇入门MongoDB

    目录 1.MongoDB 基本介绍 2.MongoDB 基本概念 3.数据库操作 4.集合操作 5.文档操作 6.查询条件 7.索引 1.MongoDB 基本介绍 (1)安装 MongoDB 简单来说 ...