iOS中 按钮和标题完美各种排列/完美教程 韩俊强的博客
每日更新关注:http://weibo.com/hanjunqiang
新浪微博!
前言:最近常常用到按钮和相应标题的组合,当按钮设置图片加标题时,触发范围较小,不易触发,最重要的是还要调试偏移量,相信研究过的开发者都很头疼这一点,那我我就想解决,于是在网上研究了一番,个人总结封装了一个,觉得很棒,推荐给大家!
下面看教程:
整体是对UIButton的自定义封装:
//UIButton+UIButtonSetEdgeInsets.h #import <UIKit/UIKit.h> @interface UIButton (CenterImageAndTitle) //上下居中,图片在上,文字在下 - (void)verticalCenterImageAndTitle:(CGFloat)spacing; - (void)verticalCenterImageAndTitle; //默认6.0 //左右居中,文字在左,图片在右 - (void)horizontalCenterTitleAndImage:(CGFloat)spacing; - (void)horizontalCenterTitleAndImage; //默认6.0 //左右居中,图片在左,文字在右 - (void)horizontalCenterImageAndTitle:(CGFloat)spacing; - (void)horizontalCenterImageAndTitle; //默认6.0 //文字居中,图片在左边 - (void)horizontalCenterTitleAndImageLeft:(CGFloat)spacing; - (void)horizontalCenterTitleAndImageLeft; //默认6.0 //文字居中,图片在右边 - (void)horizontalCenterTitleAndImageRight:(CGFloat)spacing; - (void)horizontalCenterTitleAndImageRight; //默认6.0 @end
每日更新关注:http://weibo.com/hanjunqiang
新浪微博!
//#import "UIButton+CenterImageAndTitle.m"
#import "UIButton+CenterImageAndTitle.h"
@implementation UIButton (CenterImageAndTitle)
- (void)verticalCenterImageAndTitle:(CGFloat)spacing
{
// get the size of the elements here for readability
CGSize imageSize = self.imageView.frame.size;
CGSize titleSize = self.titleLabel.frame.size;
// lower the text and push it left to center it
self.titleEdgeInsets = UIEdgeInsetsMake(0.0, - imageSize.width, - (imageSize.height + spacing/2), 0.0);
// the text width might have changed (in case it was shortened before due to
// lack of space and isn't anymore now), so we get the frame size again
titleSize = self.titleLabel.frame.size;
// raise the image and push it right to center it
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
{
// get the size of the elements here for readability
CGSize imageSize = self.imageView.frame.size;
CGSize titleSize = self.titleLabel.frame.size;
// lower the text and push it left to center it
self.titleEdgeInsets = UIEdgeInsetsMake(0.0, - imageSize.width, 0.0, imageSize.width + spacing/2);
// the text width might have changed (in case it was shortened before due to
// lack of space and isn't anymore now), so we get the frame size again
titleSize = self.titleLabel.frame.size;
// raise the image and push it right to center it
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;
{
// get the size of the elements here for readability
// CGSize imageSize = self.imageView.frame.size;
// CGSize titleSize = self.titleLabel.frame.size;
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
{
// get the size of the elements here for readability
// CGSize imageSize = self.imageView.frame.size;
// CGSize titleSize = self.titleLabel.frame.size;
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
{
// get the size of the elements here for readability
CGSize imageSize = self.imageView.frame.size;
CGSize titleSize = self.titleLabel.frame.size;
// lower the text and push it left to center it
self.titleEdgeInsets = UIEdgeInsetsMake(0.0, - imageSize.width, 0.0, 0.0);
// the text width might have changed (in case it was shortened before due to
// lack of space and isn't anymore now), so we get the frame size again
titleSize = self.titleLabel.frame.size;
// raise the image and push it right to center it
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:
{
//系统默认图片在左,文字在右,间隔为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
QQ群:446310206
iOS中 按钮和标题完美各种排列/完美教程 韩俊强的博客的更多相关文章
- iOS中 扫描二维码/生成二维码详解 韩俊强的博客
最近大家总是问我有没有关于二维码的demo,为了满足大家的需求,特此研究了一番,希望能帮到大家! 每日更新关注:http://weibo.com/hanjunqiang 新浪微博 指示根视图: se ...
- iOS中 扫描二维码/生成二维码具体解释 韩俊强的博客
近期大家总是问我有没有关于二维码的demo,为了满足大家的需求,特此研究了一番,希望能帮到大家! 每日更新关注:http://weibo.com/hanjunqiang 新浪微博 指示根视图: se ...
- iOS中 HTTP/Socket/TCP/IP通信协议具体解释 韩俊强的博客
简介: // OSI(开放式系统互联), 由ISO(国际化标准组织)制定 // 1. 应用层 // 2. 表示层 // 3. 会话层 // 4. 传输层 // 5. 网络层 // 6. 数据链接层 / ...
- iOS中 本地通知/本地通知详解 韩俊强的博客
布局如下:(重点讲本地通知) iOS开发者交流QQ群: 446310206 每日更新关注:http://weibo.com/hanjunqiang 新浪微博 Notification是智能手机应用编 ...
- iOS中 Animation 动画大全 韩俊强的博客
每日更新关注:http://weibo.com/hanjunqiang 新浪微博! iOS开发者交流QQ群: 446310206 1.iOS中我们能看到的控件都是UIView的子类,比如UIButt ...
- iOS中 Realm的学习与使用 韩俊强的博客
每日更新关注:http://weibo.com/hanjunqiang 新浪微博! 有问题或技术交流可以咨询!欢迎加入! 这篇直接搬了一份官方文档过来看的 由于之前没用markdown搞的乱七八糟的 ...
- iOS中 流媒体播放和下载 韩俊强的博客
每日更新关注:http://weibo.com/hanjunqiang 新浪微博 iOS中关于流媒体的简介:介于下载本地播放与实时流媒体之间的一种播放形式,下载本地播放必须全部将文件下载完成后才能播 ...
- iOS中崩溃调试的使用和技巧总结 韩俊强的博客
每日更新关注:http://weibo.com/hanjunqiang 新浪微博 在iOS开发调试过程中以及上线之后,程序经常会出现崩溃的问题.简单的崩溃还好说,复杂的崩溃就需要我们通过解析Cras ...
- iOS中 图文混排/自定义图文混排 作者:韩俊强
指示根视图:(准备几张图片,把label加载在window上) CustomLable *label = [[CustomLable alloc]initWithFrame:CGRectMake(0, ...
随机推荐
- hdu 5112 (2014北京 水)
题意:有个人在跑步,一直每个时间他所在的位置,求最大速度 #include <iostream> #include <cstring> #include <cstdio& ...
- hdu 5429(大数模板)
题意:判断是否是等比数列 a[i] * a[i] = a[i+1] * a[i-1] + 部分为0 的情况 /* 表示还没学java.... ,模板来自NK_test #include &l ...
- Java Servlet 笔记4
Servlet 客户端 HTTP 请求 当浏览器请求网页时,它会向 Web 服务器发送特定信息,这些信息不能被直接读取,因为这些信息是作为 HTTP 请求的头的一部分进行传输的. 读取 HTTP 头的 ...
- 用ECMAScript4 ( ActionScript3) 实现Unity的热更新 -- 使用第三方组件
Unity开发中,常常会用到一些第三方组件.本文以实例介绍如何在热更新脚本中使用这些第三方组件. 首先说明几个基本步骤: 第三方组件通常是以dll或者源码方式提供的,它们本身往往无法热更. 我们在脚本 ...
- STM32 基DMA的DAC波形发生器
DAC是STM32系列的一个基本外设,可以将数字信号转化成模拟信号,这次我将使用DAC来输出一个特定波形. 首先确定工作方法,由于我目前在做的简易示波器在输出波形的同时还需要显示输入信号,所以不能占用 ...
- linux上安装fastdfs+nginx+ngin-module实践并解决多个异常篇
为什么选择Nginx Nginx 是一个很牛的高性能Web和反向代理服务器, 它具有有很多非常优越的特性: 在高连接并发的情况下,Nginx是Apache服务器不错的替代品:Nginx在美国是做虚拟主 ...
- RandomAccessFile读取文本简介
RandomAccessFile类的常用的操作方法 1.public RandomAccessFile(File file, String mode)throws FileNotFoundExcep ...
- JAVA反射之Class类的练习
package zhang; /** * JAVA反射之CLass类的练习 * * 在面向对象的语言里,万事万物皆对象,那么类是谁的对象呢? * 类的类型是CLass * * */ class Tes ...
- __str__与__repr__
在讲解之前,我们先来了解下str和repr的区别:两者都是用来将数字,列表等类型的数据转化为字符串的形式.不同之处在于str更加类似于C语言中使用printf输出的内容,而repr输出的内容会直接将变 ...
- POJ 2135 最小费用最大流
题目链接 Farm Tour Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18961 Accepted: 7326 D ...