標籤:
來自:http://www.jianshu.com/p/bcf86b170d9c
前言
為什麼會寫這個?因為在iOS開發中,介面的布局一直沒有Android布局有那麼多的方法和優勢,我個人開發都是純程式碼,Masonry這個架構我在開發中也是不用的,一個是代碼布局的時候,代碼量比較多,另外好像在iOS10 布局有問題,網上也有些解決的方法了。
所以就想能自訂一些UI控制項,使布局更加簡單
實現思路
可以像Android的wrap_content一樣,是UILabel 可以根據內容來展示控制項的寬高
目前提供ZFUI framework裡只提供了UILabel和UIButton的方法
它們都繼承對應父類,然後重寫裡父類的方法,讓控制項使用更加簡單和方便
實現方法
直接上代碼,Demo和framework可以去github上下載
https://github.com/joshuaGeng/ZFUI-Framework
首先把ZFUI.framework 添加到項目裡
調用的方法
#import <ZFUI/ZFUI.h>
// 建立一個Label的方法
ZFUILabel *label = [[ZFUILabel alloc] init];
label.numberOfLines = 0;
label.backgroundColor = [UIColor greenColor];
[label setLabelText:@"HelloWorld" andWithFrame:CGRectMake(100, 70, 0, 0)];
//label.isCanCopy = YES;
//label.titleCopy = @"Copy";
//設定不同顏色
[label setAttributeColorWithAllStr:label.text andAllColor:[UIColor redColor] andWithDiffStr:@"World" andDiffColor:[UIColor blackColor]];
[self.view addSubview:label];
// button
ZFUIButton *button = [[ZFUIButton alloc] initWithFrame:CGRectMake(100, 120, 120, 60)];
[button setImage:[UIImage imageNamed:@"img"] forState:UIControlStateNormal];
[button setTitle:@"Button" forState:UIControlStateNormal];
button.backgroundColor = [UIColor orangeColor];
button.btnImgRect = CGRectMake(10, 20, 22, 21);
button.btnTitleRect = CGRectMake(50, 20, 120, 30);
[self.view addSubview:button];
目前提供ZFUI framework裡只提供了UILabel和UIButton的方法,後續還會繼續更新,有需要的可以下載,Demo裡的注釋也很很詳細。
iOS 開發 ZFUI framework控制項,使布局更簡單