第四课 框架和带属性字符串
1. Dynamic Binding
保证发送消息安全,两种方法:
a. 内省(Introspection):isKindOfClass, isMemberOfClass, respondsToSelector;
b. 协议(Protocols)
2. Foundation
A. NSObject
a. -(id)copy; -(id)mutableCopy; 不是所有类都实现了,要想拷贝自己,需要自己实现。
B. NSArray
a. 初始化可用 @[];
b. 非常酷的方法:
- (NSArray*)sortedArrayUsingSelector:(SEL)aSelector; // 数组排序
- (void)makeObjectsPerformSelector:(SEL)aSelector withObject:(nullable id)argument;
- (NSString *)componentsJoinedByString:(NSString *)separator;
(详情:http://blog.youkuaiyun.com/sharpyl/article/details/51940573)
C. NSNumber
a. 初始化可用 @();
NSNumber *three = @3;
NSNumber *underline = @(NSUnderlineStyleSingle); // enum
NSNumber *match = @([card match:@[otherCard]]); // 原始类型表达式
D. NSValue
用来封装非对象,非原始类型表达式的类型,基本就是C struct。
一般封装struct,把他转换成字符串。
NSValue *edgeObj = [NSValue valueWithUIEdgeInsets:UIEdgeInsetsMake(1,1,1,1)];
E. NSSet 无序集合,在数据很多时,列如10000个,NSArray查找会变慢,NSSet 经过hash处理,效率很高,
F. NSDictionary
a 初始化可用@{key:value,key2:value2};
b 查找可用[];
c 键需要实现hash和isEqual
G. NSUserDefaults
用来存储小的对象,不要存储大图片等数据。
H. NSRange
查找字符串时:
NSRange r = [strText rangeOfString:hi];
if ( r.location == NSNotFound){}
a 优先使用的字体:UIFont *font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
b. 不要在用户内容中使用系统字体;系统字体用在按钮上;
5. NSAttributedString
像NSString,但每个字符都有自己的属性,如下划线,描边宽度,字体等。
6. NSMutableAttributedString
a.
- (void)addAttributes:(NSDictionary*)attributes range:(NSRange)range;
- (void)setAttributes:…;
@{
NSFontAttributeName:[UIFont preferredFontWithTextStyle:UIFontTextStyleHeadLine],
NSForegroundColorAttributeName:[UIColor greenColor],
NSStrokeColorAttributeName:[UIColor redColor],
NSUnderlineStyleAttributeName:@(NSUnderlineStyleSingle)
UIButton : - (void)setAttributedTitle:(NSAttributedString*)title forState:…;
UILabel : @property (nonatomic, strong) NSAttributedString *attributedText;
等等。
注:这些attributed不能改变,如果需要改变,需要mutableCopy,modify, then set it:
NSMutableAtributedString *labelText = [myLabel.attributedText mutableCopy];
[labelText setAttributes:…];
myLabel.attributtedText = labelText;
1. Dynamic Binding
保证发送消息安全,两种方法:
a. 内省(Introspection):isKindOfClass, isMemberOfClass, respondsToSelector;
b. 协议(Protocols)
2. Foundation
A. NSObject
a. -(id)copy; -(id)mutableCopy; 不是所有类都实现了,要想拷贝自己,需要自己实现。
B. NSArray
a. 初始化可用 @[];
b. 非常酷的方法:
- (NSArray*)sortedArrayUsingSelector:(SEL)aSelector; // 数组排序
- (void)makeObjectsPerformSelector:(SEL)aSelector withObject:(nullable id)argument;
- (NSString *)componentsJoinedByString:(NSString *)separator;
(详情:http://blog.youkuaiyun.com/sharpyl/article/details/51940573)
C. NSNumber
a. 初始化可用 @();
NSNumber *three = @3;
NSNumber *underline = @(NSUnderlineStyleSingle); // enum
NSNumber *match = @([card match:@[otherCard]]); // 原始类型表达式
D. NSValue
用来封装非对象,非原始类型表达式的类型,基本就是C struct。
一般封装struct,把他转换成字符串。
NSValue *edgeObj = [NSValue valueWithUIEdgeInsets:UIEdgeInsetsMake(1,1,1,1)];
E. NSSet 无序集合,在数据很多时,列如10000个,NSArray查找会变慢,NSSet 经过hash处理,效率很高,
F. NSDictionary
a 初始化可用@{key:value,key2:value2};
b 查找可用[];
c 键需要实现hash和isEqual
G. NSUserDefaults
用来存储小的对象,不要存储大图片等数据。
H. NSRange
查找字符串时:
NSRange r = [strText rangeOfString:hi];
if ( r.location == NSNotFound){}
3. Colors
a 优先使用的字体:UIFont *font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
b. 不要在用户内容中使用系统字体;系统字体用在按钮上;
5. NSAttributedString
像NSString,但每个字符都有自己的属性,如下划线,描边宽度,字体等。
6. NSMutableAttributedString
a.
- (void)addAttributes:(NSDictionary*)attributes range:(NSRange)range;
- (void)setAttributes:…;
- (void)removeAttributes:…;
@{
NSFontAttributeName:[UIFont preferredFontWithTextStyle:UIFontTextStyleHeadLine],
NSForegroundColorAttributeName:[UIColor greenColor],
NSStrokeColorAttributeName:[UIColor redColor],
NSUnderlineStyleAttributeName:@(NSUnderlineStyleSingle)
}; 等等。
UIButton : - (void)setAttributedTitle:(NSAttributedString*)title forState:…;
UILabel : @property (nonatomic, strong) NSAttributedString *attributedText;
等等。
注:这些attributed不能改变,如果需要改变,需要mutableCopy,modify, then set it:
NSMutableAtributedString *labelText = [myLabel.attributedText mutableCopy];
[labelText setAttributes:…];
myLabel.attributtedText = labelText;
本文介绍了Objective-C编程语言的基础知识,包括动态绑定的概念及其保障消息传递安全性的方法,Foundation框架中的核心类如NSObject、NSArray、NSDictionary的使用技巧,以及NSAttributedString和NSMutableAttributedString如何为文本添加样式。

被折叠的 条评论
为什么被折叠?



