美文网首页牛叉的demo上海恩美路演学无止境
自定义按钮文字与图片的位置

自定义按钮文字与图片的位置

作者: 贼海鸥 | 来源:发表于2017-04-14 13:44 被阅读0次

首先声明,我是在git上看到的这个封装方法.我感觉挺好用的.就给大家推荐一下(具体的位置我找不到了,不好意思了).好了,还是直接上代码吧,让我们也来学习一下大神的方法.
这个代码是对UIButton的扩展(category)

首先是.h的代码

#import <UIKit/UIKit.h>

typedef enum : NSUInteger {
    ButtonImgViewStyleTop,
    ButtonImgViewStyleLeft,
    ButtonImgViewStyleBottom,
    ButtonImgViewStyleRight,
} ButtonImgViewStyle;

@interface UIButton (HXButton)
/**
 设置 按钮 图片所在的位置
 
 @param style   图片位置类型(上、左、下、右)
 @param size    图片的大小
 @param space 图片跟文字间的间距
 */
- (void)setImgViewStyle:(ButtonImgViewStyle)style imageSize:(CGSize)size space:(CGFloat)space;
@end

.m的代码

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

static const char Btn_ImgViewStyle_Key;
static const char Btn_ImgSize_key;
static const char Btn_ImgSpace_key;

@implementation UIButton (HXButton)
- (void)setImgViewStyle:(ButtonImgViewStyle)style imageSize:(CGSize)size space:(CGFloat)space
{
    objc_setAssociatedObject(self, &Btn_ImgViewStyle_Key, @(style), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    objc_setAssociatedObject(self, &Btn_ImgSpace_key, @(space), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    objc_setAssociatedObject(self, &Btn_ImgSize_key, NSStringFromCGSize(size), OBJC_ASSOCIATION_COPY_NONATOMIC);
}


+ (void)load
{
    Method m1 = class_getInstanceMethod([self class], @selector(layoutSubviews));
    Method m2 = class_getInstanceMethod([self class], @selector(layoutSubviews1));
    method_exchangeImplementations(m1, m2);
}

- (void)layoutSubviews1
{
    [self layoutSubviews1];
    
    NSNumber *typeNum = objc_getAssociatedObject(self, &Btn_ImgViewStyle_Key);
    if (typeNum) {
        NSNumber *spaceNum = objc_getAssociatedObject(self, &Btn_ImgSpace_key);
        NSString *imgSizeStr = objc_getAssociatedObject(self, &Btn_ImgSize_key);
        CGSize imgSize = self.currentImage ? CGSizeFromString(imgSizeStr) : CGSizeZero;
        CGSize labelSize = self.currentTitle.length ? [self.currentTitle sizeWithAttributes:@{NSFontAttributeName:self.titleLabel.font}] : CGSizeZero;
        
        CGFloat space = (labelSize.width && self.currentImage) ? spaceNum.floatValue : 0;
        
        CGFloat width = self.frame.size.width;
        CGFloat height = self.frame.size.height;
        
        CGFloat imgX, imgY, labelX, labelY;
        
        switch (typeNum.integerValue) {
            case ButtonImgViewStyleLeft:
            {
                imgX = (width - imgSize.width - labelSize.width - space)/2.0;
                imgY = (height - imgSize.height)/2.0;
                labelX = imgX + imgSize.width + space;
                labelY = (height - labelSize.height)/2.0;
                self.imageView.contentMode = UIViewContentModeRight;
                break;
            }
            case ButtonImgViewStyleTop:
            {
                imgX = (width - imgSize.width)/2.0;
                imgY = (height - imgSize.height - space - labelSize.height)/2.0;
                labelX = (width - labelSize.width)/2;
                labelY = imgY + imgSize.height + space;
                self.imageView.contentMode = UIViewContentModeBottom;
                break;
            }
            case ButtonImgViewStyleRight:
            {
                labelX = (width - imgSize.width - labelSize.width - space)/2.0;
                labelY = (height - labelSize.height)/2.0;
                imgX = labelX + labelSize.width + space;
                imgY = (height - imgSize.height)/2.0;
                self.imageView.contentMode = UIViewContentModeLeft;
                break;
            }
            case ButtonImgViewStyleBottom:
            {
                labelX = (width - labelSize.width)/2.0;
                labelY = (height - labelSize.height - imgSize.height -space)/2.0;
                imgX = (width - imgSize.width)/2.0;
                imgY = labelY + labelSize.height + space;
                self.imageView.contentMode = UIViewContentModeTop;
                break;
            }
            default:
                break;
        }
        self.imageView.frame = CGRectMake(imgX, imgY, imgSize.width, imgSize.height);
        self.titleLabel.frame = CGRectMake(labelX, labelY, labelSize.width, labelSize.height);
    }
}
@end

我感觉挺好用的.下面是代码示例

    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 30, 50)];
    [button setTitle:@"小熊" forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:@"小熊.png"] forState:UIControlStateNormal];
    [button setImgViewStyle:ButtonImgViewStyleTop imageSize:CGSizeMake(30, 30) space:5];
    [self.view addSubview:button];

