频道栏目
首页 > 程序开发 > 移动开发 > IOS > 正文
ios 开发中UIBottom的image的上下左右方向排列实现方法
2017-11-14 10:32:31      个评论    来源:杨克进的博客  
收藏   我要投稿

ios 开发中UIBottom的image的上下左右方向排列实现方法,效果图:

\

主要代码:继承于UIBottom,主要修改titleEdgeInsets和imageEdgeInsets 属性

CustomizeBtn.h

 

#import 

typedef IBInspectable NS_ENUM(NSUInteger, CustomizeBtnImagePosition) {
    CustomizeBtnImagePositionTop, // image在上,label在下
    CustomizeBtnImagePositionLeft, // image在左,label在右
    CustomizeBtnImagePositionBottom, // image在下,label在上
    CustomizeBtnImagePositionRight // image在右,label在左
};

IB_DESIGNABLE
@interface CustomizeBtn : UIButton
@property(nonatomic,unsafe_unretained)IBInspectable NSUInteger imagePosition;
@property(nonatomic,unsafe_unretained)IBInspectable NSUInteger space;

@end

 

CustomizeBtn.m

 

#import "CustomizeBtn.h"

@implementation CustomizeBtn

- (void)drawRect:(CGRect)rect {
    [self BtnImagePosition:self.imagePosition imageTitleSpace:self.space];
}

-(void)setImagePosition:(NSUInteger)imagePosition {
    _imagePosition = imagePosition;
    [self BtnImagePosition:imagePosition imageTitleSpace:self.space];
}

-(void)setSpace:(NSUInteger)space {
    _space = space;
    [self BtnImagePosition:self.imagePosition imageTitleSpace:space];
}

- (void)BtnImagePosition:(CustomizeBtnImagePosition)style
                        imageTitleSpace:(CGFloat)space {
    
    CGFloat imageWith = self.imageView.frame.size.width;
    CGFloat imageHeight = self.imageView.frame.size.height;
    
    CGFloat labelWidth = 0.0;
    CGFloat labelHeight = 0.0;
    if ([UIDevice currentDevice].systemVersion.floatValue >= 8.0) {
        // 由于iOS8中titleLabel的size为0,用下面的这种设置
        labelWidth = self.titleLabel.intrinsicContentSize.width;
        labelHeight = self.titleLabel.intrinsicContentSize.height;
    } else {
        labelWidth = self.titleLabel.frame.size.width;
        labelHeight = self.titleLabel.frame.size.height;
    }
    
    UIEdgeInsets imageEdgeInsets = UIEdgeInsetsZero;
    UIEdgeInsets labelEdgeInsets = UIEdgeInsetsZero;
    
    switch (style) {
        case CustomizeBtnImagePositionTop:
        {
            imageEdgeInsets = UIEdgeInsetsMake(-labelHeight-space/2.0, 0, 0, -labelWidth);
            labelEdgeInsets = UIEdgeInsetsMake(0, -imageWith, -imageHeight-space/2.0, 0);
        }
            break;
        case CustomizeBtnImagePositionLeft:
        {
            imageEdgeInsets = UIEdgeInsetsMake(0, -space/2.0, 0, space/2.0);
            labelEdgeInsets = UIEdgeInsetsMake(0, space/2.0, 0, -space/2.0);
        }
            break;
        case CustomizeBtnImagePositionBottom:
        {
            imageEdgeInsets = UIEdgeInsetsMake(0, 0, -labelHeight-space/2.0, -labelWidth);
            labelEdgeInsets = UIEdgeInsetsMake(-imageHeight-space/2.0, -imageWith, 0, 0);
        }
            break;
        case CustomizeBtnImagePositionRight:
        {
            imageEdgeInsets = UIEdgeInsetsMake(0, labelWidth+space/2.0, 0, -labelWidth-space/2.0);
            labelEdgeInsets = UIEdgeInsetsMake(0, -imageWith-space/2.0, 0, imageWith+space/2.0);
        }
            break;
        default:
            break;
    }
    
    self.titleEdgeInsets = labelEdgeInsets;
    self.imageEdgeInsets = imageEdgeInsets;
}

@end
点击复制链接 与好友分享!回本站首页
上一篇:ios开发中指纹识别简单介绍
下一篇:ios开发中苹果2D引擎SpriteKit介绍
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站