#import <CoreText/CoreText.h>
#import "ViewController.h"
@interface ViewController ()
@property(nonatomic, weak) IBOutlet UILabel *label1;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:@"Some String"];
[attString addAttribute:(NSString*)kCTUnderlineStyleAttributeName value:[NSNumber numberWithInt:kCTUnderlineStyleSingle] range:(NSRange){,[attString length]}];
self.label1.attributedText = attString;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end

由于iOS7新出的NSTextStorge是NSMutableAttributedString的子类,所以要用好NSTextStorage,首先要学好NSMutableAttributedString和NSAttributedString。


按个人的理解,NSAttributedString是一个带有属性的字符串,通过该类可以灵活地操作和呈现多种样式的文字数据。


因为是初步使用,所以基本上都是对照着文档上的指导和介绍的方法来写Demo。


首先是两种类的初始化方法(基本上是相似的):



  1.  1 // initWithString:
    2 NSAttributedString *attributedString_str = [[NSAttributedString alloc] initWithString:@"attributedString"];
    3 NSLog(@"%@", attributedString_str);
    4 // textView.attributedText = attributedString_str;
    5
    6
    7 // initWithAttributedString:
    8 NSAttributedString *attributedString_atts = [[NSAttributedString alloc] initWithAttributedString:attributedString_str];
    9 NSLog(@"%@", attributedString_atts);
    10 // textView.attributedText = attributedString_atts;
    11
    12
    13 // initWithString:attributes:
    14 UIColor *backgroundColor = [UIColor blackColor];
    15 NSNumber *baseLineOffset = [NSNumber numberWithFloat:20.0];
    16 UIColor *foregroundColor = [UIColor whiteColor];
    17 NSNumber *kern = [NSNumber numberWithFloat:5.0];
    18 NSNumber *ligature = [NSNumber numberWithFloat:3.0];
    19 NSURL *linkURL = [NSURL URLWithString:@"http://www.baidu.com"];
    20 NSNumber *underline = [NSNumber numberWithInt:NSUnderlineStyleSingle];
    21 NSDictionary *attrsDic = @{NSForegroundColorAttributeName: foregroundColor,
    22 NSBackgroundColorAttributeName: backgroundColor,
    23 NSBaselineOffsetAttributeName: baseLineOffset,
    24 NSKernAttributeName: kern,
    25 NSLigatureAttributeName: ligature,
    26 NSLinkAttributeName: linkURL,
    27 NSUnderlineStyleAttributeName: underline
    28 };
    29 NSAttributedString *attributedString_str_atts = [[NSAttributedString alloc] initWithString:@"http://www.baidu.com" attributes:attrsDic];
    30 NSLog(@"%@", attributedString_str_atts);
    31 // textView.attributedText = attributedString_str_atts;
    32
    33
    34 // initWithFileURL:options:documentAttributes:error:
    35 NSURL *fileURL = nil;
    36 fileURL = [[NSBundle mainBundle] URLForResource:@"Dynamic Coloring" withExtension:@"rtf"];
    37 NSAttributedString *attributedString_fileURL = [[NSAttributedString alloc] initWithFileURL:fileURL options:@{} documentAttributes:nil error:nil];
    38 NSLog(@"%@", attributedString_fileURL);
    39 // textView.attributedText = attributedString_fileURL;
    40
    41
    42 // initWithData:options:documentAttributes:error:
    43 fileURL = nil;
    44 fileURL = [[NSBundle mainBundle] URLForResource:@"View Layout" withExtension:@"rtf"];
    45 NSData *data = [[NSData alloc] initWithContentsOfURL:fileURL];
    46 NSAttributedString *attributedString_data = [[NSAttributedString alloc] initWithData:data options:@{} documentAttributes:nil error:nil];
    47 NSLog(@"%@", attributedString_data);
    48 // textView.attributedText = attributedString_data;
    49
    50
    51 // initWithAttributedString:
    52 NSMutableAttributedString *mutableAttributedString_attrs = [[NSMutableAttributedString alloc] initWithAttributedString:attributedString_fileURL];

非常简单。




由于NSAttributedString的属性以字典的形式记录,所以要弄清楚其中一些属性对应的键值:



说说几个自己使用上或者了解作用的。


NSBackgroundColorAttributeName:文字背景的颜色。


NSBaselineOffsetAttributeName:设置行距。


NSFontAttributeName:字体的样式,必须设置NSFont作为Value,NSFont在iOS中不能使用,这个属性的设置上我还不会。