效果图

效果图.png

分层图

分层图

还有一种就是直接修改button.这样做的方法就是创建button的时候,代码会更少
代码如下:

.m

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initialize];
    }
    return self;
}

- (void)initialize {
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    self.imageView.contentMode = UIViewContentModeScaleAspectFit;
    self.titleLabel.font = [UIFont systemFontOfSize:14];
}

-(CGRect)titleRectForContentRect:(CGRect)contentRect{
    CGFloat titleX = -contentRect.size.width * 0.2;
    CGFloat titleY = contentRect.size.height * 0.7;
    CGFloat titleW = contentRect.size.width * 1.4;
    CGFloat titleH = contentRect.size.height * 0.2;
    return CGRectMake(titleX, titleY, titleW, titleH);
}

-(CGRect)imageRectForContentRect:(CGRect)contentRect{
    CGFloat imageW = CGRectGetWidth(contentRect);
    CGFloat imageH = contentRect.size.height * 0.7;
    return CGRectMake(0, 0, imageW, imageH);
}

示例如下

    // 联系卖家
    YLButton *serviceButton = [[YLButton alloc] initWithFrame:CGRectMake(16, 0, 25, self.frame.size.height)];
    [serviceButton setImage:[UIImage imageNamed:@"kefu_"] forState:UIControlStateNormal];
    [serviceButton setTitle:@"联系卖家" forState:UIControlStateNormal];
    [serviceButton setTitleColor:[UIColor colorWithHexString:@"#333333"] forState:UIControlStateNormal];
    serviceButton.titleLabel.font = [UIFont systemFontOfSize:FontSize];
    [serviceButton addTarget:self action:@selector(service:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:serviceButton];

如下图所示

示例图

相关文章

  • UIButton Image和title 的位置关系

    自定义按钮之:文字图片位置随意定制http://www.tuicool.com/articles/YvUJnqR

  • 自定义按钮文字与图片的位置

    首先声明,我是在git上看到的这个封装方法.我感觉挺好用的.就给大家推荐一下(具体的位置我找不到了,不好意思了)....

  • iOS 小常识4--按钮的图与标题位置调整二

    4:按钮的图与标题位置调整二 之前,写过利用按钮的图片,标题边缘设置属性()来控制图片与文字的位置,网上,也有很多...

  • UIButton图片文字位置

    分类:.h中 --space 是图片与文字的间距。.m中 或者自定义,如果达不到需求(图片与文字宽度大于按钮宽度会...

  • 多态

    参考博文: 多态? 多态就是父类指针指向子类对象 例如: 自定义按钮的时候想改变按钮内部的图片和文字的位置,这个时...

  • OC下的自定义Button

    用OC实现自定义按钮的图片和文字布局 实现按钮中的文字和图片自定义布局的方法很简单,只要在按钮添加到父视图之前获取...

  • 自定义按钮一

    一、自定义按钮 可实现的效果如图1.0和1.1所示 1.这是一个可以自定义按钮背景图片位置大小,可以自定义按钮图片...

  • 按钮图片文字位置设置

    btn默认图左字右 注意: 这两个值不同,titleSize只跟btn的Frame相关。textSize跟titl...

  • IOS 按钮 文字图片的位置

    -(void)initButton:(UIButton*)btn{ float spacing =3;//图片...

  • (Swift)自定义UIButton图片与文字位置EdgeIns

    1)触发场景 项目中时常需要自定义UIButton的图片与文字布局位置,重写UIEdgeInsets属性,约束文字...

网友评论

    本文标题:自定义按钮文字与图片的位置

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