通過這種方式、可以快速完成簡單基礎(chǔ)UI的創(chuàng)建吃度。
也不用在定義一堆的控件屬性甩挫、保持代碼干凈。
先看示例圖
1椿每、簡潔版
2伊者、復(fù)雜版
省了代碼、看這舒心间护。
具體實(shí)現(xiàn)
核心:通過UIView的擴(kuò)展方式亦渗,分別對(duì)UIImageView、UILabel等常用控件挨個(gè)實(shí)現(xiàn)需要的方法和屬性汁尺。
噗法精、代碼格式?jīng)]弄出來、湊合著看吧痴突!
統(tǒng)一創(chuàng)建:
///添加內(nèi)容
//格式1字符串搂蜓、q_*n+數(shù)量/q_view_*h/q_*kf
//格式2數(shù)組、[格式1]
-(void)q_addSubitems:(id)item;
對(duì)應(yīng)獲取控件的:
/// 根據(jù)名字獲取元素
/// @paramname通過 數(shù)組 q_*k+下標(biāo)生成 單個(gè)直接用q_view_*h/或者
-(UIView*)q_subkey:(NSString*)name;
對(duì)應(yīng)具體屬性需要區(qū)別對(duì)待辽装、(例如UIView+Label):
///文字
@property(nonatomic)NSString * ql_text;
///行數(shù)
@property(nonatomic)int ql_line;
///對(duì)齊
@property(nonatomic)NSTextAlignment ql_alignment;
///顏色
@property(nonatomic)UIColor * ql_color;
///字體
@property(nonatomic)UIFont * ql_font;
///富文本
@property(nonatomic)NSAttributedString * ql_att;
例如:UIView+Image
///獲取圖片
@property(nonatomic,readonly)UIImage * qi_img;
///設(shè)置圖片
@property(nonatomic)NSString * qi_icon;
///填充方式
@property(nonatomic)UIViewContentMode qi_mode;
結(jié)語:
常用的控件就這四個(gè)帮碰、其他的也可擴(kuò)展、按照基本規(guī)則來就行拾积、(先定義Key殉挽、然后在具體實(shí)現(xiàn))。
代碼地址
https://github.com/quua/QUIExt
最后祝你身份健康拓巧、再見斯碌。