iOS 之UIButton左文右图

对于button,当添加了图片时,默认是左图右文的

‘[self.pageViewsLB setImage:[UIImage imageNamed:@"read"] forState:UIControlStateNormal];
[self.commentsLB setImage:[UIImage imageNamed:@"commentNum_ico"] forState:UIControlStateNormal];
[self.dateLB setImage:[UIImage imageNamed:@"time"] forState:UIControlStateNormal];
[self.commentsLB.imageView setContentMode:UIViewContentModeScaleAspectFit];
[self.dateLB.imageView setContentMode:UIViewContentModeScaleAspectFit];’

但很多时候应需求需要,我们需要把图片放在右边,文字在左边,如下

‘[btn setTitleEdgeInsets:UIEdgeInsetsMake(0, -btn.imageView.bounds.size.width, 0, btn.imageView.bounds.size.width)];
[btn setImageEdgeInsets:UIEdgeInsetsMake(0, btn.titleLabel.bounds.size.width, 0, -btn.titleLabel.bounds.size.width)];’

有时候也需要把图片放在button的最右,文字最左,如下

'btn.titleEdgeInsets = UIEdgeInsetsMake(0, -CGRectGetMaxX(btn.imageView.frame), 0, CGRectGetMaxX(btn.imageView.frame));
btn.imageEdgeInsets = UIEdgeInsetsMake(0, SCREEN_WIDTH-10-(CGRectGetMaxX(btn.imageView.frame)), 0, -(SCREEN_WIDTH-10-(CGRectGetMaxX(btn.imageView.frame))));'
posted @ 2016-11-15 15:42  YaoJinye  阅读(3802)  评论(0编辑  收藏  举报