IOS总结实现UIButton的图文混排 同时显示文字和图片
2015-05-28 22:44
741 查看
默认情况下,在UIButton中既添加文字,又添加图片的时候,图片是显示在文字的左边。
当我们实际需要图片显示在文字的右边的时候,需要重写UIButton的方法。
如下代码实现的功能就是:文字在左边,图片在右边。
- (id)initWithFrame:(CGRect)frame
{
self = [super
initWithFrame:frame];
if (self)
{
self.backgroundColor = [UIColor
purpleColor];
[self
setTitleColor:[UIColor
blackColor] forState:UIControlStateNormal];
self.titleLabel.textAlignment =
NSTextAlignmentCenter;
self.imageView.contentMode =
UIViewContentModeCenter;
}
return
self;
}
- (CGRect)titleRectForContentRect:(CGRect)contentRect
{
CGFloat titleX =
0;
CGFloat titleY =
0;
CGFloat titleW =
self.width -
IWTitleImageWidth;
CGFloat titleH =
self.height;
return
CGRectMake(titleX, titleY, titleW, titleH);
}
- (CGRect)imageRectForContentRect:(CGRect)contentRect
{
CGFloat imageX =
self.width -
IWTitleImageWidth;
CGFloat imageY =
0;
CGFloat imageH =
self.height;
CGFloat imageW =
IWTitleImageWidth;
return
CGRectMake(imageX, imageY, imageW, imageH);
}
根据此原理,我们也可以调整图片在上,文字在下。
还有一种情况,就是让图片和文字有一定的距离,代码如下:
// 添加分享按钮
UIButton *shareButton = [[UIButton
alloc] init];
// 设置文字和颜色
[shareButton setTitle:@"分享给大家"
forState:UIControlStateNormal];
[shareButton setTitleColor:[UIColor
blackColor] forState:UIControlStateNormal];
// 设置图标
[shareButton setImage:[UIImage
imageWithName:@"new_feature_share_false"]
forState:UIControlStateNormal];
[shareButton setImage:[UIImage
imageWithName:@"new_feature_share_true"]
forState:UIControlStateSelected];
// 设置frame
shareButton.width =
200;
shareButton.height =
35;
shareButton.centerX =
self.view.width *
0.5;
shareButton.centerY =
self.view.height *
0.7;
shareButton.titleEdgeInsets =
UIEdgeInsetsMake(0,
20, 0,
0);//四个参数分别是距离top,left,bottom,right的距离。
当我们实际需要图片显示在文字的右边的时候,需要重写UIButton的方法。
如下代码实现的功能就是:文字在左边,图片在右边。
- (id)initWithFrame:(CGRect)frame
{
self = [super
initWithFrame:frame];
if (self)
{
self.backgroundColor = [UIColor
purpleColor];
[self
setTitleColor:[UIColor
blackColor] forState:UIControlStateNormal];
self.titleLabel.textAlignment =
NSTextAlignmentCenter;
self.imageView.contentMode =
UIViewContentModeCenter;
}
return
self;
}
- (CGRect)titleRectForContentRect:(CGRect)contentRect
{
CGFloat titleX =
0;
CGFloat titleY =
0;
CGFloat titleW =
self.width -
IWTitleImageWidth;
CGFloat titleH =
self.height;
return
CGRectMake(titleX, titleY, titleW, titleH);
}
- (CGRect)imageRectForContentRect:(CGRect)contentRect
{
CGFloat imageX =
self.width -
IWTitleImageWidth;
CGFloat imageY =
0;
CGFloat imageH =
self.height;
CGFloat imageW =
IWTitleImageWidth;
return
CGRectMake(imageX, imageY, imageW, imageH);
}
根据此原理,我们也可以调整图片在上,文字在下。
还有一种情况,就是让图片和文字有一定的距离,代码如下:
// 添加分享按钮
UIButton *shareButton = [[UIButton
alloc] init];
// 设置文字和颜色
[shareButton setTitle:@"分享给大家"
forState:UIControlStateNormal];
[shareButton setTitleColor:[UIColor
blackColor] forState:UIControlStateNormal];
// 设置图标
[shareButton setImage:[UIImage
imageWithName:@"new_feature_share_false"]
forState:UIControlStateNormal];
[shareButton setImage:[UIImage
imageWithName:@"new_feature_share_true"]
forState:UIControlStateSelected];
// 设置frame
shareButton.width =
200;
shareButton.height =
35;
shareButton.centerX =
self.view.width *
0.5;
shareButton.centerY =
self.view.height *
0.7;
shareButton.titleEdgeInsets =
UIEdgeInsetsMake(0,
20, 0,
0);//四个参数分别是距离top,left,bottom,right的距离。
相关文章推荐
- 让UILabel的文字顶部对齐
- UISearchBar和UISearchDisplayController
- iOSUI学习之--------将程序json字符串上传至服务器
- IOS 计算UILable字符串高度
- iOSUI学习之使用苹果系统自带的方法处理缓存问题
- 115.Distinct Subsequences
- 错误 CUICatalog: Invalid asset name supplied: (null), or invalid scale factor: 2.000000
- Next Permutation&&Permutation Sequence
- NEU 1132 Renew MST Quickly 增量最小生成树
- UVA10099 - The Tourist Guide(floyd + 最小值的最大化)
- 20150528 UI考试项目中,使用SHAI加密,生成请求URL
- [dp]poj1458 -Common Subsequence(LCS)
- 分类UIView+Extension:frame直接赋值方法
- 从客户端检测到有潜在危险的Request.Form值
- 为什么我们可以在非UI线程中更新UI
- XCODE UITextField 中的属性和用法
- Android 4.4 Graphic系统详解(4)HWUI概述
- IOS-UISlider的详细使用
- 用UIScroView实现图片的滚动
- 代码实现UIPickerView