美文网首页iOS学习iOS 开发每天分享优质文章iOS Developer
iOS UIButton的简单封装(对于图片和标题同时存在的情况

iOS UIButton的简单封装(对于图片和标题同时存在的情况

作者: vision_colion | 来源:发表于2016-11-30 14:25 被阅读169次

很多时候的需求是要求Button的文字和图片垂直居中对齐或者水平居中对齐,虽然可以通过
设置titleEdgeInsetsimageEdgeInsets。但是一直总调不好,也许是没理解要点吧,反正就是各种不爽。针对各种不服,楼主自己瞎折腾简单封装了一下,针对文字和图片垂直居中对齐或者水平居中对齐的给各位看官分享下


//
//  VisionBtn.h
//  Test
//
//  Created by ime on 2016/11/30.
//  Copyright © 2016年 Vision. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface VisionBtn : UIButton

//图片大小
@property(nonatomic,assign)CGSize iconsize;
//图片与标题间隙
@property(nonatomic,assign)CGFloat index;
//是否为垂直对齐(默认为否,即水平对齐)
@property BOOL isHorizonalCenter;
@end

//
//  VisionBtn.m
//  Test
//
//  Created by ime on 2016/11/30.
//  Copyright © 2016年 Vision. All rights reserved.
//

#import "VisionBtn.h"

@implementation VisionBtn

- (void)layoutSubviews
{
    [super layoutSubviews];
    if(self.titleLabel.text && self.imageView.image)
    {
        self.imageView.frame = CGRectMake(0, 0, self.iconsize.width, self.iconsize.height);
        CGFloat marginH = (self.frame.size.height - self.imageView.frame.size.height - self.titleLabel.frame.size.height)/3;
        
        if (self.isHorizonalCenter == YES) {//垂直居中
            //图片
            CGPoint imageCenter = self.imageView.center;
            imageCenter.x = self.frame.size.width/2;
            imageCenter.y = self.imageView.frame.size.height/2 + marginH;
            self.imageView.center = imageCenter;
            
            //文字
            CGRect newFrame = self.titleLabel.frame;
            newFrame.origin.x = 0;
            newFrame.origin.y = self.imageView.frame.origin.y + self.imageView.frame.size.height + self.index;
            newFrame.size.width = self.frame.size.width;
            newFrame.size.height = self.frame.size.height - self.imageView.frame.origin.y - self.imageView.frame.size.height;
            self.titleLabel.frame = newFrame;
            self.titleLabel.textAlignment = NSTextAlignmentCenter;
        }else{//水平居中
            //图片
            CGPoint imageCenter = self.imageView.center;
            imageCenter.x = self.imageView.frame.size.width/2;
            imageCenter.y = self.frame.size.height/2;
            self.imageView.center = imageCenter;
            
            //文字
            CGPoint labelCenter = self.titleLabel.center;
            labelCenter.x = self.imageView.frame.origin.x + self.imageView.frame.size.width + self.index + (self.frame.size.width - self.index - self.imageView.frame.size.width)/2;
            labelCenter.y = imageCenter.y;
            CGRect newFrame = self.titleLabel.frame;
            newFrame.origin.x = 0;
            newFrame.origin.y = 0;
            newFrame.size.width = self.frame.size.width - self.index - self.imageView.frame.size.width;
            newFrame.size.height = self.frame.size.height;
            self.titleLabel.frame = newFrame;
            self.titleLabel.center = labelCenter;
            self.titleLabel.textAlignment = NSTextAlignmentCenter;
        }
    }
}
@end

下面是效果图:


水平居中.png 垂直居中.png

献丑了,大牛勿喷

相关文章

网友评论

    本文标题:iOS UIButton的简单封装(对于图片和标题同时存在的情况

    本文链接:https://www.haomeiwen.com/subject/myxtmttx.html