在ios开发过程中,我们经常会遇到这样的情况:要在一行中显示文字和图片,如图所示:
我们可以在一个view中添加labe来显示文字,添加UIImageView来显示图片,
现在我们只要用一个Button就可以实现以上的功能。
在UIButton的API文件中,我们可以看到有以下两个属性:
@property(nonatomic) UIEdgeInsets titleEdgeInsets; //设置title显示的位置
@property(nonatomic) UIEdgeInsets imageEdgeInsets; //设置image显示的位置
只需要对EdgeInsets的设置即可
typedef struct UIEdgeInsets {
CGFloat top, left, bottom, right;
} UIEdgeInsets;
UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(100, 100, 200,44)];
[button setBackgroundColor:[UIColor grayColor]];
[button setTitle:@"弹出" forState:UIControlStateNormal];
//设置title的间距
button.titleEdgeInsets = UIEdgeInsetsMake(10, 16, 10, 200 - 62);
[button setImage:[UIImage imageNamed:@"icon_arrow.png"] forState:UIControlStateNormal];
//设置image的间距
button.imageEdgeInsets = UIEdgeInsetsMake(27, 200 - 30, 27, 10);
button.layer.shadowColor = [UIColor redColor].CGColor;
button.layer.shadowOffset = CGSizeMake(5, 5);
运行效果如下: