猫猫学iOS之UIButton一行代码重写图片和标题位置源代码分享

Posted 翟乃玉

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了猫猫学iOS之UIButton一行代码重写图片和标题位置源代码分享相关的知识,希望对你有一定的参考价值。

猫猫分享,必须精品

原创文章,欢迎转载。转载请注明:翟乃玉的博客
地址:http://blog.csdn.net/u013357243

效果图

神马图片在上面title在下面的button之类的需求啊完全不需要考虑,直接来这个就可以了随意调

代码

UIButton+NYImageLocation.h

//
//  UIButton+NYImageLocation.h
//  ChangButton
//
//  Created by ZNYCAT on 16/3/2.
//  Copyright © 2016年 com.znycat.com. All rights reserved.
//根据图片的位置和图片文字的间距来重新设置button的image和title的排列
//如果图片和文字大于button的大小,文字和图片显示的地方就会超出按钮

#import <UIKit/UIKit.h>

typedef NS_ENUM(NSInteger, NYImageLocation) 
    NYImageLocationLeft = 0,              //图片在文字的左边,默认
    NYImageLocationRight,             //图片在文字的右边
    NYImageLocationTop,               //图片在文字的上边
    NYImageLocationBottom,            //图片在文字的下边
;

typedef NS_ENUM(NSInteger, NYOffSetDirection) 
    NYOffSetDirectionLeft = 0,   //图片文字整体向左边偏移,默认
    NYOffSetDirectionRight,      //图片文字整体向右边偏移
    NYOffSetDirectionTop,        //图片文字整体向上边偏移
    NYOffSetDirectionBottom,     //图片文字整体向下边偏移
;

@interface UIButton (NYImageLocation)

/**
 *  根据图片的位置和图片文字的间距来重新设置button的image和title的排列
 *   如果图片和文字大于button的大小,文字和图片显示的地方就会超出按钮
 *
 *  @param location 图片位于文字的哪个方位
 *  @param spacing  图片和文字的间距离
 */
- (void)setImageLocation:(NYImageLocation)location spacing:(CGFloat)spacing;

/**
 *  根据图片的位置和图片文字的间距来重新设置button的image和title的排列,根据offset来确定整体要偏移的方向以及偏移的数值
 *   如果图片和文字大于button的大小,文字和图片显示的地方就会超出按钮
 *
 *  @param postion         图片在文字的哪个方向
 *  @param spacing         图片和文字的间隔
 *  @param offSetDirection 哪个方向偏移
 *  @param offSetVar       偏移多少
 */
- (void)setImageLocation:(NYImageLocation)location spacing:(CGFloat)spacing offSet:(NYOffSetDirection)offSetDirection offSetVar:(CGFloat)offSetVar;
@end

UIButton+NYImageLocation.m

//
//  UIButton+NYImageLocation.m
//  ChangButton
//
//  Created by ZNYCAT on 16/3/2.
//  Copyright © 2016年 com.znycat.com. All rights reserved.
//

#import "UIButton+NYImageLocation.h"

@implementation UIButton (NYImageLocation)
- (void)setImageLocation:(NYImageLocation)location spacing:(CGFloat)spacing 
    CGFloat imageWith = self.imageView.image.size.width;
    CGFloat imageHeight = self.imageView.image.size.height;

    CGFloat titleWidth = [self.titleLabel.text sizeWithFont:self.titleLabel.font].width;
    CGFloat titleHeight = [self.titleLabel.text sizeWithFont:self.titleLabel.font].height;
    //image中心移动的x距离
    CGFloat imageOffsetX = (imageWith + titleWidth) / 2 - imageWith / 2;
    //image中心移动的y距离
    CGFloat imageOffsetY = imageHeight / 2 + spacing / 2;
    //title中心移动的x距离
    CGFloat titleOffsetX = (imageWith + titleWidth / 2) - (imageWith + titleWidth) / 2;
    //title中心移动的y距离
    CGFloat labelOffsetY = titleHeight / 2 + spacing / 2;

    switch (location) 
        case NYImageLocationLeft:
            self.imageEdgeInsets = UIEdgeInsetsMake(0, -spacing/2, 0, spacing/2);
            self.titleEdgeInsets = UIEdgeInsetsMake(0, spacing/2, 0, -spacing/2);
            break;

        case NYImageLocationRight:
            self.imageEdgeInsets = UIEdgeInsetsMake(0, titleWidth + spacing/2, 0, -(titleWidth + spacing/2));
            self.titleEdgeInsets = UIEdgeInsetsMake(0, -(imageHeight + spacing/2), 0, imageHeight + spacing/2);
            break;

        case NYImageLocationTop:

            self.imageEdgeInsets = UIEdgeInsetsMake(-imageOffsetY, imageOffsetX, imageOffsetY, -imageOffsetX);
            self.titleEdgeInsets = UIEdgeInsetsMake(labelOffsetY, -titleOffsetX, -labelOffsetY, titleOffsetX);
            break;

        case NYImageLocationBottom:

            self.imageEdgeInsets = UIEdgeInsetsMake(imageOffsetY, imageOffsetX, -imageOffsetY, -imageOffsetX);
            self.titleEdgeInsets = UIEdgeInsetsMake(-labelOffsetY, -titleOffsetX, labelOffsetY, titleOffsetX);

            break;

        default:
            break;
    



