属性化字符串问题集
2015-11-11 00:00
337 查看
摘要: iOS
属性化字符串,用到的时候问题一大堆,一一记下。
UITextView , UILabel , 都可以使用属性化字符串.
以UILabel为例,如下代码.
3.在UITextView 中涉及到换行,要加\n,对属性化字符串一样有效
4.顺便说下,UITextView顶部空白,需要设置self.automaticallyAdjustsScrollViewInsets = NO;
5.属性化字符串 属性列举
NSString *const NSFontAttributeName;(字体)
NSString *const NSParagraphStyleAttributeName;(段落)
NSString *const NSForegroundColorAttributeName;(字体颜色)
NSString *const NSBackgroundColorAttributeName;(字体背景色)
NSString *const NSLigatureAttributeName;(连字符)
NSString *const NSKernAttributeName;(字间距)
NSString *const NSStrikethroughStyleAttributeName;(删除线)
NSString *const NSUnderlineStyleAttributeName;(下划线)
NSString *const NSStrokeColorAttributeName;(边线颜色)
NSString *const NSStrokeWidthAttributeName;(边线宽度)
NSString *const NSShadowAttributeName;(阴影)(横竖排版)
NSString *const NSVerticalGlyphFormAttributeName;
属性化字符串,用到的时候问题一大堆,一一记下。
UITextView , UILabel , 都可以使用属性化字符串.
以UILabel为例,如下代码.
//函数作用:添加统计数据label; 参数text是数据(包括单位),如:3600分; 参数unit是单位,如: 分 - (void)addLabelBy:(CGRect)frame withText:(NSString *)text inView:(UIView *)view unit:(NSString *)unit size:(CGFloat)high{ //属性化字符串 NSMutableAttributedString *atrString=[[NSMutableAttributedString alloc]initWithString:text]; NSUInteger length=[atrString length]; //不同字体 UIFont *dataFont=[UIFont fontWithName:@"Georgia-Bold" size:high]; UIFont *unitFont=[UIFont systemFontOfSize:high/2]; //把数据和单位设置成 不同字体和颜色 [atrString addAttribute:NSFontAttributeName value:dataFont range:NSMakeRange(0, length)]; [atrString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, length)]; [atrString addAttribute:NSFontAttributeName value:unitFont range:[text rangeOfString:unit]]; [atrString addAttribute:NSForegroundColorAttributeName value:[UIColor brownColor] range:[text rangeOfString:unit]]; //添加label UILabel *label=[[UILabel alloc]initWithFrame:frame]; label.attributedText=atrString; label.textAlignment=NSTextAlignmentCenter; [view addSubview:label]; }
3.在UITextView 中涉及到换行,要加\n,对属性化字符串一样有效
4.顺便说下,UITextView顶部空白,需要设置self.automaticallyAdjustsScrollViewInsets = NO;
5.属性化字符串 属性列举
NSString *const NSFontAttributeName;(字体)
NSString *const NSParagraphStyleAttributeName;(段落)
NSString *const NSForegroundColorAttributeName;(字体颜色)
NSString *const NSBackgroundColorAttributeName;(字体背景色)
NSString *const NSLigatureAttributeName;(连字符)
NSString *const NSKernAttributeName;(字间距)
NSString *const NSStrikethroughStyleAttributeName;(删除线)
NSString *const NSUnderlineStyleAttributeName;(下划线)
NSString *const NSStrokeColorAttributeName;(边线颜色)
NSString *const NSStrokeWidthAttributeName;(边线宽度)
NSString *const NSShadowAttributeName;(阴影)(横竖排版)
NSString *const NSVerticalGlyphFormAttributeName;
相关文章推荐
- Android Studio Gradle 导出JavaDocJar提示编码GBK的字符无法映射解决
- 排序算法
- 微信 invalid signature 处理
- git push fatal: The remote end hung up unexpectedl
- 无参构造方法 ? 反射
- Android数据库小记
- MAC下SVN的常用命令
- IOS开发--循环引用问题,普通控件为什么用weak,代理为什么用weak,block内用到外面的东
- HTTP Status 500 - Unable to compile class for JSP
- Javascript 面向对象编程
- centos防火墙开发新端口
- Hadoop2.7.1配置NameNode+ResourceManager高可用原理分析
- SELinux下安装MariaDB/MySQL,并自定义数据库目录
- asp.net web 自定义控件
- 搭建CAS单点登录服务器
- Java课程设计笔记-Android系统获取所有存储器路径
- txt文件导入数据库的utf-8问题
- 结构型模式——代理模式
- Java基本数据类型与四个运算
- 【转】使用Core Graphics绘画一个山寨微信icon