以前看到这种字号和颜色不一样的字符串,想出个讨巧的办法就是“¥150”一个UILabel,“元/位”一个UILabel。今天翻看以前的工程,command点进UITextField中看到[attributedText]这个关键字,以前都没注意过UITextField还有这个属性,其实UITextView、UILable也有这个属性,iOS6就已经有了,说来惭愧,对此罚站1秒钟。

NSAttributedString叫做富文本,是一种带有属性的字符串,通过它可以轻松的在一个字符串中表现出多种字体、字号、字体大小等各不相同的风格,还可以对段落进行格式化。

通过以下代码即可实现上面图示效果,十分方便,从此再也不用设置两个UILable,并且处心积虑的处理它们的长度了。

     UILabel * aLable = [[UILabel alloc] initWithFrame:CGRectMake(, , , )];
aLable.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:aLable]; NSString * aString = @"¥150 元/位"; //富文本对象
NSMutableAttributedString * aAttributedString = [[NSMutableAttributedString alloc] initWithString:aString]; //富文本样式
[aAttributedString addAttribute:NSForegroundColorAttributeName //文字颜色
value:[UIColor redColor]
range:NSMakeRange(, )]; [aAttributedString addAttribute:NSFontAttributeName //文字字体
value:[UIFont systemFontOfSize:]
range:NSMakeRange(, )]; aLable.attributedText = aAttributedString;

常用属性:

NSFontAttributeName           文字字体

NSParagraphStyleAttributeName     段落样式(字符串通过“\n”进行分段,此设置必须在lable.numberOfLines = 0时有效,value通过NSMutableParagraphStyle设置,它有以下属性)

 [段落样式-插曲]
1 @property(readwrite) CGFloat lineSpacing;              //行间距
@property(readwrite) CGFloat paragraphSpacing;           //段间距
@property(readwrite) NSTextAlignment alignment;           //对齐方式
@property(readwrite) CGFloat firstLineHeadIndent;          //首行缩进
@property(readwrite) CGFloat headIndent;               //除首行之外其他行缩进
@property(readwrite) CGFloat tailIndent;               //每行容纳字符的宽度
@property(readwrite) NSLineBreakMode lineBreakMode;        //换行方式
@property(readwrite) CGFloat minimumLineHeight;           //最小行高
@property(readwrite) CGFloat maximumLineHeight;           //最大行高
@property(readwrite) NSWritingDirection baseWritingDirection;  //书写方式(NSWritingDirectionNatural,NSWritingDirectionLeftToRight,NSWritingDirectionRightToLeft)
@property(readwrite) CGFloat lineHeightMultiple;
@property(readwrite) CGFloat paragraphSpacingBefore;
@property(readwrite) float hyphenationFactor;
@property(readwrite,copy,NS_NONATOMIC_IOSONLY) NSArray *tabStops NS_AVAILABLE_IOS(7_0);
@property(readwrite,NS_NONATOMIC_IOSONLY) CGFloat defaultTabInterval NS_AVAILABLE_IOS(7_0);
 [段落样式demo]
1 UILabel * lable = [[UILabel alloc] initWithFrame:CGRectMake(, , self.view.frame.size.width-, )];
lable.backgroundColor = [UIColor lightGrayColor];
lable.numberOfLines = ;
[self.view addSubview:lable]; NSString * string = @"Always believe that something wonderful is about \nto happen!"; //富文本
NSMutableAttributedString * attributedString = [[NSMutableAttributedString alloc] initWithString:string]; //段落样式
NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init]; #warning lable.numberOfLines必须为0,段落样式才生效
//行间距
paragraphStyle.lineSpacing = 10.0;
//段落间距
paragraphStyle.paragraphSpacing = 20.0; // paragraphStyle.baseWritingDirection = NSWritingDirectionLeftToRight;
// paragraphStyle.firstLineHeadIndent = 10.0;
// paragraphStyle.headIndent = 50.0;
// paragraphStyle.tailIndent = 200.0; [attributedString addAttribute:NSParagraphStyleAttributeName
value:paragraphStyle
range:NSMakeRange(, string.length)]; lable.attributedText = attributedString;


NSForegroundColorAttributeName    文字前景色

NSBackgroundColorAttributeName     文字背景色

NSLigatureAttributeName        连体字(NSNumber  @0:无连体,@1:默认连体,系统字体不包含对连体的支持)

NSUnderlineStyleAttributeName     下划线

NSStrokeColorAttributeName       只有在NSStrokeWidthAttributeName设置了值之后才有效(默认字体颜色和前景色一致,如果设置的颜色和前景色不一致则前景色无效)

NSStrokeWidthAttributeName      设置该属性之后字体变成空心字体,字体边线宽度为value设定的值

NSBaselineOffsetAttributeName     值为NSNumber类型,表明文字相对于其他文字基准线向上的偏移量

NSUnderlineColorAttributeName      值为UIColor类型,下划线颜色(只有在NSUnderlineStyleAttributeName的value为@1时有效)

NSUnderlineStyleAttributeName      值为NSNumber类型,下划线宽度(默认值为@0:下划线宽度为0——不现实下划线,@1:字符串有下划线)

