cool hit counter Objective-CUIButton Custom Image Position_Intefrankly

Objective-CUIButton Custom Image Position


It's not uncommon in projects to have a button with an image placed on it, possibly above, below, left or right of the header. If one button, you can just set titleEdgeInsets and imageEdgeInsets separately, but if there are many Similar button but setting it up would be a pain. To solve this problem we write a Category for UIButton

#import <UIKit/UIKit.h>

typedef NS_ENUM(NSUInteger,ButtonEdgeInsetsStyle) {
    ButtonEdgeInsetsStyleTop,
    ButtonEdgeInsetsStyleLeft,
    ButtonEdgeInsetsStyleRight,
    ButtonEdgeInsetsStyleBottom,
};

@interface UIButton (EdgeInsets)

/** Non-selected images*/
@property (copy, nonatomic) NSString *normalImageName;
/** Select the picture*/
@property (copy, nonatomic) NSString *selectedImageName;
/** Default image*/
@property (copy, nonatomic) NSString *defaultImageName;

- (void)setImagePositionWithEdgeInsetsStyle:(ButtonEdgeInsetsStyle)style imageSpace:(CGFloat)space;

- (void)setDefaultImageWithNamed:(NSString *)imgName;

@end

Why set three image attributes, in order to solve such needs, for example, there are many filter buttons, the product manager said there is no sorting with one image, ascending order with one image, descending order with one image.

#import "UIButton+EdgeInsets.h"
#import <objc/runtime.h>

@implementation UIButton (EdgeInsets)

- (void)setImagePositionWithEdgeInsetsStyle:(ButtonEdgeInsetsStyle)style imageSpace:(CGFloat)space
{
    
    //  getimageView harmonytitleLabel width、 loud
    CGFloat imageWith = self.currentImage.size.width;
    CGFloat imageHeight = self.currentImage.size.height;
    
    //  gainTitleSize
    CGSize titleSize = [self sizeForText:[self titleForState:UIControlStateNormal] font:self.titleLabel.font size:CGSizeMake(HUGE, HUGE) mode:self.titleLabel.lineBreakMode];
    
    CGFloat labelWidth = titleSize.width;
    CGFloat labelHeight = titleSize.height;
   
    //  Declaration of the globalimageEdgeInsets harmonylabelEdgeInsets
    UIEdgeInsets imageEdgeInsets = UIEdgeInsetsZero;
    UIEdgeInsets labelEdgeInsets = UIEdgeInsetsZero;
    
    //  depending onstyle harmonyspace getimageEdgeInsets harmonylabelEdgeInsets values
    switch (style) {
        case ButtonEdgeInsetsStyleTop: {
            imageEdgeInsets = UIEdgeInsetsMake(-labelHeight-space, 0, 0, -labelWidth);
            labelEdgeInsets = UIEdgeInsetsMake(0, -imageWith, -imageHeight-space, 0);
        }
            break;
        case ButtonEdgeInsetsStyleLeft: {
            imageEdgeInsets = UIEdgeInsetsMake(0, -space, 0, space);
            labelEdgeInsets = UIEdgeInsetsMake(0, space, 0, -space);
        }
            break;
        case ButtonEdgeInsetsStyleBottom: {
            imageEdgeInsets = UIEdgeInsetsMake(0, 0, -labelHeight-space, -labelWidth);
            labelEdgeInsets = UIEdgeInsetsMake(-imageHeight-space, -imageWith, 0, 0);
        }
            break;
        case ButtonEdgeInsetsStyleRight: {
            imageEdgeInsets = UIEdgeInsetsMake(0, labelWidth+space, 0, -labelWidth-space);
            labelEdgeInsets = UIEdgeInsetsMake(0, -imageWith-space, 0, imageWith+space);
        }
            break;
        default:
            break;
    }
    //  assign a value to
    self.titleEdgeInsets = labelEdgeInsets;
    self.imageEdgeInsets = imageEdgeInsets;
}

- (void)setSelected:(BOOL)selected
{
    if (selected) {
        [self setImage:[UIImage imageNamed:self.selectedImageName] forState:UIControlStateNormal];
    }
    else{
         [self setImage:[UIImage imageNamed:self.normalImageName] forState:UIControlStateNormal];
    }
}

- (void)setDefaultImageWithNamed:(NSString *)imgName
{
    [self setImage:[UIImage imageNamed:imgName] forState:UIControlStateNormal];
    self.defaultImageName = imgName;
}

#pragma mark - pravite Method
static const void *normalImageNameKey = &normalImageNameKey;
- (void)setNormalImageName:(NSString *)normalImageName
{
    objc_setAssociatedObject(self, normalImageNameKey, normalImageName,  OBJC_ASSOCIATION_COPY);
}
- (NSString *)normalImageName
{
    return objc_getAssociatedObject(self, normalImageNameKey);
}

