Button调整按钮中title和image的位置(包含工具类)ios

ViewController中使用实例

    UIView *headerView = [[ UIView alloc ] initWithFrame : CGRectMake ( 0 , 0 , CKScreenW , 50 )];
    headerView. backgroundColor = HexColor ( 0xceeffe );
    [ self . view addSubview :headerView];
   
   
    UIButton *btn = [[ UIButton alloc ] initWithFrame : CGRectMake ( 10 , 10 , CKScreenW * 2 / 3 , 30 )];
    btn. backgroundColor = [ UIColor whiteColor ];
    [btn setTitle : @" 按检查批次查询 " forState : 0 ];
    [btn TiaoZhengButtonWithOffsit : 100 TextImageSite : UIButtonTextLeft ]; //UIButton + Helper
    btn. titleLabel . font = [ UIFont systemFontOfSize : 12 ];
    [btn setTitleColor :[ UIColor grayColor ] forState : 0 ];
    [headerView addSubview :btn];


(1) UIButton+Helper.h

//
//  UIButton+Helper.h
//  zack
//
//  Created by apple on 17/7/2.


#import <UIKit/UIKit.h>
#define MsgCodeTime  60
typedef NS_OPTIONS (NSUInteger, UIButtonTextSite) {
    UIButtonTextRight= 0 , // 图片在左文字在右
    UIButtonTextLeft , // 图片在右文字在左
    UIButtonTextBottom, // 图片在上文字在下
    UIButtonTextTop, // 图片在下文字在上
};
@interface UIButton(Helper)
/**
按钮上的图片跟文字位置
*
*  @param offset 间距
*  @param site   位置
*/
-( void )TiaoZhengButtonWithOffsit:( CGFloat )offset TextImageSite:( UIButtonTextSite )site;
/**
 *  开始倒计时
 */
-( void )StartMsgCodeTimeDown;
@end



(2) UIButton+Helper.m
//
//  UIButton+Helper.m
//  zack
//
//  Created by apple on 17/7/2.


#import "UIButton+Helper.h"



@implementation UIButton(Helper)
/**
 *  按钮上的图片跟文字,居中对齐
 */
-( void )TiaoZhengButtonWithOffsit:( CGFloat )offset TextImageSite:( UIButtonTextSite )site{
   
   
    if (site== UIButtonTextLeft ) {
        [ self TextImageLeftWithOffsit :offset];
    } else if (site== UIButtonTextTop ) {
        [ self TextImageTopWithOffsit :offset];
    } else   if (site== UIButtonTextBottom ) {
        [ self TextImageBottomWithOffsit :offset];
    } else {
       
        [ self TextImageRightWithOffsit :offset];
    }
}
/**
 *  文字在右边,图片在左边,居中对齐
 *
 *  @param offset 文字跟图片间的间距大小
 */
-( void )TextImageRightWithOffsit:( CGFloat )offset{
   
    CGFloat offsetBetweenImageAndText = offset;
    CGFloat insetAmount = offsetBetweenImageAndText / 2.0 ;
    self . imageEdgeInsets = UIEdgeInsetsMake ( 0 ,-insetAmount, 0 ,insetAmount);
    self . titleEdgeInsets = UIEdgeInsetsMake ( 0 ,insetAmount, 0 , -insetAmount);
    self . contentEdgeInsets = UIEdgeInsetsMake ( 0 , insetAmount, 0 , insetAmount);
}
/**
 *  文字在左边,图片在右边,居中对齐
 *
 *  @param offset 文字跟图片间的间距大小
 */
-( void )TextImageLeftWithOffsit:( CGFloat )offset{
    CGFloat imgW= self . imageView . frame . size . width ;
    CGFloat titleW= self . titleLabel . frame . size . width ;
    CGFloat offsetBetweenImageAndText = offset;
    CGFloat insetAmount = offsetBetweenImageAndText / 2.0 ;
    self . imageEdgeInsets = UIEdgeInsetsMake ( 0 ,titleW+insetAmount, 0 ,-titleW-insetAmount);
    self . titleEdgeInsets = UIEdgeInsetsMake ( 0 ,-imgW-insetAmount, 0 , imgW+insetAmount);
    self . contentEdgeInsets = UIEdgeInsetsMake ( 0 , insetAmount, 0 , insetAmount);
}
/**
 *  文字在下边,图片在上边,居中对齐
 *
 *  @param offset 文字跟图片间的间距大小
 */
