4 Foundation和带属性的字符串(3)

颜色
UIColor
表示颜色的对象

为基于RGB,HSB,甚至图案(UIImage)而创建的颜色的 初始化

一小撮“标准”颜色有类方法(如[UIColor greenColor])
一些“系统”的颜色也有类的方法(如[UIColor lightTextColor])



字体
UIFont
在IOS 7中获得正确的字体非常重要

通过 询问给定的文本样式的首选字体,是获得一个UIFont最好的方式...
UIFont *font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
一些其他样式(参见UIFontDescriptor官方文档)...
UIFontTextStyleHeadline, UIFontTextStyleCaption1, UIFontTextStyleFootnote等

也有“系统”字体
它们被用在像按钮标题的地方
+ (UIFont *)systemFontOfSize:(CGFloat)pointSize;
+ (UIFont *)boldSystemFontOfSize:(CGFloat)pointSize;
你不应该为你的用户的内容使用这些
应使用preferredFontForTextStyle:


UIFontDescriptor
字体是由艺术家设计的
他们并不总是为适应任何分类而设计的
有些字体有窄体或粗体或缩减的外观,有些则没有
甚至“尺寸”有时也是一个特定字体的设计角度

不过UIFontDescriptor试图去将字体分类
它通过家族,字型,大小和其他属性做到
这时你可以为字体要求有他们有的这些属性的并且获得“最佳匹配”

要知道“bold(粗体)”的最佳匹配可能并不是粗体,如果没有这样外观设计

你可以从一个已存在UIFont,通过这个UIFont方法得到它的字体描述...
- (UIFontDescriptor *)fontDescriptor;
你很可用已经通过preferredFontForTextStyle:得到了原始的UIFont

然后你可以修改他来创建一个新的描述,在UIFontDescriptor的方法,如…
- (UIFontDescriptor *)fontDescriptorByAddingAttributes:(NSDictionary *)attributes;
(属性和值可以在UIFontDescriptor的类参考页面中找到)
你也可以直接使用属性去创建一个UIFontDescriptor(虽然这很罕见)
+ (UIFontDescriptor *)fontDescriptorWithFontAttributes:(NSDictionary *)attributes;


象形特征
Italic, Bold, Condensed等,都是重要的足以在UIFontDescriptor中得到我们的API
- (UIFontDescriptorSymbolicTraits)symbolicTraits;
- (UIFontDescriptor *)fontDescriptorWithSymbolicTraits:(UIFontDescriptorSymbolicTraits)traits;
一些特征的例子(同样,看UIFontDescriptor的官方文档以了解更多)
UIFontDescriptorTraitItalic, UIFontDescriptorTraitBold, UIFontDescriptorTraitCondensed等

一旦你你想要有一个UIFontDescriptor描述的字体,请使用此UIFont方法:
+ (UIFont *)fontWithDescriptor:(UIFontDescriptor *)descriptor size:(CGFloat)size;
(指定0大小,如果你想描述中使用任何大小的)
将为你的描述给出的可用字体和外观得到一个“最佳匹配”。

举例
让我们尝试得到一个 “粗体字体” ...
UIFont *bodyFont = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
UIFontDescriptor *existingDescriptor = [bodyFont fontDescriptor];
UIFontDescriptorSymbolicTraits traits = existingDescriptor.symbolicTraits;
traits |= UIFontDescriptorTraitBold;
UIFontDescriptor *newDescriptor = [existingDescriptor fontDescriptorWithSymbolicTraits:traits];
UIFont *boldBodyFont = [UIFont fontWithFontDescriptor:newDescriptor size:0];

这将尽力使你的UIFontTextStyleBody首选字体为你的粗体版本



带属性的字符串
文本在屏幕上看起来如何
文本在屏幕上看起来如何,字体要做很多事
但这里有其他的限定词(颜色,是否是“轮廓”,笔画,宽度,下划线等)
使用NSAttributedString把文本连带字体和其他的限定词放在一起


NSAttributedString
可把他看成一个所有字符都有“属性”NSDictionary的NSString
属性是字符的,如字体,颜色,下火线与否等
然而他不是NSString实际上的子类(过会儿会说)