- (void)setImageLocation:(NYImageLocation)location spacing:(CGFloat)spacing offSet:(NYOffSetDirection)offSetDirection offSetVar:(CGFloat)offSetVar
    CGFloat imageWith = self.imageView.image.size.width;
    CGFloat imageHeight = self.imageView.image.size.height;
    //title的宽度
    CGFloat titleWidth = [self.titleLabel.text sizeWithFont:self.titleLabel.font].width;
    //title的高度
    CGFloat titleHeight = [self.titleLabel.text sizeWithFont:self.titleLabel.font].height;

    //image中心移动的x距离
    CGFloat imageOffsetX = (imageWith + titleWidth) / 2 - imageWith / 2;
    //image中心移动的y距离
    CGFloat imageOffsetY = imageHeight / 2 + spacing / 2;
    //title中心移动的x距离
    CGFloat titleOffsetX = (imageWith + titleWidth / 2) - (imageWith + titleWidth) / 2;
    //title中心移动的y距离
    CGFloat labelOffsetY = titleHeight / 2 + spacing / 2;

    switch (location) 
        case NYImageLocationLeft:
            self.imageEdgeInsets = UIEdgeInsetsMake(0, -spacing/2, 0, spacing/2);
            self.titleEdgeInsets = UIEdgeInsetsMake(0, spacing/2, 0, -spacing/2);
            break;

        case NYImageLocationRight:
            self.imageEdgeInsets = UIEdgeInsetsMake(0, titleWidth + spacing/2, 0, -(titleWidth + spacing/2));
            self.titleEdgeInsets = UIEdgeInsetsMake(0, -(imageHeight + spacing/2), 0, imageHeight + spacing/2);
            break;

        case NYImageLocationTop:

            self.imageEdgeInsets = UIEdgeInsetsMake(-imageOffsetY, imageOffsetX, imageOffsetY, -imageOffsetX);
            self.titleEdgeInsets = UIEdgeInsetsMake(labelOffsetY, -titleOffsetX, -labelOffsetY, titleOffsetX);
            break;

        case NYImageLocationBottom:

            self.imageEdgeInsets = UIEdgeInsetsMake(imageOffsetY, imageOffsetX, -imageOffsetY, -imageOffsetX);
            self.titleEdgeInsets = UIEdgeInsetsMake(-labelOffsetY, -titleOffsetX, labelOffsetY, titleOffsetX);

            break;

        default:
            break;
    

    CGFloat imageTop = self.imageEdgeInsets.top;
    CGFloat imageLeft = self.imageEdgeInsets.left;
    CGFloat imageBottom = self.imageEdgeInsets.bottom;
    CGFloat imageRight = self.imageEdgeInsets.right;

    CGFloat titleTop = self.titleEdgeInsets.top;
    CGFloat titleLeft = self.titleEdgeInsets.left;
    CGFloat titleBottom = self.titleEdgeInsets.bottom;
    CGFloat titleRight = self.titleEdgeInsets.right;
    switch (offSetDirection)
        case NYOffSetDirectionLeft:
            self.imageEdgeInsets = UIEdgeInsetsMake(imageTop, imageLeft - offSetVar, imageBottom, imageRight + offSetVar);
            self.titleEdgeInsets = UIEdgeInsetsMake(titleTop, titleLeft - offSetVar, titleBottom, titleRight + offSetVar);

            break;
        case NYOffSetDirectionRight:
            self.imageEdgeInsets = UIEdgeInsetsMake(imageTop, imageLeft + offSetVar, imageBottom, imageRight - offSetVar);
            self.titleEdgeInsets = UIEdgeInsetsMake(titleTop, titleLeft + offSetVar, titleBottom, titleRight - offSetVar);
            break;
        case NYOffSetDirectionTop:
            self.imageEdgeInsets = UIEdgeInsetsMake(imageTop - offSetVar , imageLeft, imageBottom + offSetVar, imageRight);
            self.titleEdgeInsets = UIEdgeInsetsMake(titleTop - offSetVar , titleLeft, titleBottom + offSetVar, titleRight);
            break;
        case NYOffSetDirectionBottom:
            self.imageEdgeInsets = UIEdgeInsetsMake(imageTop + offSetVar, imageLeft, imageBottom - offSetVar, imageRight);
            self.titleEdgeInsets = UIEdgeInsetsMake(titleTop + offSetVar, titleLeft, titleBottom - offSetVar, titleRight);
            break;
        default:
            break;
    



@end

以上是关于猫猫学iOS之UIButton一行代码重写图片和标题位置源代码分享的主要内容,如果未能解决你的问题,请参考以下文章

猫猫学iOS之UIButton一行代码重写图片和标题位置

猫猫学iOS之UITextField右边设置图片,以及UITextField全解

猫猫学iOS(五十五)多线程网络之图片下载框架之SDWebImage

猫猫学iOS之UITextField右边设置图片,以及UITextField全解

猫猫学iOS之小知识之xcode6自己主动提示图片插件 KSImageNamed的安装

猫猫学iOS之UILabel设置圆角不成功所做调控更改