123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- //
- // UIButton+ImageTitleStyle.m
- // ACGPro
- //
- // Created by kangzhiqiang on 16/5/4.
- // Copyright © 2016年 CCJ. All rights reserved.
- //
- #import "UIButton+ImageTitleStyle.h"
- @implementation UIButton (ImageTitleStyle)
- -(void)setButtonImageTitleStyle:(ButtonImageTitleStyle)style padding:(CGFloat)padding
- {
- if (self.imageView.image != nil && self.titleLabel.text != nil)
- {
- self.adjustsImageWhenHighlighted=NO;
-
- //先还原
- self.titleEdgeInsets = UIEdgeInsetsZero;
- self.imageEdgeInsets = UIEdgeInsetsZero;
-
- CGRect imageRect = self.imageView.frame;
- CGRect titleRect = self.titleLabel.frame;
-
- CGFloat totalHeight = imageRect.size.height + padding + titleRect.size.height;
- CGFloat selfHeight = self.frame.size.height;
- CGFloat selfWidth = self.frame.size.width;
-
- switch (style) {
- case ButtonImageTitleStyleLeft:
- if (padding != 0)
- {
- self.titleEdgeInsets = UIEdgeInsetsMake(0,
- padding/2,
- 0,
- -padding/2);
-
- self.imageEdgeInsets = UIEdgeInsetsMake(0,
- -padding/2,
- 0,
- padding/2);
- }
- break;
- case ButtonImageTitleStyleRight:
- {
- //图片在右,文字在左
- self.titleEdgeInsets = UIEdgeInsetsMake(0,
- -(imageRect.size.width + padding/2),
- 0,
- (imageRect.size.width + padding/2));
-
- self.imageEdgeInsets = UIEdgeInsetsMake(0,
- (titleRect.size.width+ padding/2),
- 0,
- -(titleRect.size.width+ padding/2));
- }
- break;
- case ButtonImageTitleStyleTop:
- {
- //图片在上,文字在下
- self.titleEdgeInsets = UIEdgeInsetsMake(((selfHeight - totalHeight)/2 + imageRect.size.height + padding - titleRect.origin.y),
- (selfWidth/2 - titleRect.origin.x - titleRect.size.width /2) - (selfWidth - titleRect.size.width) / 2,
- -((selfHeight - totalHeight)/2 + imageRect.size.height + padding - titleRect.origin.y),
- -(selfWidth/2 - titleRect.origin.x - titleRect.size.width /2) - (selfWidth - titleRect.size.width) / 2);
-
- self.imageEdgeInsets = UIEdgeInsetsMake(((selfHeight - totalHeight)/2 - imageRect.origin.y),
- (selfWidth /2 - imageRect.origin.x - imageRect.size.width / 2),
- -((selfHeight - totalHeight)/2 - imageRect.origin.y),
- -(selfWidth /2 - imageRect.origin.x - imageRect.size.width / 2));
-
- }
- break;
- case ButtonImageTitleStyleBottom:
- {
- //图片在下,文字在上。
- self.titleEdgeInsets = UIEdgeInsetsMake(((selfHeight - totalHeight)/2 - titleRect.origin.y),
- (selfWidth/2 - titleRect.origin.x - titleRect.size.width / 2) - (selfWidth - titleRect.size.width) / 2,
- -((selfHeight - totalHeight)/2 - titleRect.origin.y),
- -(selfWidth/2 - titleRect.origin.x - titleRect.size.width / 2) - (selfWidth - titleRect.size.width) / 2);
-
- self.imageEdgeInsets = UIEdgeInsetsMake(((selfHeight - totalHeight)/2 + titleRect.size.height + padding - imageRect.origin.y),
- (selfWidth /2 - imageRect.origin.x - imageRect.size.width / 2),
- -((selfHeight - totalHeight)/2 + titleRect.size.height + padding - imageRect.origin.y),
- -(selfWidth /2 - imageRect.origin.x - imageRect.size.width / 2));
- }
- break;
- case ButtonImageTitleStyleCenterTop:
- {
- self.titleEdgeInsets = UIEdgeInsetsMake(-(titleRect.origin.y - padding),
- (selfWidth / 2 - titleRect.origin.x - titleRect.size.width / 2) - (selfWidth - titleRect.size.width) / 2,
- (titleRect.origin.y - padding),
- -(selfWidth / 2 - titleRect.origin.x - titleRect.size.width / 2) - (selfWidth - titleRect.size.width) / 2);
-
- self.imageEdgeInsets = UIEdgeInsetsMake(0,
- (selfWidth / 2 - imageRect.origin.x - imageRect.size.width / 2),
- 0,
- -(selfWidth / 2 - imageRect.origin.x - imageRect.size.width / 2));
- }
- break;
- case ButtonImageTitleStyleCenterBottom:
- {
- self.titleEdgeInsets = UIEdgeInsetsMake((selfHeight - padding - titleRect.origin.y - titleRect.size.height),
- (selfWidth / 2 - titleRect.origin.x - titleRect.size.width / 2) - (selfWidth - titleRect.size.width) / 2,
- -(selfHeight - padding - titleRect.origin.y - titleRect.size.height),
- -(selfWidth / 2 - titleRect.origin.x - titleRect.size.width / 2) - (selfWidth - titleRect.size.width) / 2);
-
- self.imageEdgeInsets = UIEdgeInsetsMake(0,
- (selfWidth / 2 - imageRect.origin.x - imageRect.size.width / 2),
- 0,
- -(selfWidth / 2 - imageRect.origin.x - imageRect.size.width / 2));
- }
- break;
- case ButtonImageTitleStyleCenterUp:
- {
- self.titleEdgeInsets = UIEdgeInsetsMake(-(titleRect.origin.y + titleRect.size.height - imageRect.origin.y + padding),
- (selfWidth / 2 - titleRect.origin.x - titleRect.size.width / 2) - (selfWidth - titleRect.size.width) / 2,
- (titleRect.origin.y + titleRect.size.height - imageRect.origin.y + padding),
- -(selfWidth / 2 - titleRect.origin.x - titleRect.size.width / 2) - (selfWidth - titleRect.size.width) / 2);
-
- self.imageEdgeInsets = UIEdgeInsetsMake(0,
- (selfWidth / 2 - imageRect.origin.x - imageRect.size.width / 2),
- 0,
- -(selfWidth / 2 - imageRect.origin.x - imageRect.size.width / 2));
- }
- break;
- case ButtonImageTitleStyleCenterDown:
- {
- self.titleEdgeInsets = UIEdgeInsetsMake((imageRect.origin.y + imageRect.size.height - titleRect.origin.y + padding),
- (selfWidth / 2 - titleRect.origin.x - titleRect.size.width / 2) - (selfWidth - titleRect.size.width) / 2,
- -(imageRect.origin.y + imageRect.size.height - titleRect.origin.y + padding),
- -(selfWidth / 2 - titleRect.origin.x - titleRect.size.width / 2) - (selfWidth - titleRect.size.width) / 2);
-
- self.imageEdgeInsets = UIEdgeInsetsMake(0,
- (selfWidth / 2 - imageRect.origin.x - imageRect.size.width / 2),
- 0,
- -(selfWidth / 2 - imageRect.origin.x - imageRect.size.width / 2));
- }
- break;
- case ButtonImageTitleStyleRightLeft:
- {
- //图片在右,文字在左,距离按钮两边边距
-
- self.titleEdgeInsets = UIEdgeInsetsMake(0,
- -(titleRect.origin.x - padding),
- 0,
- (titleRect.origin.x - padding));
-
- self.imageEdgeInsets = UIEdgeInsetsMake(0,
- (selfWidth - padding - imageRect.origin.x - imageRect.size.width),
- 0,
- -(selfWidth - padding - imageRect.origin.x - imageRect.size.width));
- }
-
- break;
-
- case ButtonImageTitleStyleLeftRight:
- {
- //图片在左,文字在右,距离按钮两边边距
-
- self.titleEdgeInsets = UIEdgeInsetsMake(0,
- (selfWidth - padding - titleRect.origin.x - titleRect.size.width),
- 0,
- -(selfWidth - padding - titleRect.origin.x - titleRect.size.width));
-
- self.imageEdgeInsets = UIEdgeInsetsMake(0,
- -(imageRect.origin.x - padding),
- 0,
- (imageRect.origin.x - padding));
-
-
-
- }
- break;
- default:
- break;
- }
- }
- else {
- self.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
- self.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
- }
-
- }
- - (void)setHighlighted:(BOOL)highlighted{
-
- }
- @end
|