NSForegroundColorAttributeName:文字颜色。


NSUnderlineStyleAttributeName:为文字添加下划线。必须设置NSNumber对象为Value。如:



  1. NSNumber *underline = [NSNumber numberWithInt:NSUnderlineStyleSingle];


以上attribute在NSAttributedString和NSMutableAttributedString对象创建时可以设定,不同的是NSAttributedString对象在创建成功后其属性便不可改变,而NSMutableAttributedString的属性是可以改变的。如下所示:



  1.  1 // Change NSMutableAttributedString
    2 [mutableAttributedString_attrs beginEditing];
    3 /*
    4 // addAttributes:range:
    5 [mutableAttributedString_attrs addAttributes:@{NSLinkAttributeName: @"www.baidu.com",
    6 NSBackgroundColorAttributeName: [UIColor greenColor],
    7 NSUnderlineStyleAttributeName: [NSNumber numberWithInt:NSUnderlineStyleDouble]
    8 }
    9 range:NSMakeRange(0, [attributedString_fileURL length])]; */
    10 // addAttribute:value:range:
    11 [mutableAttributedString_attrs addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInt:NSUnderlineStyleThick] range:NSMakeRange(0, 10)];
    12 [mutableAttributedString_attrs addAttribute:NSBaselineOffsetAttributeName value:[NSNumber numberWithFloat:20.0] range:NSMakeRange(20, 100)];
    13 [mutableAttributedString_attrs endEditing];
    14 NSLog(@"%@", mutableAttributedString_attrs);
    15

在修改文字属性的开头和结尾要分别调用beginEditing和endEditing方法,这些方法可以在文字属性发生变化时发送消息给事件监听者。


可以为某个范围内的文字单个地添加属性key-value对,也可以添加一个属性字典。
注意到在控制台输出NSAttributedString或NSMutableAttributedString时,输出的不仅是字符内容,还有对应的属性值:



  1. 1 2013-08-11 16:40:44.737 AttributedString_i7_Demo[43468:a0b] http://www.baidu.com{
    2 NSBackgroundColor = "UIDeviceWhiteColorSpace 0 1";
    3 NSBaselineOffset = 20;
    4 NSColor = "UIDeviceWhiteColorSpace 1 1";
    5 NSKern = 5;
    6 NSLigature = 3;
    7 NSLink = "http://www.baidu.com";
    8 NSUnderline = 1;
    9 }



也可以获取某一段文字的属性:



  1. 1 // get attribute
    2 NSRange range = NSMakeRange(0, mutableAttributedString_attrs.length);
    3 // attributesAtIndex:effectiveRange:
    4 NSDictionary *dic = [mutableAttributedString_attrs attributesAtIndex:0 effectiveRange:&range];
    5 NSLog(@"%@", [dic objectForKey:NSFontAttributeName]);

如果要将NSMutableAttributedString对象赋值给NSAttributedString时,要使用copy方法:


  1. 1 textView.attributedText = [mutableAttributedString_attrs copy]; 

    以上是对NSAttributedString和NSMutableAttributedString最基本的使用(主要还是NSAttributedString),为了强化作为NSMutableAttributedString的子类NSTextStorage处理文本的能力,明显在这两个类中增加了许多新的成员,如NSTextAttachment,在这里没有用上,必须继续深入学习。

 

设置UITextView,UILabel 中的文字加下划线



1
2
3
4
5
6
7
8
9
10
11
//添加下划线
-(NSAttributedString*) getAttributedString:(NSAttributedString*) attributedString isUnderline:(BOOL) isUnderline
{
    NSNumber *valuUnderline = [NSNumbernumberWithBool:isUnderline];
    NSRange rangeAll = NSMakeRange(0, attributedString.string.length);
    NSMutableAttributedString *as = [attributedString mutableCopy];
    [as beginEditing];
    [as addAttribute:NSUnderlineStyleAttributeNamevalue:valuUnderline range:rangeAll];
    [as endEditing];
    return as;
}


使用


1
2
textView.attributedText = [self getAttributedString:_mainTextView.attributedText isUnderline:YES];
label.attributedText =  [self getAttributedString:_mainTextView.attributedText isUnderline:YES];
												

NSAttributedString能否设置文字下划线?是否支持line break?的更多相关文章

  1. 实现文字下划线 ---模拟text-decoration

    css 的text-decoration可以实现文字下方的下划线,但是距离文字比较近,不是很好看,我们可以使用border-bottom来模拟这个效果 (inline元素虽然不可以设置margin-t ...

  2. 设置TextView下划线并响应点击事件(SpannableString)

    下面是一个20行的完整Demo代码:基本原理是使用一个SpannableString并设置其ClickableSpan来响应点击事件. TextView useInfo = (TextView) fi ...

  3. android TextView 例子代码(文字中划线、文字下划线)

    XML: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android ...

  4. 1.1.15 word调整文字与下划线之间的间距

    先请按CTRL+U快捷键,或点击“下划线”按钮,然后输入一个空格,再输入文字“下划线间距”,在文字的尾部再添加一个空格.选中文字内容(注意不要选中首尾的空格),单击菜单“格式”→“字体”,在“字体”设 ...

  5. css背景图片、隐藏、指针、垂直居中、去除下划线、缩进、列表类型

    <html><head lang="en"> <meta charset="UTF-8"> <title>< ...

  6. 如何去掉a标签的下划线

    首先来了解下<a>标签的一些样式: <a>标签的伪类样式 一组专门的预定义的类称为伪类,主要用来处理超链接的状态.超链接文字的状态可以通过伪类选择符+样式规则来控制.伪类选择符 ...

  7. Android自己定义无下划线ClickableSapn超链接文本样式

    近期在做评论的时候须要实现这样的效果 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvamF2X2ltYmE=/font/5a6L5L2T/fontsize/ ...

  8. CSS样式下划线

    样式之文字下划线 第一 text-decoration: underline; 缺点是不好修改颜色 不要修改下划线的位置,优点就是一行代码解决 可以使用line-height: 20px; 也能调整位 ...

  9. iOS - UIButton设置文字标题下划线以及下划线颜色

    创建button设置可以折行显示 - (void)viewDidLoad { [super viewDidLoad]; UIButton * button = [[UIButton alloc] in ...

随机推荐

  1. C++基础之类和对象二

    (1)指向类的成员的指针分为指向数据成员的指针和指向成员函数的指针两种.一个类的对象用运算符“.*”来操作指向该类成员的指针,而一个指向类的对象的指针用运算符“->”来操作指向该类成员的指针.( ...

  2. Python-OpenCV中图像颜色空间转换

    目录 cv2.cvtColor() 1. RGB to GRAY 2. RGB to CIE XYZ 3. RGB to YCrCb JPEG 4. RGB to HSV 5. RGB to HLS ...

  3. 求组合数 C(n,m)

    下面内容转自: http://blog.csdn.net/zengaming/article/details/63681754 一.求解C(n, m) 公式一: 公式二: 公式二可以这么理解,从n个物 ...

  4. 对各种lca算法的理解

    1.RMQ+ST 首先注意这个算法的要素:结点编号,dfs序,结点深度. 首先dfs,求出dfs序,同时求出每个结点的深度.然后st算法,维护深度最小的结点编号(dfs序也可以,因为他们俩可以互相转换 ...

  5. EL表达式的语法介绍及九大隐含对象

    一. 简介 > JSP表达式 <%= %> 用于向页面中输出一个对象. > 到JSP2.0时,在我们的页面中不允许出现 JSP表达式和 脚本片段. > 使用EL表达式来代 ...

  6. AT2582 Mirrored

    传送门 智障爆搜题 可以发现题目给出的式子可以移项 然后就是\(rev(N)-N=D\) 然后假设\(N=a_1*10^{n-1}+a_2*10^{n-2}+...+a_{n}\) 那么\(rev(N ...

  7. POJ1013 Counterfeit Dollar

    题目来源:http://poj.org/problem?id=1013 题目大意:有12枚硬币,其中有一枚假币.所有钱币的外表都一样,所有真币的重量都一样,假币的重量与真币不同,但我们不知道假币的重量 ...

  8. svn常用功能使用简介

    1.文档库地址: https://xxx.xxx.xxx.xxx/svn/ 2.svn添加文件 2.1 在本地电脑上任何空白地方,右键-->打开“浏览版本库(Repo-browser)”,如图: ...

  9. Python 类总结

    Python可以继承多个父类,多重继承. 类支持多个对象的产生,命名空间的继承,运算符重载1).类产生多个实例对象Python OOP模型中的两种对象:类对象和实例对象.类对象提供默认的行为,是实例对 ...

  10. 嵌入式CISC模型机设计

    一.     课程设计的题目和内容 题目:设计一台嵌入式CISC模型计算机 采用定长CPU周期.联合控制方式,并运行能完成一定功能的机器语言源程序进行验证,机器语言源程序功能如下: 任意输入5个整数, ...