在iOS开发中,常常会有一段文字显示不同的颜色和字体,或者给某几个文字加删除线或下划线的需求。之前在网上找了一些资料,有的是重绘UILabel的textLayer,有的是用html5实现的,都比较麻烦,而且很多UILabel的属性也不起作用了,效果都不理想。后来了解到NSMuttableAttstring(带属性的字符串),上面的一些需求都可以很简便的实现。

NSMuttableAttstring 方法

为某一范围内文字设置多个属性

- (void)setAttributes:(NSDictionary *)attrs range:(NSRange)range;

为某一范围内文字添加某个属性

- (void)addAttribute:(NSString *)name value:(id)value range:(NSRange)range;

为某一范围内文字添加多个属性

- (void)addAttributes:(NSDictionary *)attrs range:(NSRange)range;

移除某范围内的某个属性

- (void)removeAttribute:(NSString *)name range:(NSRange)range;

常见的属性及说明

NSFontAttributeName  字体

NSParagraphStyleAttributeName  段落格式

NSForegroundColorAttributeName  字体颜色

NSBackgroundColorAttributeName   背景颜色

NSStrikethroughStyleAttributeName 删除线格式

NSUnderlineStyleAttributeName      下划线格式

NSStrokeColorAttributeName        删除线颜色

NSStrokeWidthAttributeName 删除线宽度

NSShadowAttributeName  阴影

例子一:

   UILabel *testLabel = [[UILabel alloc]initWithFrame:CGRectMake(, , , )];
testLabel.backgroundColor = [UIColor lightGrayColor]; testLabel.textAlignment = NSTextAlignmentCenter; NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc]initWithString:@"今天天气不错呀"]; [AttributedStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:16.0] range:NSMakeRange(, )]; [AttributedStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(, )]; testLabel.attributedText = AttributedStr; [self.view addSubview:testLabel];

效果:

例子二:

        UILabel *titleView = [[UILabel alloc] init];
titleView.width = ;
titleView.height = ;
titleView.textAlignment = NSTextAlignmentCenter;
// 自动换行
titleView.numberOfLines = ;
titleView.y = ; NSString *str = [NSString stringWithFormat:@"%@\n%@", prefix, name]; // 创建一个带有属性的字符串(比如颜色属性、字体属性等文字属性)
NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:str];
// 添加属性
[attrStr addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:] range:[str rangeOfString:prefix]];
[attrStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:] range:[str rangeOfString:name]];
titleView.attributedText = attrStr;

例子二中,range:[str rangeOfString:name] 找到name 有str 所在的范围。

删除:

    NSString *marketPrice = [NSString stringWithFormat:@"¥%d",];
NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:marketPrice];
[attrStr addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlinePatternSolid | NSUnderlineStyleSingle) range:NSMakeRange(, marketPrice.length)];

文字换行

        UILabel *tips = [[UILabel alloc]initWithFrame:CGRectMake(, , kScreenWidth - , )];
[tips setTextColor:[UIColor grayColor]];
[tips setText:@"支付密码必须为6位数字组合。\n您可依次进入 '功能列表' -> '安全中心' 修改支付密码。"];
[tips setFont:[UIFont boldSystemFontOfSize:]];
tips.textAlignment = NSTextAlignmentLeft;
tips.numberOfLines = ; // 关键一句

参考博客:http://snowyshell.blog.163.com/blog/static/2209140342014475383375/