属性挺多的,有其他需要的话command点进去看一下就ok,如果对他们的功能不了解三根指头点一下关键词,或者按住option点一下看看官方文档的Description相信就会有所了解了,其他的暂时就不介绍了。

如有问题,欢迎指正,小弟在此拜谢。

这里有篇讲解富文本的文章:http://www.2cto.com/kf/201409/334308.html

本文参考源码:https://github.com/NSSONGMENG/Practice/tree/master/LessonAttributedString

饿死了,吃饭去嘞~

NSAttributedString用法的更多相关文章

  1. NSAttributedString的用法

    标签: 以前看到这种字号和颜色不一样的字符串,想出个讨巧的办法就是“¥150”一个UILable,“元/位”一个UILable.今天翻看以前的工程,command点进UITextField中看到[at ...

  2. 富文本常用封装(NSAttributedString浅析)

    最近经常遇到关于富文本的一些需求,特此封装了几个最常用的API分享给大家,但授之以鱼不如授之以渔,接下来会顺便谈谈NSAttributedString,确保你读了本篇文章能够自己封装关于富文本的API ...

  3. NSAttributedString富文本简单介绍和常用方法浅析

    NSAttributedString基本知识点介绍 1.初始化方法 - (instancetype)initWithString:(NSString *)str; - (instancetype)in ...

  4. iOS属性文字NSAttributedString

    它本身是一个Foundation框架的类, 但如果要使用它主要用到了UIKit框架中的NSAttributedString中的一些常量字符串 ----------------------------- ...

  5. UI各种小控件的用法

    今天给大家列举出来UI中的一些小控件的用法.方便大的学习与使用 一些方法和属性我们能够查看API文档.不必将每一个控件的功能都记住, 由于在使用的过程中,我们能够查看API文档.方便使用,我们仅仅要记 ...

  6. EditText 基本用法

    title: EditText 基本用法 tags: EditText,编辑框,输入框 --- EditText介绍: EditText 在开发中也是经常用到的控件,也是一个比较必要的组件,可以说它是 ...

  7. jquery插件的用法之cookie 插件

    一.使用cookie 插件 插件官方网站下载地址:http://plugins.jquery.com/cookie/ cookie 插件的用法比较简单,直接粘贴下面代码示例: //生成一个cookie ...

  8. Java中的Socket的用法

                                   Java中的Socket的用法 Java中的Socket分为普通的Socket和NioSocket. 普通Socket的用法 Java中的 ...

  9. [转载]C#中MessageBox.Show用法以及VB.NET中MsgBox用法

    一.C#中MessageBox.Show用法 MessageBox.Show (String) 显示具有指定文本的消息框. 由 .NET Compact Framework 支持. MessageBo ...

随机推荐

  1. js 获取 sktime时间

    效果图如下: HTML代码: <html> <head> <script> //------------------------------------------ ...

  2. nyoj 经典的连续字串和

    import java.util.Scanner; public class 字串和 { public static void main(String[] args) { // TODO Auto-g ...

  3. [SAN4N学习笔记]使用SysTick精准延时

    一.准备工作:      将上一节搭建的LED工程复制一份,命名为"2.systick".这一节主要讲如何使用系统的SysTick节拍定时器来进行精准延时程序. 二.程序编写: S ...

  4. Delphi 7下使用Log4Delphi 0.8日志组件

    Log4Delphi是一个开放源码项目,旨在制作用于Borland的Delphi高质量实用的日志套件,是基于Apache Software Foundation的log4j包. 安装:         ...

  5. 1 weekend110的hdfs源码跟踪之打开输入流 + hdfs源码跟踪之打开输入流总结

    3种形式的元数据,fsimage是在磁盘上,meta.data是在内存上, 我们继续,前面呢,断点是打在这一行代码处, FileSystem fs = FileSystem.get(conf); we ...

  6. 在JSP页面中调用另一个JSP页面中的变量

    在jsp学习中,经常需要在一个jsp页面中调用另一个jsp页面中的变量,下面就这几天的学习,总结一下. jsp页面之间的变量调用有多种方法: 1.通过jsp的内置对象—request对象获取参数: ( ...

  7. 关于PreferenceActivity的使用和一些问题的解决(自己定义Title和取值)

    android的Setting往往用PreferenceActivity来写的 我们在建立layout文件: <PreferenceScreen xmlns:android="http ...

  8. careercup-栈与队列 3.1

    3.1 描述如何只用一个数组来实现三个栈. 解答 我们可以很容易地用一个数组来实现一个栈,压栈就往数组里插入值,栈顶指针加1: 出栈就直接将栈顶指针减1:取栈顶值就把栈顶指针指向的单元的值返回: 判断 ...

  9. 【转】谈Objective-c block的实现

    本文转自http://blog.devtang.com/blog/2013/07/28/a-look-inside-blocks/,如有侵权,请联系我删除 前言 这里有关于block的5道测试题,建议 ...

  10. Ext信息提示对话框

    Ext.window.MessageBox是一个工具类,他继承自Ext.window.Windoe对象,用来生成各种风格的信息提示对话框,其实例对象可以通过Ext.MessageBox或Ext.Msg ...