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]//文字颜色
};
效果
![]()
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 文字属性字典的更多相关文章
- IOS文字属性备注
// Predefined character attributes for text. If the key is not in the dictionary, then use the defau ...
- [转] iOS文字排版(CoreText)那些事儿
文章转载自 http://www.cocoachina.com/applenews/devnews/2014/0521/8504.html iOS文字排版(CoreText)那些事儿 转自阿毛的蛋疼地 ...
- [BS-02] iOS数组、字典、NSNumber 新写法—— @[]、@{}
IOS数组.字典.NSNumber 新写法—— @[].@{} //标准写法 NSNumber * number = [NSNumber numberWithInt:]; NSArray * ar ...
- html5文本框提示文字属性为placeholder
html5文本框提示文字属性为placeholder 例子: <textarea id="comment" class="commentCont" n ...
- 学习笔记TF023:下载、缓存、属性字典、惰性属性、覆盖数据流图、资源
确保目录结构存在.每次创建文件,确保父目录已经存在.确保指定路径全部或部分目录已经存在.创建沿指定路径上不存在目录. 下载函数,如果文件名未指定,从URL解析.下载文件,返回本地文件系统文件名.如果文 ...
- CSS3:文字属性
文字属性注意的细节: <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...
- css的基本操作学习--css样式,选择器,hover,文字属性,文本属性,背景
什么是css? 通配符选择器 <head> /* *通配符选择器 匹配任何元素 */ *{ margin: 0; padding: 0; } </head> css样式有三种 ...
- day67-CSS字体属性、文字属性、背景属性、css盒子模型
1. 字体属性 1.1 文字字体:font-family可以把多个字体名称作为一个“回退”系统来保存.如果浏览器不支持第一个字体,则会尝试下一个.浏览器会使用它可识别的第一个值. * {font-fa ...
- 伪元素选择器,选择器优先级,CSS修改文字属性,CSS修改字体属性,CSS修改其他属性
伪元素选择器 未使用元素选择器的效果 第一行:伪元素选择器:选择部分内容 第二行:伪元素选择器:选择部分内容 伪元素选择器:选择部分内容 伪元素选择器:选择部分内容 ::selection:选择指定元 ...
随机推荐
- Java多线程——线程之间的同步
Java多线程——线程之间的同步 摘要:本文主要学习多线程之间是如何同步的,如何使用volatile关键字,如何使用synchronized修饰的同步代码块和同步方法解决线程安全问题. 部分内容来自以 ...
- web安全测试--XSS(跨站脚本)与CSRF
XSS攻击原理 反射型 发出请求时,xss代码出现在URL中,作为输入提交到服务器端,服务器端解析后响应,xss代码随响应内容一起传回浏览器,最后浏览器解析执行xss代码.这个过程像一次反射,故叫反射 ...
- 三维重建:GitHub百度Apollo 2.0
GitHub:https://github.com/ApolloAuto/apollo 1. 关于Apollo的数据:Apollo的数据会如何开放? 自动驾驶数据将包括具有高分辨率图像和像素级别标注的 ...
- 场景分割:MIT Scene Parsing 与DilatedNet 扩展卷积网络
MIT Scene Parsing Benchmark简介 Scene parsing is to segment and parse an image into different image re ...
- OpenTSDB监控
OpenTSDB监控
- Explaining difference between automaticallyAdjustsScrollViewInsets, extendedLayoutIncludesOpaqueBars, edgesForExtendedLayout
automaticallyAdjustsScrollViewInsets:在导航栏透明时用到 In your viewDidLoad method, add if([self respondsToSe ...
- TP中U方法详解
U方法常用于ThinkPHP里的页面跳转 官方称为url组装, 就是根据某种规则组成一个url地址,这个功能就叫组装. 在ThinkPHP里,系统提供了一个封装的函数来处理url的组装,俗称U方法. ...
- gearman的安装与使用
Gearman是一个分发任务的程序框架,它会对作业进行排队自动分配到一系列机器上.gearman跨语言跨平台,很方便的实现异步后台任务.php官方收录:http://php.net/manual/zh ...
- Luogu P2847 [USACO20DEC]Moocast(gold)奶牛广播-金
解题思路 要保证图是强连通的,用因为给出的边全部都是双向边.考虑树形的结构,在一棵树上的$N$个节点一定是强连通的.他们都能够互相到达.又要保证树上的$n-1$条边中的最长的一条边最小.那就用Krus ...
- Linux修改系统时间与时区
GMT (Greewich Mean Time) 格林威治标准时间:GMT是老的时间计量标准,根据地球的自转和公转来计算时间,也就是太阳每天经过位于英国伦敦郊区的皇家格林尼治天文台的标准时间就是中午 ...