iOS UI基础-17.0 UILable之NSMutableAttributedString的更多相关文章

  1. iOS UI基础-9.0 UITableView基础

    在iOS中,要实现表格数据展示,最常用的做法就是使用UITableView.UITableView继承自UIScrollView,因此支持垂直滚动,而且性能极佳. UITableView有两种样式: ...

  2. iOS UI基础-4.0应用程序管理

    功能与界面 功能分析: 以九宫格的形式展示应用信息 点击下载按钮后,做出相应的操作 步骤分析: 加载应用信息 根据应用的个数创建对应的view 监听下载按钮点击 整个应用界面: 程序实现 思路 UI布 ...

  3. iOS UI基础-19.0 UICollectionView

    直接上代码,说明请看注释吧 1.继承三个代理 UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateF ...

  4. iOS UI基础-16.0 UIButton

    回归自然,UIButton是我们使用最频烦的一个控件.下面,对该控件的一些常用方法进行一些总结. UIButton *payStateBtn = [UIButton buttonWithType:UI ...

  5. iOS UI基础-15.0 UIWebView

    WebView介绍 知识点: 代码创建一个UIWebView OC调用html的js js页面调用OC 相关代码实现 代码创建一个UIWebView // 1.webView UIWebView *w ...

  6. iOS UI基础-13.0 数据存储

    应用沙盒 每个iOS应用都有自己的应用沙盒(应用沙盒就是文件系统目录),与其他文件系统隔离.应用必须待在自己的沙盒里,其他应用不能访问该沙盒 应用沙盒的文件系统目录,如下图所示(假设应用的名称叫Lay ...

  7. iOS UI基础-12.0 Storyboard

    storyboard创建控制器 1.先加载storyboard文件(Test是storyboard的文件名) UIStoryboard *storyboard = [UIStoryboard stor ...

  8. iOS UI基础-10.0 QQ聊天布局之键盘及文本使用

    要实现的效果:   这里只说用到的几个知识点 1.图片包含文字 在设置文字的Frame的时候,使用背景(按钮)的尺寸,文字使用了内边距 背景图片,使用拉伸 /** * 返回一张可以随意拉伸不变形的图片 ...

  9. iOS UI基础-8.0 UIAlertView使用

    弹出框的使用 1.实现代理UIAlertViewDelegate 2.弹出框 // 弹框初始化 UIAlertView *alert = [[UIAlertView alloc] initWithTi ...

随机推荐

  1. hdu 1028 Ignatius and the Princess III(DP)

    Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K ...

  2. seq 显示00 01的格式

    for i in `seq -w 00 20` ; do echo $i ;done 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 ...

  3. Hive variable demo

    create table ori_trans (account string, maker string, tdate string) partitioned by (country string); ...

  4. hadoop入门:hadoop使用shell命令总结

    第一部分:Hadoop Bin后面根据项目的实际需要Hadoop Bin  包括:Hadoop  hadoop的Shellhadoop-config.sh 它的作用是对一些变量进行赋值     HAD ...

  5. noip2014提高组day2二题题解-rLq

    (又是昨天的作业……本题写于昨天) (这破题都做这么久,我是不是吃枣药丸……) (好吧这是一道图论题呢) 本题地址:http://www.luogu.org/problem/show?pid=2296 ...

  6. 【温故而知新-CSS】使用CSS设计网站导航栏

    body #nav li a { width: auto; } #nav li a:hover { background-color: #ffcc00; color: #fff; border-rig ...

  7. 递归:codevs 1251 括号

    codevs 1251 括号  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold   题目描述 Description 计算乘法时,我们可以添加括号,来改变相乘的顺 ...

  8. 漫谈计算摄像学 (一):直观理解光场(Light Field)

    什么是计算摄像学 计算摄像学(Computational Photography)是近年来越来越受到注意的一个新的领域,在学术界早已火热.本来计算摄像学的业界应用在群众中一直没什么知名度,直到Lytr ...

  9. Javascript中的delete

    一.问题的提出 我们先来看看下面几段代码,要注意的是,以下代码不要在浏览器的开发者工具(如FireBug.Chrome Developer tool)中运行,原因后面会说明: 为什么我们可以删除对象的 ...

  10. .net程序集强命名(签名)

    要想得到强签名的dll有两种情况: 1.给项目添加强命名 在你的项目右键->属性->签名,勾选“为程序集签名”,新建 或 浏览已经新建过的.pfx文件,然后重新build项目,生成的dll ...