static const void *selectedImageNameKey = &selectedImageNameKey;
- (void)setSelectedImageName:(NSString *)selectedImageName
{
    objc_setAssociatedObject(self, selectedImageNameKey, selectedImageName,  OBJC_ASSOCIATION_COPY);
}
- (NSString *)selectedImageName
{
    return objc_getAssociatedObject(self, selectedImageNameKey);
}

static const void *defaultImageNameKey = &defaultImageNameKey;
- (void)setDefaultImageName:(NSString *)defaultImageName
{
    objc_setAssociatedObject(self, defaultImageNameKey, defaultImageName,  OBJC_ASSOCIATION_COPY);
}
- (NSString *)defaultImageName
{
    return objc_getAssociatedObject(self, defaultImageNameKey);
}


#pragma mark - Drawing
- (CGSize)sizeForText:(NSString *)text font:(UIFont *)font size:(CGSize)size mode:(NSLineBreakMode)lineBreakMode {
    CGSize result;
    if (!font) font = [UIFont systemFontOfSize:12];
    if ([self respondsToSelector:@selector(boundingRectWithSize:options:attributes:context:)]) {
        NSMutableDictionary *attr = [NSMutableDictionary new];
        attr[NSFontAttributeName] = font;
        if (lineBreakMode != NSLineBreakByWordWrapping) {
            NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
            paragraphStyle.lineBreakMode = lineBreakMode;
            attr[NSParagraphStyleAttributeName] = paragraphStyle;
        }
        CGRect rect = [text boundingRectWithSize:size
                                         options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
                                      attributes:attr context:nil];
        result = rect.size;
    } else {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
        result = [text sizeWithFont:font constrainedToSize:size lineBreakMode:lineBreakMode];
#pragma clang diagnostic pop
    }
    return result;
}

invoke

#import "ViewController.h"
#import "UIButton+EdgeInsets.h"

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UIButton *buttonTop;
@property (weak, nonatomic) IBOutlet UIButton *buttonBottom;
@property (weak, nonatomic) IBOutlet UIButton *buttonLeft;
@property (weak, nonatomic) IBOutlet UIButton *buttonRight;

@property (weak, nonatomic) IBOutlet UIButton *button4;
@property (weak, nonatomic) IBOutlet UIButton *button5;
@property (weak, nonatomic) IBOutlet UIButton *button6;

@property (assign, nonatomic) BOOL isSelected;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    [self.buttonTop setDefaultImageWithNamed:@"sort_filter_default_img"];
    [self.buttonTop setImagePositionWithEdgeInsetsStyle:ButtonEdgeInsetsStyleTop imageSpace:0];

    [self.buttonBottom setDefaultImageWithNamed:@"sort_filter_default_img"];
    [self.buttonBottom setImagePositionWithEdgeInsetsStyle:ButtonEdgeInsetsStyleBottom imageSpace:0];
    
    [self.buttonLeft setDefaultImageWithNamed:@"sort_filter_default_img"];
    [self.buttonLeft setImagePositionWithEdgeInsetsStyle:ButtonEdgeInsetsStyleLeft imageSpace:5];
    
    [self.buttonRight setDefaultImageWithNamed:@"sort_filter_default_img"];
    [self.buttonRight setImagePositionWithEdgeInsetsStyle:ButtonEdgeInsetsStyleRight imageSpace:5];
    
    [self.button4 setDefaultImageWithNamed:@"sort_filter_asc_img"];
    [self.button4 setSelectedImageName:@"sort_filter_asc_img"];
    [self.button4 setNormalImageName:@"sort_filter_desc_img"];
    [self.button4 setImagePositionWithEdgeInsetsStyle:ButtonEdgeInsetsStyleRight imageSpace:0];
    self.isSelected = YES;
    
    [self.button5 setDefaultImageWithNamed:@"sort_filter_default_img"];
    [self.button5 setImagePositionWithEdgeInsetsStyle:ButtonEdgeInsetsStyleRight imageSpace:0];
    
    [self.button6 setDefaultImageWithNamed:@"sort_filter_default_img"];
    [self.button6 setImagePositionWithEdgeInsetsStyle:ButtonEdgeInsetsStyleRight imageSpace:0];
}

- (IBAction)btn4Action:(UIButton *)sender {
    
    self.isSelected = !self.isSelected;
    sender.selected = self.isSelected;
}

effects

Simulator Screen Shot - iPhone 5s - 2018-05-31 at 11.28.51.png

For those of you who need to take a look git address.https://github.com/destinyzhao/ButtonEdgeInsets


Recommended>>
1、Budweiser moves full steam aheadAI What changes will it bring to the beer industry
2、倒计时三天101信息学集训队选拔开始
3、python验证码识别实战
4、Baidu goes live with deep learning visualization tool VisualDL training results what you see is what you get
5、Our first driverless bus

    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号