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

本文介绍了一个UIButton的分类扩展方法,用于调整按钮内的图片和文字布局,包括左右、上下等多种排列方式,并实现了倒计时功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ViewController中使用实例

   UIView*headerView = [[UIViewalloc]initWithFrame:CGRectMake(0,0,CKScreenW,50)];
    headerView.backgroundColor=HexColor(0xceeffe);
    [self.viewaddSubview:headerView];
   
   
   UIButton*btn = [[UIButtonalloc]initWithFrame:CGRectMake(10,10,CKScreenW*2/3,30)];
    btn.backgroundColor= [UIColorwhiteColor];
    [btnsetTitle:@"按检查批次查询"forState:0];
    [btnTiaoZhengButtonWithOffsit:100TextImageSite:UIButtonTextLeft];//UIButton + Helper
    btn.titleLabel.font= [UIFontsystemFontOfSize:12];
    [btnsetTitleColor:[UIColorgrayColor]forState:0];
    [headerViewaddSubview:btn];


(1)UIButton+Helper.h

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


#import<UIKit/UIKit.h>
#define MsgCodeTime 60
typedefNS_OPTIONS(NSUInteger, UIButtonTextSite) {
    UIButtonTextRight=0,//图片在左文字在右
    UIButtonTextLeft ,//图片在右文字在左
    UIButtonTextBottom,//图片在上文字在下
    UIButtonTextTop,//图片在下文字在上
};
@interfaceUIButton(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"



@implementationUIButton(Helper)
/**
 * 按钮上的图片跟文字,居中对齐
 */
-(void)TiaoZhengButtonWithOffsit:(CGFloat)offset TextImageSite:(UIButtonTextSite)site{
   
   
   if(site==UIButtonTextLeft) {
        [selfTextImageLeftWithOffsit:offset];
    }elseif (site==UIButtonTextTop) {
        [selfTextImageTopWithOffsit:offset];
    }else if(site==UIButtonTextBottom) {
        [selfTextImageBottomWithOffsit:offset];
    }else{
       
        [selfTextImageRightWithOffsit:offset];
    }
}
/**
 * 文字在右边,图片在左边,居中对齐
 *
 *  @param offset文字跟图片间的间距大小
 */
-(void)TextImageRightWithOffsit:(CGFloat)offset{
   
   CGFloatoffsetBetweenImageAndText = offset;
   CGFloatinsetAmount = 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{
   CGFloatimgW=self.imageView.frame.size.width;
   CGFloattitleW=self.titleLabel.frame.size.width;
   CGFloatoffsetBetweenImageAndText = offset;
   CGFloatinsetAmount = 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{
   CGPointbuttonBoundsCenter = CGPointMake(CGRectGetMidX(self.bounds),CGRectGetMidY(self.bounds));
   //找出imageView最终的center
   CGPointendImageViewCenter = CGPointMake(buttonBoundsCenter.x,CGRectGetMidY(self.imageView.bounds));
   //找出titleLabel最终的center
   CGPointendTitleLabelCenter = CGPointMake(buttonBoundsCenter.x,CGRectGetHeight(self.bounds)-CGRectGetMidY(self.titleLabel.bounds));
   //取得imageView最初的center
   CGPointstartImageViewCenter = self.imageView.center;
   //取得titleLabel最初的center
   CGPointstartTitleLabelCenter = self.titleLabel.center;
   //设置imageEdgeInsets
   CGFloatimgH=self.imageView.frame.size.height/2;
   CGFloattitleH=self.titleLabel.frame.size.height/2;
   CGFloatoffsetBetweenImageAndText = offset;
   CGFloatinsetAmount = offsetBetweenImageAndText / 2.0;
   
   CGFloatimageEdgeInsetsTop = -titleH-insetAmount;
   CGFloatimageEdgeInsetsLeft = endImageViewCenter.x- startImageViewCenter.x;
   CGFloatimageEdgeInsetsBottom = -imageEdgeInsetsTop;
   CGFloatimageEdgeInsetsRight = -imageEdgeInsetsLeft;
   
   self.imageEdgeInsets=UIEdgeInsetsMake(imageEdgeInsetsTop, imageEdgeInsetsLeft, imageEdgeInsetsBottom, imageEdgeInsetsRight);
   //设置titleEdgeInsets
   CGFloattitleEdgeInsetsTop = imgH+insetAmount;
   CGFloattitleEdgeInsetsLeft = endTitleLabelCenter.x- startTitleLabelCenter.x;
   CGFloattitleEdgeInsetsBottom = -titleEdgeInsetsTop;
   CGFloattitleEdgeInsetsRight = -titleEdgeInsetsLeft;
   self.titleEdgeInsets=UIEdgeInsetsMake(titleEdgeInsetsTop, titleEdgeInsetsLeft, titleEdgeInsetsBottom, titleEdgeInsetsRight);
}
/**
 * 文字在上边,图片在下边,居中对齐
 *
 *  @param offset文字跟图片间的间距大小
 */
-(void)TextImageTopWithOffsit:(CGFloat)offset{
   CGPointbuttonBoundsCenter = CGPointMake(CGRectGetMidX(self.bounds),CGRectGetMidY(self.bounds));
   //找出imageView最终的center
   CGPointendImageViewCenter = CGPointMake(buttonBoundsCenter.x,CGRectGetMidY(self.imageView.bounds));
   //找出titleLabel最终的center
   CGPointendTitleLabelCenter = CGPointMake(buttonBoundsCenter.x,CGRectGetHeight(self.bounds)-CGRectGetMidY(self.titleLabel.bounds));
   //取得imageView最初的center
   CGPointstartImageViewCenter = self.imageView.center;
   //取得titleLabel最初的center
   CGPointstartTitleLabelCenter = self.titleLabel.center;
   //设置imageEdgeInsets
   CGFloatimgH=self.imageView.frame.size.height/2;
   CGFloattitleH=self.titleLabel.frame.size.height/2;
   CGFloatoffsetBetweenImageAndText = offset;
   CGFloatinsetAmount = offsetBetweenImageAndText / 2.0;
   
   CGFloatimageEdgeInsetsTop = titleH+insetAmount;
   CGFloatimageEdgeInsetsLeft = endImageViewCenter.x- startImageViewCenter.x;
   CGFloatimageEdgeInsetsBottom = -imageEdgeInsetsTop;
   CGFloatimageEdgeInsetsRight = -imageEdgeInsetsLeft;
   
   self.imageEdgeInsets=UIEdgeInsetsMake(imageEdgeInsetsTop, imageEdgeInsetsLeft, imageEdgeInsetsBottom, imageEdgeInsetsRight);
   //设置titleEdgeInsets
   CGFloattitleEdgeInsetsTop = -imgH-insetAmount;
   CGFloattitleEdgeInsetsLeft = endTitleLabelCenter.x- startTitleLabelCenter.x;
   CGFloattitleEdgeInsetsBottom = -titleEdgeInsetsTop;
   CGFloattitleEdgeInsetsRight = -titleEdgeInsetsLeft;
   self.titleEdgeInsets=UIEdgeInsetsMake(titleEdgeInsetsTop, titleEdgeInsetsLeft, titleEdgeInsetsBottom, titleEdgeInsetsRight);
}
#pragma mark -获取验证码倒计时

NSTimer*_timer;
NSIntegertimeout1;
-(void)StartMsgCodeTimeDown
{
//    NSTimer *_timer;
//    NSInteger timeout;
   timeout1= MsgCodeTime;
    [selfsetTitle:[NSStringstringWithFormat:@"%@秒后重发",@(timeout1)]forState:UIControlStateDisabled];
     self.enabled=NO;
   _timer= [NSTimerscheduledTimerWithTimeInterval:1target:selfselector:@selector(timeji)userInfo:nilrepeats:YES];
    [[NSRunLoopcurrentRunLoop]addTimer:_timerforMode:UITrackingRunLoopMode];
}
-(void)timeji
{
//    NSTimer *_timer;
//    NSInteger timeout;
    timeout1--;
   if(timeout1<=0) {
        [_timerinvalidate];
       _timer= nil;
       self.enabled=YES;
    }else{
        [selfsetTitle:[NSStringstringWithFormat:@"%@秒后重发",@(timeout1)]forState:UIControlStateDisabled];
       self.enabled=NO;
    }
}
-(void)dealloc{
   if(_timer) {
        [_timerinvalidate];
       _timer= nil;
    }
}
@end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值