iOS按钮左边文字右边图片

照葫芦画瓢就行。

- (void)setBtnTitle:(NSString *)btnTitle {
    _btnTitle = btnTitle;
    [self.btn setTitle:btnTitle forState:UIControlStateNormal];
    CGRect rect = [btnTitle boundingRectWithSize:CGSizeMake(1000, 100) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:15]} context:nil];
    //设置button正常状态下的图片
    [self.btn setImage:[UIImage imageNamed:@"xiangshang"] forState:UIControlStateNormal];
    [self.btn setImage:[UIImage imageNamed:@"xiangshang"] forState:UIControlStateHighlighted];
    //button标题的偏移量,这个偏移量是相对于图片的
    self.btn.titleEdgeInsets = UIEdgeInsetsMake(0, -self.btn.imageView.image.size.width-2.5, 0, self.btn.imageView.image.size.width+2.5);
    //button图片的偏移量,这个偏移量是相对于标题的
    self.btn.imageEdgeInsets = UIEdgeInsetsMake(0, rect.size.width+2.5, 0, -rect.size.width-2.5);
}

你可能感兴趣的:(iOS按钮左边文字右边图片)