Button
- Custom button: Adjust the frame of the inner child control
- Mode 1: Implement Titlerectforcontentrect: And Imagerectforcontentrect: Method, return the frame of Titlelabel and ImageView respectively
- Method 2: Set in the Layoutsubviews method
- Inner margin
// 设置按钮内容的内边距(影响到imageView和titleLabel)@property(nonatomic) UIEdgeInsets contentEdgeInsets;// 设置titleLabel的内边距(影响到titleLabel)@property(nonatomic) UIEdgeInsets titleEdgeInsets;// 设置imageView的内边距(影响到imageView)@property(nonatomic) UIEdgeInsets imageEdgeInsets;
Picture stretching
// 只拉伸中间的1x1区域- (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight;
- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets;- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets resizingMode:(UIImageResizingMode)resizingMode;
Copy
- There are 2 ways to implement a copy
- Copy: Returns an immutable copy
- Mutablecopy: Returns a mutable copy
- Steps to implement a copy of a normal object
- Compliance with Nscopying Protocol
- Implementing-copywithzone: Methods
- Create a new object
- Assigning a value to a new object's properties
KVC
- Full Name: Key value Coding (key code)
- Assign value
// 能修改私有成员变量- (void)setValue:(id)value forKey:(NSString *)key;- (void)setValue:(id)value forKeyPath:(NSString *)keyPath;- (void)setValuesForKeysWithDictionary:(NSDictionary *)keyedValues;
// 能取得私有成员变量的值 - (id)valueForKey:(NSString *)key; - (id)valueForKeyPath:(NSString *)keyPath; - (NSDictionary *)dictionaryWithValuesForKeys:(NSArray *)keys;
Dictionary Turn model
What is the type of dictionary value, and what type of property is given to the model
[Flag setvaluesforkeyswithdictionary:dict];
SETVALUESFORKEYSWITHDICTIONARY:KVC Dictionary Turn model
1. Traverse the dictionary all key Name,icon//2. Will go to the model to find, there is no corresponding property or method, if there is a property assignment [flag setvalue:@ "zhongguo.jpg" forkey:@ "icon"]
1. Find out if there is no SetIcon method, have, directly call method to assign value [flag seticon:@ "Zhongguo.jpg"]
2. Look for there is no icon attribute, there, direct assignment icon = icon;
3. Look for there is no _icon attribute, there, direct assignment _icon = icon;
4. Not found, direct error Setvalue:forundefinedkey:
The property name of the model must correspond to the dictionary key one by one KVO
- Full Name: Key value observing (key monitoring)
- Role: Changes in the property values of the listener model
- Steps
- Add Listener
// 利用b对象来监听a对象name属性的改变[a addObserver:b forKeyPath:@"name" options:NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew context:@"test"];
- Implementing a listening method in the listener
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{ NSLog(@"%@ %@ %@ %@", object, keyPath, change, context);}
UIButton, KVC, KVO