1 - 設(shè)置斜體
UILabel *lab = [[UILabel alloc] init];
lab.frame = CGRectMake(100, 100, 100, 50);
[self.view addSubview:lab];
lab.backgroundColor = MCColor(@"#aaaaaa", 1.0); //自己定義的顏色宏
lab.text = @"斜體字體測(cè)試";
CGAffineTransform matrix =CGAffineTransformMake(1, 0, tanf(25 * (CGFloat)M_PI / 180), 1, 0, 0);//設(shè)置反射。傾斜角度刮吧。
UIFontDescriptor *desc = [ UIFontDescriptor fontDescriptorWithName :[UIFont systemFontOfSize:14].fontName matrix:matrix];//取得系統(tǒng)字符并設(shè)置反射。
lab.font = [UIFont fontWithDescriptor:desc size :14];
2 - 對(duì)象內(nèi)部盡量直接訪問實(shí)例變量
摘要: 在寫入實(shí)例變量時(shí),通過 設(shè)置方法 來做傻昙,而在讀取的時(shí)候 ,通過直接訪問的方式
在對(duì)象之外訪問實(shí)例變量時(shí)彩扔,應(yīng)該通過屬性來做妆档,然而在對(duì)象內(nèi)部訪問實(shí)例變量,強(qiáng)烈建議讀取實(shí)例變量的時(shí)候通過直接訪問的形式虫碉,而在設(shè)置實(shí)例變量的時(shí)候通過屬性來做贾惦。
一 self.property 和_property 區(qū)別
直接訪問實(shí)例變量,不經(jīng)過OC 的 "方法派發(fā)" (下篇寫),訪問速度快,編譯器生成的代碼會(huì)直接訪問保存對(duì)象實(shí)例變量的內(nèi)存
直接訪問實(shí)例變量须板,不會(huì)調(diào)用 設(shè)置方法碰镜,繞過了相關(guān)屬性所定義的“內(nèi)存管理語(yǔ)義”,例如在ARC 中习瑰,直接方法copy的屬性洋措,不會(huì)拷貝而是直接覆蓋
直接訪問實(shí)例變量,不會(huì)觸發(fā)KVO
通過屬性訪問杰刽,可以設(shè)置斷點(diǎn)調(diào)試
綜合上面的情況:在寫入實(shí)例變量時(shí),通過 設(shè)置方法 來做王滤,而在讀取的時(shí)候 贺嫂,通過直接訪問的方式。既可以提高讀取操作速讀雁乡,又能控制對(duì)屬性的寫入第喳,保證相關(guān)屬性的 "內(nèi)存管理語(yǔ)義" 得以貫徹。
二 注意點(diǎn)
2.1 懶加載
懶加載的情況下踱稍,必須通過 獲取方法 來訪問屬性曲饱,或者,實(shí)例變量不會(huì)別初始化
- (NSMutableArray *)modelArray {
if (!_modelArray) {
_modelArray = [[NSMutableArray alloc] initWithCapacity:9];
}
return _modelArray;
}
2.2 不要在 init 和 dealloc 方法中使用 self.property 因?yàn)樵?init 和 dealloc 中珠月,對(duì)象的存在與否還不確定扩淀,所以給對(duì)象發(fā)消息可能不會(huì)成功
- (instancetype)init
{
self = [super init];
if (self) {
_models = [NSArray array];
}
return self;
}
三 總結(jié)
在對(duì)象內(nèi)部讀取數(shù)據(jù)時(shí),應(yīng)該直接通過實(shí)例變量來讀啤挎,在寫入數(shù)據(jù)時(shí)驻谆,則應(yīng)該通過屬性來寫
在初始化方法和 dealloc 方法中,應(yīng)該直接通實(shí)例變量名直接來讀寫數(shù)據(jù)庆聘,避免用self.property訪問
使用懶加載時(shí)胜臊,需要通過屬性來讀取數(shù)據(jù)
3 - 通過按鈕的事件來設(shè)置背景色
1.通過按鈕的事件來設(shè)置背景色
- (void)viewDidLoad {
[super viewDidLoad];
UIButton *button1 = [[UIButton alloc] initWithFrame:CGRectMake(50, 200, 100, 50)];
button1.backgroundColor = [UIColor orangeColor];
[button1 setTitle:@"button1" forState:UIControlStateNormal];
[button1 addTarget:self action:@selector(button1BackGroundHighlighted:) forControlEvents:UIControlEventTouchDown];
[button1 addTarget:self action:@selector(button1BackGroundNormal:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button1]; }
// button1普通狀態(tài)下的背景色
- (void)button1BackGroundNormal:(UIButton *)sender {
sender.backgroundColor = [UIColor orangeColor]; } // button1高亮狀態(tài)下的背景色
- (void)button1BackGroundHighlighted:(UIButton *)sender {
sender.backgroundColor = [UIColor greenColor]; }
2.通過把顏色轉(zhuǎn)換為UIImage來作為按鈕不同狀態(tài)下的背景圖片
- (void)viewDidLoad {
[super viewDidLoad];
UIButton *button2 = [[UIButton alloc] initWithFrame:CGRectMake(170, 200, 100, 50)];
[button2 setTitle:@"button2" forState:UIControlStateNormal];
[button2 setBackgroundImage:[self imageWithColor:[UIColor redColor]] forState:UIControlStateNormal];
[button2 setBackgroundImage:[self imageWithColor:[UIColor grayColor]] forState:UIControlStateHighlighted];
[self.view addSubview:button2];
}
// 顏色轉(zhuǎn)換為背景圖片
- (UIImage *)imageWithColor:(UIColor *)color {
CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
4 - scrollview滑動(dòng)到一定距離后現(xiàn)在不能滑動(dòng)
CGFloat offsetY = scrollView.contentOffset.y;
// 最多向下拉伸200
if (offsetY < -200) {
CGPoint point = scrollView.contentOffset;
point.y = -200;
scrollView.contentOffset = point;
}