UIButton文字靠左、上、下的多种实现方式

2018-04-24  本文已影响451人  SmallWhiteMouse
写在前面:

本文主要是针对iOS系统控件UIButton进行操作,介绍多种改变图片、文本的位置关系的方式方法。提供多种做法,是希望在遇到新课题时可以为各位朋友激发一丝丝灵感。不评优劣,方式自取。

效果图:


四种位置关系.png

1.继承自UIButton,重写- layoutSubveiws

////示例:实现图片上文字下.(效果图右上)
- (void)layoutSubviews{
    [super layoutSubviews];
    CGFloat  buttonW = self.bounds.size.width;
    CGFloat  buttonH = self.bounds.size.height;
    
    CGFloat  imageH =buttonW - 10;
    self.imageView.frame = CGRectMake(0, 0, buttonW, imageH);
    self.titleLabel.frame = CGRectMake(0, imageH, buttonW, buttonH - imageH);
}

2.继承自UIButton,重写两个方法titleRectForContentRectimageRectForContentRect

//示例:实现图片上文字下.(效果图右上)
- (CGRect)titleRectForContentRect:(CGRect)contentRect{
    
    CGFloat titleY = contentRect.size.height * 0.6;
    CGFloat titleW = contentRect.size.width;
    CGFloat titleH = contentRect.size.height * 0.4;
    return CGRectMake(0, titleY, titleW, titleH);
}

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

3、实例化UIButton并加到父控件之后修改imageView和titleLabel的内边距

//示例:实现文字左图片右.(效果图左下)
- (void)xbs_updateImageAlignmentToRightWithSpace:(CGFloat)space{

    CGFloat halfSpace = space / 2.0f;
    CGFloat imageWidth = self.currentImage.size.width;
    [self setTitleEdgeInsets:UIEdgeInsetsMake(0, -imageWidth-halfSpace, 0, imageWidth+halfSpace)];
    
    CGFloat edgeWidth = [self.currentTitle sizeWithAttributes:@{NSFontAttributeName:self.titleLabel.font}].width;
    [self setImageEdgeInsets:UIEdgeInsetsMake(0, edgeWidth+halfSpace, 0, -edgeWidth-halfSpace)];
}

注意:

确保你在使用UIButton+XBSImageAlignment这个分类之前的文本和图片是可以正常显示的。

附录


#import <UIKit/UIKit.h>

@interface UIButton (XBSImageAlignment)

//to Right
- (void)xbs_updateImageAlignmentToRight;

//to UP
- (void)xbs_updateImageAlignmentToUp;

//to Down
- (void)xbs_updateImageAlignmentToDown;


//space : space  between  textLabel and  imageView
- (void)xbs_updateImageAlignmentToRightWithSpace:(CGFloat)space;
- (void)xbs_updateImageAlignmentToUpWithSpace:(CGFloat)space;
- (void)xbs_updateImageAlignmentToDownWithSpace:(CGFloat)space;


@end

#import "UIButton+XBSImageAlignment.h"

@implementation UIButton (XBSImageAlignment)

- (void)xbs_updateImageAlignmentToRight{
    [self xbs_updateImageAlignmentToRightWithSpace:0];
}

- (void)xbs_updateImageAlignmentToRightWithSpace:(CGFloat)space{

    CGFloat halfSpace = space / 2.0f;
    CGFloat imageWidth = self.currentImage.size.width;
    [self setTitleEdgeInsets:UIEdgeInsetsMake(0, -imageWidth-halfSpace, 0, imageWidth+halfSpace)];
    
    CGFloat edgeWidth = [self.currentTitle sizeWithAttributes:@{NSFontAttributeName:self.titleLabel.font}].width;
    [self setImageEdgeInsets:UIEdgeInsetsMake(0, edgeWidth+halfSpace, 0, -edgeWidth-halfSpace)];
}

- (void)xbs_updateImageAlignmentToUp{
    [self xbs_updateImageAlignmentToUpWithSpace:0];
}

- (void)xbs_updateImageAlignmentToUpWithSpace:(CGFloat)space {
    
    CGFloat halfSpace = space / 2.0f;
    CGFloat imageWidth = self.currentImage.size.width;
    CGFloat imageHeight = self.currentImage.size.height;
    [self setTitleEdgeInsets:UIEdgeInsetsMake(imageHeight/2 + halfSpace, -imageWidth/2, -imageHeight/2 - halfSpace, imageWidth/2)];
    
    CGFloat edgeWidth = [self.currentTitle sizeWithAttributes:@{NSFontAttributeName:self.titleLabel.font}].width;
    CGFloat edgeHeight = [self.currentTitle sizeWithAttributes:@{NSFontAttributeName:self.titleLabel.font}].height;
    [self setImageEdgeInsets:UIEdgeInsetsMake(-edgeHeight/2 - halfSpace, edgeWidth / 2, edgeHeight/2 + halfSpace, -edgeWidth/2)];
}

- (void)xbs_updateImageAlignmentToDown{
    [self xbs_updateImageAlignmentToDownWithSpace:0];
}

- (void)xbs_updateImageAlignmentToDownWithSpace:(CGFloat)space {
    CGFloat halfSpace = space / 2.0f;
    CGFloat imageWidth = self.currentImage.size.width;
    CGFloat imageHeight = self.currentImage.size.height;
    [self setTitleEdgeInsets:UIEdgeInsetsMake(-imageHeight/2 - halfSpace, -imageWidth/2, imageHeight/2 + halfSpace, imageWidth/2)];
    
    CGFloat edgeWidth = [self.currentTitle sizeWithAttributes:@{NSFontAttributeName:self.titleLabel.font}].width;
    CGFloat edgeHeight = [self.currentTitle sizeWithAttributes:@{NSFontAttributeName:self.titleLabel.font}].height;
    [self setImageEdgeInsets:UIEdgeInsetsMake(edgeHeight/2 + halfSpace, edgeWidth / 2, -edgeHeight/2 - halfSpace, -edgeWidth/2)];
}
@end

如果您有什么疑问或者发现书写歧义,非常感激您能留言~

上一篇 下一篇

猜你喜欢

热点阅读