仿UIButton設置ImageView和TitleLabel

1 。我們經常會遇到需要文字和圖片相鄰的情況 通俗的我們回去改變Button的ImageView和TitleLable的 ImageInsets和TitleInsets做法 但是很多時候并不是很通用這里是指了一個UIView的類目 可以方便移植 也加入了點擊事件Block的回調處理 這只是我的一些經驗分享 大家可以根據(jù)喜好去選擇設置UIButton還是通過這個辦法
2煮仇。有時候會出現(xiàn)文字大小不一致或者文字時可以點擊的也會單獨拿出來 。同云。今天沒時間暫時寫到這吧俗扇。
注:輕噴吧。還有很多完善的地方 會慢慢加進來
代碼地址來這里吧

如何使用

圖片在上邊 + 文字在下邊

<pre><code>
UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(4, 20, 100, 80)]; view1.backgroundColor = [UIColor orangeColor]; [view1 setImageLocation:PJBImageLocationCenterUp interval:0 image:[UIImage imageNamed:@"ICON"] attributedText:[[NSAttributedString alloc] initWithString:@"圖片在上邊 + 文字在下邊"] spacing:0]; view1.pjTitleLabel.textColor = [UIColor whiteColor]; view1.pjTitleLabel.font = [UIFont systemFontOfSize:12]; view1.pjTitleLabel.numberOfLines = 2; [self.view addSubview:view1];
</code></pre>

圖片在左邊 + 文字在右邊

<pre><code>
UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(4, 120, 150, 80)]; view2.backgroundColor = [UIColor orangeColor]; [view2 setImageLocation:PJBImageLocationCenterLeft interval:0 image:[UIImage imageNamed:@"ICON"] attributedText:[[NSAttributedString alloc] initWithString:@"圖片在左邊 + 文字在右邊"] spacing:0]; view2.pjTitleLabel.textColor = [UIColor whiteColor]; view2.pjTitleLabel.font = [UIFont systemFontOfSize:12]; view2.pjTitleLabel.numberOfLines = 2; [self.view addSubview:view2];
</code></pre>

圖片在下邊 + 文字在上邊

<pre><code>
UIView *view3 = [[UIView alloc] initWithFrame:CGRectMake(4, 220, 100, 80)]; view3.backgroundColor = [UIColor orangeColor]; [view3 setImageLocation:PJBImageLocationCenterDown interval:0 image:[UIImage imageNamed:@"ICON"] attributedText:[[NSAttributedString alloc] initWithString:@"圖片在下邊 + 文字在上邊"] spacing:0]; view3.pjTitleLabel.textColor = [UIColor whiteColor]; view3.pjTitleLabel.font = [UIFont systemFontOfSize:12]; view3.pjTitleLabel.numberOfLines = 2; [self.view addSubview:view3];
</code></pre>

圖片在右邊 + 文字在左邊

<pre><code>
`
UIView *view4 = [[UIView alloc] initWithFrame:CGRectMake(4, 320, 150, 80)];
view4.backgroundColor = [UIColor orangeColor];
[view4 setImageLocation:PJBImageLocationCenterRight
interval:-10
image:[UIImage imageNamed:@"ICON"]
attributedText:[[NSAttributedString alloc] initWithString:@"圖片在右邊 + 文字在左邊"]
spacing:-10];
view4.pjTitleLabel.textColor = [UIColor whiteColor];
view4.pjTitleLabel.font = [UIFont systemFontOfSize:12];
view4.pjTitleLabel.numberOfLines = 2;
[self.view addSubview:view4];

view4.pjTapBlock = ^(){
    NSLog(@"你點我干嗎别威!");
};

`
</code></pre>

圖片在左邊 + 文字在右邊 + 字體不一樣大小

