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>
下面是效果圖
未完待續(xù)。驴剔。省古。。丧失。