UIButton 图片文字居左居右的设置

主要用到的就是

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;//设置按钮的位置居左

如果不设置按钮中的图片文字会居中显示


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值