<pre><code>
`
UIView *view5 = [[UIView alloc] initWithFrame:CGRectMake(4, 410, 300, 80)];
view5.backgroundColor = [UIColor orangeColor];

NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] init];

AttributedLayoutModel *model1 = [[AttributedLayoutModel alloc] init];
model1.text = @"第一段文字";
model1.textFont = [UIFont systemFontOfSize:14];
model1.textColor = [UIColor whiteColor];

AttributedLayoutModel *model2 = [[AttributedLayoutModel alloc] init];
model2.text = @"第二段文字";
model2.textFont = [UIFont boldSystemFontOfSize:18];
model2.textColor = [UIColor yellowColor];


AttributedLayoutModel *model3 = [[AttributedLayoutModel alloc] init];
model3.text = @"第三段文字";
model3.textFont = [UIFont boldSystemFontOfSize:14];
model3.textColor = [UIColor redColor];

[attributedText setDirection:AttributeDirectionHorizontal
            layoutModelArray:@[model1,model2,model3]
                     spacing:10
                   alignment:NSTextAlignmentCenter];

[view5 setImageLocation:PJBImageLocationCenterLeft
               interval:0
                  image:[UIImage imageNamed:@"ICON"]
         attributedText:attributedText
                spacing:0];

[self.view addSubview:view5];

`
</code></pre>

圖片在左邊 + 文字在右邊 + 字體不一樣大小

<pre><code>
UIView *view6 = [[UIView alloc] initWithFrame:CGRectMake(200, 20, 150, 150)];
view6.backgroundColor = [UIColor orangeColor];

NSMutableAttributedString *attributedText1 = [[NSMutableAttributedString alloc] init];

AttributedLayoutModel *model4 = [[AttributedLayoutModel alloc] init];
model4.text = @"1 第一段文字";
model4.textFont = [UIFont systemFontOfSize:14];
model4.textColor = [UIColor whiteColor];
model4.textSpacing = 5;
model4.textAlignment = NSTextAlignmentLeft;

AttributedLayoutModel *model5 = [[AttributedLayoutModel alloc] init];
model5.text = @"2 第二段文字";
model5.textFont = [UIFont boldSystemFontOfSize:18];
model5.textColor = [UIColor yellowColor];
model5.textSpacing = 10;
model5.textAlignment = NSTextAlignmentCenter;

AttributedLayoutModel *model6 = [[AttributedLayoutModel alloc] init];
model6.text = @"3 第三段文字";
model6.textFont = [UIFont boldSystemFontOfSize:14];
model6.textColor = [UIColor redColor];
model6.textSpacing = 20;
model6.textAlignment = NSTextAlignmentRight;

[attributedText1 setDirection:AttributeDirectionVertical
             layoutModelArray:@[model4,model5,model6]];

[view6 setImageLocation:PJBImageLocationCenterUp
               interval:0
                  image:[UIImage imageNamed:@"ICON"]
         attributedText:attributedText1
                spacing:0];

[self.view addSubview:view6];

</code></pre>

圖片在左邊 + 文字在右邊 + 字體不一樣大小

<pre><code>

UIView *view7 = [[UIView alloc] initWithFrame:CGRectMake(200, 180, 150, 150)];

view7.backgroundColor = [UIColor orangeColor];

NSMutableAttributedString *attributedText2 = [[NSMutableAttributedString alloc] init];

AttributedLayoutModel *model7 = [[AttributedLayoutModel alloc] init];
model7.text = @"1 第一段文字";
model7.textFont = [UIFont systemFontOfSize:14];
model7.textColor = [UIColor whiteColor];
model7.textSpacing = 5;
model7.textAlignment = NSTextAlignmentLeft;

AttributedLayoutModel *model8 = [[AttributedLayoutModel alloc] init];
model8.text = @"2 第二段文字";
model8.textFont = [UIFont boldSystemFontOfSize:18];
model8.textColor = [UIColor yellowColor];
model8.textSpacing = 10;
model8.textAlignment = NSTextAlignmentCenter;

AttributedLayoutModel *model9 = [[AttributedLayoutModel alloc] init];
model9.text = @"3 第三段文字";
model9.textFont = [UIFont boldSystemFontOfSize:14];
model9.textColor = [UIColor redColor];
model9.textSpacing = 20;
model9.textAlignment = NSTextAlignmentRight;


[attributedText2 setDirection:AttributeDirectionVertical
            layoutModelArray:@[model7,model8,model9]
                     spacing:10
                   alignment:NSTextAlignmentCenter];

[view7 setImageLocation:PJBImageLocationCenterUp
               interval:0
                  image:[UIImage imageNamed:@"ICON"]
         attributedText:attributedText2
                spacing:0];

[self.view addSubview:view7];

</code></pre>
下面是效果圖

Paste_Image.png

未完待續(xù)。驴剔。省古。。丧失。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末豺妓,一起剝皮案震驚了整個濱河市版扩,隨后出現(xiàn)的幾起案子翎朱,更是在濱河造成了極大的恐慌,老刑警劉巖露戒,帶你破解...
    沈念sama閱讀 217,406評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件描验,死亡現(xiàn)場離奇詭異白嘁,居然都是意外死亡,警方通過查閱死者的電腦和手機膘流,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,732評論 3 393
  • 文/潘曉璐 我一進店門絮缅,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人睡扬,你說我怎么就攤上這事盟蚣∈蛭觯” “怎么了卖怜?”我有些...
    開封第一講書人閱讀 163,711評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長阐枣。 經常有香客問我马靠,道長奄抽,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,380評論 1 293
  • 正文 為了忘掉前任甩鳄,我火速辦了婚禮逞度,結果婚禮上,老公的妹妹穿的比我還像新娘妙啃。我一直安慰自己档泽,他們只是感情好,可當我...
    茶點故事閱讀 67,432評論 6 392
  • 文/花漫 我一把揭開白布揖赴。 她就那樣靜靜地躺著馆匿,像睡著了一般。 火紅的嫁衣襯著肌膚如雪燥滑。 梳的紋絲不亂的頭發(fā)上渐北,一...
    開封第一講書人閱讀 51,301評論 1 301
  • 那天,我揣著相機與錄音铭拧,去河邊找鬼赃蛛。 笑死,一個胖子當著我的面吹牛搀菩,可吹牛的內容都是我干的呕臂。 我是一名探鬼主播,決...
    沈念sama閱讀 40,145評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼肪跋,長吁一口氣:“原來是場噩夢啊……” “哼诵闭!你這毒婦竟也來了?” 一聲冷哼從身側響起澎嚣,我...
    開封第一講書人閱讀 39,008評論 0 276
  • 序言:老撾萬榮一對情侶失蹤疏尿,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后易桃,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體褥琐,經...
    沈念sama閱讀 45,443評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,649評論 3 334
  • 正文 我和宋清朗相戀三年晤郑,在試婚紗的時候發(fā)現(xiàn)自己被綠了敌呈。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,795評論 1 347
  • 序言:一個原本活蹦亂跳的男人離奇死亡造寝,死狀恐怖磕洪,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情诫龙,我是刑警寧澤析显,帶...
    沈念sama閱讀 35,501評論 5 345
  • 正文 年R本政府宣布,位于F島的核電站签赃,受9級特大地震影響谷异,放射性物質發(fā)生泄漏分尸。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,119評論 3 328
  • 文/蒙蒙 一歹嘹、第九天 我趴在偏房一處隱蔽的房頂上張望箩绍。 院中可真熱鬧,春花似錦尺上、人聲如沸材蛛。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,731評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽仰税。三九已至,卻和暖如春抽诉,著一層夾襖步出監(jiān)牢的瞬間陨簇,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,865評論 1 269
  • 我被黑心中介騙來泰國打工迹淌, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留河绽,地道東北人。 一個月前我還...
    沈念sama閱讀 47,899評論 2 370
  • 正文 我出身青樓唉窃,卻偏偏與公主長得像耙饰,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子纹份,可洞房花燭夜當晚...
    茶點故事閱讀 44,724評論 2 354

推薦閱讀更多精彩內容