学习笔记-斯坦福iOS7-第四课:框架和带属性字符串

本文介绍了Objective-C编程语言的基础知识,包括动态绑定的概念及其保障消息传递安全性的方法,Foundation框架中的核心类如NSObject、NSArray、NSDictionary的使用技巧,以及NSAttributedString和NSMutableAttributedString如何为文本添加样式。
第四课 框架和带属性字符串

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


4. UIFont
a 优先使用的字体:UIFont *font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
b. 不要在用户内容中使用系统字体;系统字体用在按钮上;


5. NSAttributedString
像NSString,但每个字符都有自己的属性,如下划线,描边宽度,字体等。


6. NSMutableAttributedString
a.
- (void)addAttributes:(NSDictionary*)attributes range:(NSRange)range;
- (void)setAttributes:…;

- (void)removeAttributes:…;


b. attributes
@{
NSFontAttributeName:[UIFont preferredFontWithTextStyle:UIFontTextStyleHeadLine],
NSForegroundColorAttributeName:[UIColor greenColor],
NSStrokeColorAttributeName:[UIColor redColor],
NSUnderlineStyleAttributeName:@(NSUnderlineStyleSingle)

}; 等等。


c. use attributes
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;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值