setImageEdgeInsets
以及
setTitleEdgeInsets
分别设置图片以及文字
代码
/**
* 左边图片,右边文字
*/
- (void) setLeftAndRightImageType:(UIImage *)image withTitle:(NSString *)title forState:(UIControlState)stateType {
//UIEdgeInsetsMake(CGFloat top, CGFloat left, CGFloat bottom, CGFloat right)
//CGSize titleSize = [title sizeWithFont:[UIFont systemFontOfSize:12.0]];
[self.imageView setContentMode:UIViewContentModeCenter];
[self setImageEdgeInsets:UIEdgeInsetsMake(-8.0,
0.0,
0.0,
0.0)];
[self setImage:image forState:stateType];
[self.titleLabel setContentMode:UIViewContentModeCenter];
[self.titleLabel setBackgroundColor:[UIColor clearColor]];
[self.titleLabel setFont:[UIFont systemFontOfSize:12.0]];
[self.titleLabel setTextColor:[UIColor darkGrayColor]];
[self setTitleEdgeInsets:UIEdgeInsetsMake(-8.0,
0.0,
0.0,
-5.0)];
[self setTitle:title forState:stateType];
}
UIButton *btnAroundPlace = [[UIButton alloc] initWithFrame:CGRectMake(gapXY*(j+1)+btnWith*j, btnHeight*btnLineCount+5, btnWith, btnHeight)];
btnAroundPlace.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;//设置按钮的位置居左
[btnAroundPlace setLeftAndRightImageType:[UIImage imageNamed:@"rightLoop.png"] withTitle:[arraylogoTemp objectAtIndex:i] forState:UIControlStateNormal];
btnAroundPlace.titleLabel.font = [UIFont systemFontOfSize:12.0];
[btnAroundPlace setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
[cell.contentView addSubview:btnAroundPlace];
btnAroundPlace.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;//设置按钮的位置居左
如果不设置按钮中的图片文字会居中显示