标签:style color os io 使用 ar for 文件 数据
一、ios5.0以前
1、首先导入CoreText.framework,并在需要使用的文件中导入:
#import<CoreText/CoreText.h>
2、创建一个NSMutableAttributedString:
非常常规的创建方式,接下来我们给它配置属性:
3、NSAttributedString继承于NSObject,并且不支持任何draw的方法,那我们就只能自己draw了。写一个UIView的子类(假设命名为TView),在initWithFrame中把背景色设为透明(self.backgroundColor = [UIColor clearColor]),然后在重写drawRect方法:
在代码中我们调整了CTM(current transformation matrix),这是因为Quartz 2D的坐标系统不同,Quartz2D的坐标系统在左下角
4、另外方法使用
1)
CATextLayer可以直接支持NSAttributedString!
2) UILabel *label = [[UILabel alloc] init];
label.frame = CGRectMake(100, 100, 100, 40);
[label setAttributedText:attrTitle];
[self.view addSubview:label];
二、在iOS6之后,创建一个AttributedString变成了一件轻松的事情,<CoreText/CoreText.h>已经不需要导入了。如果我要设置字体的颜色,可以直接这样:
[textAttr addAttribute:NSForegroundColorAttributeName
value:[UIColor redColor]
range:NSMakeRange(0, text.length)];
如果要计算一个NSAttributedString的size,使用NSAttributedString的这个API:
- (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options context:(NSStringDrawingContext *)context NS_AVAILABLE_IOS(6_0);
但是需要注意一点,如果调用这个API的NSAttributedString不包含字体、行高等有利于计算的数据,那最终计算出来的size可能和实际有所出入。
标签:style color os io 使用 ar for 文件 数据
原文地址:http://www.cnblogs.com/swallow37/p/3951563.html