获取属性
你可以询问NSAttributedString所有关于字符串中给定位置的属性
- (NSDictionary *)attributesAtIndex:(NSUInteger)index
                     effectiveRange:(NSRangePointer)range;
range是返回的,并且他让你知道多少字符的属性是相同的
这里还有方法去询问关于特定属性,你可能会感兴趣
NSRangePointer本质上是一个NSRange *。他传递NULL没关系,如果你不在乎


NSAttributedString不是一个NSString
它不从NSString继承,所以你不能使用NSString的方法
如果你需要在字符上操作,这是NSAttributedString中强大的方法
- (NSString *)string;

例如,为了在NSAttributedString找出一个子串,你可以这样做...
NSAttributedString *attributedString = ...;
NSString *substring = ...;
NSRange r = [[attributedString string] rangeOfString:substring];

这个方法的字符串保证高性能,但是不稳定
如果你想保持住它,做一个它的copy


NSMutableAttributedString
不同于NSString,我们几乎总是使用可变的带属性字符串


添加或设置字符上的属性
你可以添加一个属性到一个范围内的字符...
- (void)addAttributes:(NSDictionary *)attributes range:(NSRange)range;
...这将改变属性的属性值,并不触及其他属性

或者设置属性到一个范围…
- (void)setAttributes:(NSDictionary *)attributes range:(NSRange)range;
…这将移除范围内所有其他的属性,过去的属性

你还可以移除范围中内的特定属性
- (void)removeAttribute:(NSString *)attributeName range:(NSRange)range;


定制字符串的内容(改变字符)
你可以用方法做到追加,插入,删除或替换字符。
或者调用NSMutableAttributedString方法- (NSMutableString *)mutableString
并返回修改的NSMutableString(属性将,令人难以置信的,予以保存!)


属性的种类都有什么在这里















哪里可以使用带属性的字符串?
UIButton的
- (void)setAttributedTitle:(NSAttributedString *)title forState:…; 
UILabel的
@property (nonatomic, strong) NSAttributedString *attributedText; 
UITextView的
@property (nonatomic, readonly) NSTextStorage *textStorage;


直接绘制字符串
NSAttributedStrings知道如何绘制自身在屏幕上,举例...
- (void)drawInRect:(CGRect)aRect;



UILabel
你可以设置它的内容,使用NSString属性text
但是他也有一个属性去set/get它的text,使用NSAttributedString...
@property (nonatomic, strong) NSAttributedString *attributedText;


注意这个属性字符串不可变的
因此,在UILabel去修改什么,你必须做一个mutableCopy,修改它,然后将其设置回去
NSMutableAttributedString *labelText = [myLabel.attributedText mutableCopy]; 
[labelText setAttributes:...];
myLabel.attributedText = labelText;


很多时候不需要这样
这些UILabel中的属性,像font, textColor等,用来设置所有字符的外观
UILabel中的属性字符串将主要用于“特殊标签” 



UITextView
像UILabel,但是是多行的,可选择/编辑,可以滚动等


通过NSMutableAttributedString设置它的文本与属性
在UITextView中取得NSMutableAttributedString代表文本
@property (nonatomic, readonly) NSTextStorage *textStorage;
NSTextStorage是NSMutableAttributedString的子类
你可以简单修改它并且UITextView将自动更新,iOS7新增


设置字体
UITextView中的字符与字符当然可以有各种不相同的字体
但是也有整个UITextView都适用一个字体的属性...
@property (nonatomic, strong) UIFont *font;

改变每个字符的字体不会重置其他属性(如颜色,下划线等)
然而你将失去你的象征性的特质!


在UITextView与TextKit中高级的布局
在UITextView中这个属性定义“哪里可以有文字”…
@property (readonly) NSTextContainer *textContainer;

这个对象将从textStorage中读取字符,并且在textContainer中放入字形...
@property (readonly) NSLayoutManager *layoutManager;

这个对象相当给力
例如,textContainer可以在其指定的地方有个“禁区”(图片周围变形)
如果你对排版有兴趣,检查它们

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值