-( void )TextImageBottomWithOffsit:( CGFloat )offset{
    CGPoint buttonBoundsCenter = CGPointMake ( CGRectGetMidX ( self . bounds ), CGRectGetMidY ( self . bounds ));
    // 找出 imageView 最终的 center
    CGPoint endImageViewCenter = CGPointMake (buttonBoundsCenter. x , CGRectGetMidY ( self . imageView . bounds ));
    // 找出 titleLabel 最终的 center
    CGPoint endTitleLabelCenter = CGPointMake (buttonBoundsCenter. x , CGRectGetHeight ( self . bounds )- CGRectGetMidY ( self . titleLabel . bounds ));
    // 取得 imageView 最初的 center
    CGPoint startImageViewCenter = self . imageView . center ;
    // 取得 titleLabel 最初的 center
    CGPoint startTitleLabelCenter = self . titleLabel . center ;
    // 设置 imageEdgeInsets
    CGFloat imgH= self . imageView . frame . size . height / 2 ;
    CGFloat titleH= self . titleLabel . frame . size . height / 2 ;
    CGFloat offsetBetweenImageAndText = offset;
    CGFloat insetAmount = offsetBetweenImageAndText / 2.0 ;
   
    CGFloat imageEdgeInsetsTop = -titleH-insetAmount;
    CGFloat imageEdgeInsetsLeft = endImageViewCenter. x - startImageViewCenter. x ;
    CGFloat imageEdgeInsetsBottom = -imageEdgeInsetsTop;
    CGFloat imageEdgeInsetsRight = -imageEdgeInsetsLeft;
   
    self . imageEdgeInsets = UIEdgeInsetsMake (imageEdgeInsetsTop, imageEdgeInsetsLeft, imageEdgeInsetsBottom, imageEdgeInsetsRight);
    // 设置 titleEdgeInsets
    CGFloat titleEdgeInsetsTop = imgH+insetAmount;
    CGFloat titleEdgeInsetsLeft = endTitleLabelCenter. x - startTitleLabelCenter. x ;
    CGFloat titleEdgeInsetsBottom = -titleEdgeInsetsTop;
    CGFloat titleEdgeInsetsRight = -titleEdgeInsetsLeft;
    self . titleEdgeInsets = UIEdgeInsetsMake (titleEdgeInsetsTop, titleEdgeInsetsLeft, titleEdgeInsetsBottom, titleEdgeInsetsRight);
}
/**
 *  文字在上边,图片在下边,居中对齐
 *
 *  @param offset 文字跟图片间的间距大小
 */
-( void )TextImageTopWithOffsit:( CGFloat )offset{
    CGPoint buttonBoundsCenter = CGPointMake ( CGRectGetMidX ( self . bounds ), CGRectGetMidY ( self . bounds ));
    // 找出 imageView 最终的 center
    CGPoint endImageViewCenter = CGPointMake (buttonBoundsCenter. x , CGRectGetMidY ( self . imageView . bounds ));
    // 找出 titleLabel 最终的 center
    CGPoint endTitleLabelCenter = CGPointMake (buttonBoundsCenter. x , CGRectGetHeight ( self . bounds )- CGRectGetMidY ( self . titleLabel . bounds ));
    // 取得 imageView 最初的 center
    CGPoint startImageViewCenter = self . imageView . center ;
    // 取得 titleLabel 最初的 center
    CGPoint startTitleLabelCenter = self . titleLabel . center ;
    // 设置 imageEdgeInsets
    CGFloat imgH= self . imageView . frame . size . height / 2 ;
    CGFloat titleH= self . titleLabel . frame . size . height / 2 ;
    CGFloat offsetBetweenImageAndText = offset;
    CGFloat insetAmount = offsetBetweenImageAndText / 2.0 ;
   
    CGFloat imageEdgeInsetsTop = titleH+insetAmount;
    CGFloat imageEdgeInsetsLeft = endImageViewCenter. x - startImageViewCenter. x ;
    CGFloat imageEdgeInsetsBottom = -imageEdgeInsetsTop;
    CGFloat imageEdgeInsetsRight = -imageEdgeInsetsLeft;
   
    self . imageEdgeInsets = UIEdgeInsetsMake (imageEdgeInsetsTop, imageEdgeInsetsLeft, imageEdgeInsetsBottom, imageEdgeInsetsRight);
    // 设置 titleEdgeInsets
    CGFloat titleEdgeInsetsTop = -imgH-insetAmount;
    CGFloat titleEdgeInsetsLeft = endTitleLabelCenter. x - startTitleLabelCenter. x ;
    CGFloat titleEdgeInsetsBottom = -titleEdgeInsetsTop;
    CGFloat titleEdgeInsetsRight = -titleEdgeInsetsLeft;
    self . titleEdgeInsets = UIEdgeInsetsMake (titleEdgeInsetsTop, titleEdgeInsetsLeft, titleEdgeInsetsBottom, titleEdgeInsetsRight);
}
#pragma mark - 获取验证码倒计时

NSTimer *_timer;
NSInteger timeout1;
-( void )StartMsgCodeTimeDown
{
//    NSTimer *_timer;
//    NSInteger timeout;
    timeout1 = MsgCodeTime ;
    [ self setTitle :[ NSString stringWithFormat : @"%@ 秒后重发 " , @( timeout1 ) ] forState : UIControlStateDisabled ];
      self . enabled = NO ;
    _timer = [ NSTimer scheduledTimerWithTimeInterval : 1 target : self selector : @selector (timeji) userInfo : nil repeats : YES ];
    [[ NSRunLoop currentRunLoop ] addTimer : _timer forMode : UITrackingRunLoopMode ];
}
-( void )timeji
{
//    NSTimer *_timer;
//    NSInteger timeout;
     timeout1 --;
    if ( timeout1 <= 0 ) {
        [ _timer invalidate ];
        _timer = nil ;
        self . enabled = YES ;
    } else {
        [ self setTitle :[ NSString stringWithFormat : @"%@ 秒后重发 " , @( timeout1 ) ] forState : UIControlStateDisabled ];
        self . enabled = NO ;
    }
}
-( void )dealloc{
    if ( _timer ) {
        [ _timer invalidate ];
        _timer = nil ;
    }
}
@end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值