在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. 用pygame学习初级python(一) 15.4.19

    最近有计划要学一下python,主要是要用flask.django一些框架进行后端的学习工作,但是在web应用之前希望进行一些基础的项目进行一些语法的练习,熟悉一下写法, 这个时候我就想先做几个小游戏 ...

  2. python实现插入排序

    代码如下@.·.@ # *-* coding: utf- *-* if __name__ == '__main__': def insert_sort(l): ,len(l)): tmp = l[i] ...

  3. 使用jsonpath解析json内容

    JsonPath提供的json解析非常强大,它提供了类似正则表达式的语法,基本上可以满足所有你想要获得的json内容.下面我把官网介绍的每个表达式用代码实现,可以更直观的知道该怎么用它. 一.首先需要 ...

  4. Django数据库怎么给字段设置主键

    id = models.IntegerField(primary_key = True) 附: null :缺省设置为false.通常不将其用于字符型字段上,比如CharField,TextField ...

  5. HDU 4819 Mosaic --二维线段树(树套树)

    题意: 给一个矩阵,每次查询一个子矩阵内的最大最小值,然后更新子矩阵中心点为(Max+Min)/2. 解法: 由于是矩阵,且要求区间最大最小和更新单点,很容易想到二维的线段树,可是因为之前没写过二维的 ...

  6. java 10-4 Scanner方法

    Scanner:用于接收键盘录入数据  常用的两个方法(int举例): public int nextInt():获取一个int类型的值 public String nextLine():获取一个St ...

  7. brainfuck

    /阅读这样的代码就像在强奸你的大脑 #include<stdio.h> #include<ctype.h> #include<stdlib.h>  #include ...

  8. css 字体不撑开默认块级元素问题

    问题原因是行高的元素没有随字体大小而改变,设置line-hight属性和字体同时变换

  9. flex布局滑动页面

    html: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...

  10. 挂多个class还是新建class —— 多用组合,少用继承

    用css实现下面的效果图. 方案一 <style type="text/css"> .myList1 { border: 1px solid #333; padding ...