每日更新关注:http://weibo.com/hanjunqiang 新浪微博!
前言:最近常常用到按钮和相应标题的组合,当按钮设置图片加标题时,触发范围较小,不易触发,最重要的是还要调试偏移量,相信研究过的开发者都很头疼这一点,那我我就想解决,于是在网上研究了一番,个人总结封装了一个,觉得很棒,推荐给大家!
下面看教程:
整体是对UIButton的自定义封装:
-
-
- #import <UIKit/UIKit.h>
-
- @interface UIButton (CenterImageAndTitle)
-
-
- - (void)verticalCenterImageAndTitle:(CGFloat)spacing;
- - (void)verticalCenterImageAndTitle;
-
-
- - (void)horizontalCenterTitleAndImage:(CGFloat)spacing;
- - (void)horizontalCenterTitleAndImage;
-
-
- - (void)horizontalCenterImageAndTitle:(CGFloat)spacing;
- - (void)horizontalCenterImageAndTitle;
-
-
- - (void)horizontalCenterTitleAndImageLeft:(CGFloat)spacing;
- - (void)horizontalCenterTitleAndImageLeft;
-
-
- - (void)horizontalCenterTitleAndImageRight:(CGFloat)spacing;
- - (void)horizontalCenterTitleAndImageRight;
-
- @end
每日更新关注:http://weibo.com/hanjunqiang 新浪微博!
-
- #import "UIButton+CenterImageAndTitle.h"
-
- @implementation UIButton (CenterImageAndTitle)
-
-
- - (void)verticalCenterImageAndTitle:(CGFloat)spacing
- {
-
- CGSize imageSize = self.imageView.frame.size;
- CGSize titleSize = self.titleLabel.frame.size;
-
-
- self.titleEdgeInsets = UIEdgeInsetsMake(0.0, - imageSize.width, - (imageSize.height + spacing/2), 0.0);
-
-
-
- titleSize = self.titleLabel.frame.size;
-
-
- self.imageEdgeInsets = UIEdgeInsetsMake(- (titleSize.height + spacing/2), 0.0, 0.0, - titleSize.width);
- }
-
- - (void)verticalCenterImageAndTitle
- {
- const int DEFAULT_SPACING = 6.0f;
- [self verticalCenterImageAndTitle:DEFAULT_SPACING];
- }
-
-
- - (void)horizontalCenterTitleAndImage:(CGFloat)spacing
- {
-
- CGSize imageSize = self.imageView.frame.size;
- CGSize titleSize = self.titleLabel.frame.size;
-
-
- self.titleEdgeInsets = UIEdgeInsetsMake(0.0, - imageSize.width, 0.0, imageSize.width + spacing/2);
-
-
-
- titleSize = self.titleLabel.frame.size;
-
-
- self.imageEdgeInsets = UIEdgeInsetsMake(0.0, titleSize.width + spacing/2, 0.0, - titleSize.width);
- }
-
- - (void)horizontalCenterTitleAndImage
- {
- const int DEFAULT_SPACING = 6.0f;
- [self horizontalCenterTitleAndImage:DEFAULT_SPACING];
- }
-
-
- - (void)horizontalCenterImageAndTitle:(CGFloat)spacing;
- {
-
-
-
-
- self.titleEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, 0.0, - spacing/2);
- self.imageEdgeInsets = UIEdgeInsetsMake(0.0, - spacing/2, 0.0, 0.0);
- }
-
- - (void)horizontalCenterImageAndTitle;
- {
- const int DEFAULT_SPACING = 6.0f;
- [self horizontalCenterImageAndTitle:DEFAULT_SPACING];
- }
-
-
- - (void)horizontalCenterTitleAndImageLeft:(CGFloat)spacing
- {
-
-
-
-
- self.imageEdgeInsets = UIEdgeInsetsMake(0.0, - spacing, 0.0, 0.0);
- }
-
- - (void)horizontalCenterTitleAndImageLeft
- {
- const int DEFAULT_SPACING = 6.0f;
- [self horizontalCenterTitleAndImageLeft:DEFAULT_SPACING];
- }
-
-
- - (void)horizontalCenterTitleAndImageRight:(CGFloat)spacing
- {
-
- CGSize imageSize = self.imageView.frame.size;
- CGSize titleSize = self.titleLabel.frame.size;
-
-
- self.titleEdgeInsets = UIEdgeInsetsMake(0.0, - imageSize.width, 0.0, 0.0);
-
-
-
- titleSize = self.titleLabel.frame.size;
-
-
- self.imageEdgeInsets = UIEdgeInsetsMake(0.0, titleSize.width + imageSize.width + spacing, 0.0, - titleSize.width);
- }
-
- - (void)horizontalCenterTitleAndImageRight
- {
- const int DEFAULT_SPACING = 6.0f;
- [self horizontalCenterTitleAndImageRight:DEFAULT_SPACING];
- }
- @end
每日更新关注:http://weibo.com/hanjunqiang 新浪微博!使用方法非常简单:
-
- #import "UIButton+CenterImageAndTitle.h"
- #define kScreenHeight [[UIScreen mainScreen] bounds].size.height //屏幕高度
- #define kScreenWidth [[UIScreen mainScreen] bounds].size.width //屏幕宽度
为了展现所有效果,简单展示一下:
- for (int i = 0; i< 6; i++)
- {
- UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
- button1.frame = CGRectMake(60, 80+i*60, kScreenWidth-60*2, 45);
- button1.tag = i;
- button1.backgroundColor = [UIColor greenColor];
- button1.titleLabel.font = [UIFont systemFontOfSize:15];
- [button1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
- [button1 setImage:[UIImage imageNamed:@"good"] forState:UIControlStateNormal];
- [button1 setTitle:@"小韩哥的博客更新了" forState:UIControlStateNormal];
- [button1 addTarget:self action:@selector(testAction:) forControlEvents:UIControlEventTouchUpInside];
- [self.view addSubview:button1];
-
- switch (i)
- {
- case 0:
- {
-
- }
- break;
-
- case 1:
- {
-
- [button1 verticalCenterImageAndTitle:10.0f];
- }
- break;
-
- case 2:
- {
-
- [button1 horizontalCenterTitleAndImage:50.0f];
- }
- break;
-
- case 3:
- {
-
- [button1 horizontalCenterImageAndTitle:50.0f];
- }
- break;
-
- case 4:
- {
-
- [button1 horizontalCenterTitleAndImageLeft:50.0f];
- }
- break;
-
- case 5:
- {
-
- [button1 horizontalCenterTitleAndImageRight:50.0f];
- }
- break;
-
- default:
- break;
- }
- }
每日更新关注:http://weibo.com/hanjunqiang 新浪微博!最后是点击事件了:
- - (void)testAction:(UIButton *)sender
- {
- NSLog(@"testAction:%ld", (long)sender.tag);
- }
最终效果:
如有问题可通过微博互动联系我哦!
每日更新关注:http://weibo.com/hanjunqiang 新浪微博!
Demo下载地址:https://github.com/XiaoHanGe/UIButtonCenterTitleAndImage