一、组件:

1. header file:

//
//  UIButton+CenterAlignment.h
//  QZone
//
//  Created by Jones Duan on 14-7-30.
//  Copyright (c) 2014年 tencent. All rights reserved.
//#import <UIKit/UIKit.h>@interface UIButton (CenterAlignment)/***  水平居中按钮 image 和 title**  @param spacing - image 和 title 的水平间距, 单位point*/
- (void)horizontalCenterImageAndTitleWithSpacing:(float)spacing;/***  水平居中按钮 title 和 image**  @param spacing - image 和 title 的水平间距, 单位point*/
- (void)horizontalCenterTitleAndImageWithSpacing:(float)spacing;/***  垂直居中按钮 image 和 title**  @param spacing - image 和 title 的垂直间距, 单位point*/
- (void)verticalCenterImageAndTitleWithSpacing:(float)spacing;@end

2. source file:

//
//  UIButton+CenterAlignment.m
//  QZone
//
//  Created by Jones Duan on 14-7-30.
//  Copyright (c) 2014年 tencent. All rights reserved.
//#import "UIButton+CenterAlignment.h"@implementation UIButton (CenterAlignment)/*Note:defaults image left and title right, no spacing.imageEdgeInsets (top left bottom right) is the offset relative to titleLabeltitleEdgeInsets (top left bottom right) is the offset relative to imageView忽删!!!*//***  水平居中按钮 image 和 title**  @param spacing - image 和 title 的水平间距, 单位point*/
- (void)horizontalCenterImageAndTitleWithSpacing:(float)spacing
{// left the imageself.imageEdgeInsets = UIEdgeInsetsMake(0.0, -spacing, 0.0, 0.0);// right the textself.titleEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, 0.0, -spacing);
}/***  水平居中按钮 title 和 image**  @param spacing - image 和 title 的水平间距, 单位point*/
- (void)horizontalCenterTitleAndImageWithSpacing:(float)spacing
{CGSize imageSize = self.imageView.frame.size;CGSize titleSize = [self.titleLabel.text boundingRectWithSize:self.bounds.size options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : self.titleLabel.font} context:nil].size;// get the width they will take up as a unitCGFloat totalWidth = (imageSize.width + titleSize.width + spacing);// right the imageself.imageEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, 0.0, -(totalWidth - imageSize.width) * 2);// left the textself.titleEdgeInsets = UIEdgeInsetsMake(0.0, -(totalWidth - titleSize.width) * 2, 0.0, 0.0);
}/***  垂直居中按钮 image 和 title**  @param spacing - image 和 title 的垂直间距, 单位point*/
- (void)verticalCenterImageAndTitleWithSpacing:(float)spacing
{// get the size of the elements here for readabilityCGSize imageSize = self.imageView.frame.size;//    CGSize titleSize = self.titleLabel.frame.size;CGSize titleSize = [self.titleLabel.text boundingRectWithSize:self.bounds.size options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : self.titleLabel.font} context:nil].size;// get the height they will take up as a unitCGFloat totalHeight = (imageSize.height + titleSize.height + spacing);// raise the image and push it right to center itself.imageEdgeInsets = UIEdgeInsetsMake(-(totalHeight - imageSize.height), 0.0, 0.0, -titleSize.width);// lower the text and push it left to center itself.titleEdgeInsets = UIEdgeInsetsMake(0.0, -imageSize.width, -(totalHeight - titleSize.height), 0.0);
}@end

二、应用

1. verticalCenterImageAndTitleWithSpacing 应用

//
//  QZFeedSubscribeButton.h
//  QZone
//
//  Created by Jones Duan on 14-7-29.
//  Copyright (c) 2014年 tencent. All rights reserved.
//#import <UIKit/UIKit.h>
#import "UIButton+CenterAlignment.h"typedef NS_ENUM(NSInteger, QZFeedSubscribeButtonStatus) {kFeedSubscribeButtonStatusSubscribe   = 0,kFeedSubscribeButtonStatusUnsubscribe = 1,
};@interface QZFeedSubscribeButton : UIButton- (void)setSubscribeStatus:(QZFeedSubscribeButtonStatus)status;@end
//
//  QZFeedSubscribeButton.m
//  QZone
//
//  Created by Jones Duan on 14-7-29.
//  Copyright (c) 2014年 tencent. All rights reserved.
//#import "QZFeedSubscribeButton.h"
#import "QzoneThemeManager.h"
#import "UIColor+Theme.h"@interface QZFeedSubscribeButton ()
{UIImage     *_plusInCircleImage;UIImage     *_plusInCircleImageClicked;UIImage     *_minusInCircleImage;UIImage     *_minusInCircleImageClicked;
}@end@implementation QZFeedSubscribeButton- (id)initWithFrame:(CGRect)frame
{self = [super initWithFrame:frame];if (self) {[self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];[self setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];self.titleLabel.font = [UIFont systemFontOfSize:11.0];self.backgroundColor = [UIColor clearColor];// suscribe button internal view - plus image_plusInCircleImage = ImageInTheme(@"community/list_sub_btn.png");_plusInCircleImageClicked = ImageInTheme(@"community/list_sub_btn_click.png");// ussuscribe button internal view - minus image_minusInCircleImage = ImageInTheme(@"community/list_cancel_btn.png");_minusInCircleImageClicked = ImageInTheme(@"community/list_cancel_btn_click.png");// UIImage *btnBgImg = ResizeableImageInTheme(@"feed/feed_btn_middle.png");UIImage *btnBgClickImg = ResizeableImageInTheme(@"feed/feed_btn_middle_click.png");[self setBackgroundImage:nil forState:UIControlStateNormal];[self setBackgroundImage:nil forState:UIControlStateSelected];[self setBackgroundImage:btnBgClickImg forState:UIControlStateHighlighted];}return self;
}- (void)setSubscribeStatus:(QZFeedSubscribeButtonStatus)status
{    switch (status) {case kFeedSubscribeButtonStatusSubscribe:[self setImage:_plusInCircleImage forState:UIControlStateNormal];[self setImage:_plusInCircleImageClicked forState:UIControlStateHighlighted];[self setTitle:@"立即订阅" forState:UIControlStateNormal];[self setTitleColor:DefaultLinkTextColorInTheme forState:UIControlStateNormal];[self setTitleColor:DefaultLinkTextHighlightedColorInTheme forState:UIControlStateHighlighted];break;case kFeedSubscribeButtonStatusUnsubscribe:[self setImage:_minusInCircleImage forState:UIControlStateNormal];[self setImage:_minusInCircleImageClicked forState:UIControlStateHighlighted];[self setTitle:@"取消订阅" forState:UIControlStateNormal];[self setTitleColor:DefaultDescriptionText2ColorInTheme forState:UIControlStateNormal];[self setTitleColor:DefaultButtonSelectedColorInTheme forState:UIControlStateHighlighted];break;default:break;}[self verticalCenterImageAndTitleWithSpacing:7.f];
}@end

2. horizontalCenterTitleAndImageWithSpacing 应用

//
//  QZExpandAndFoldPasterButton.h
//  QZone
//
//  Created by Jones Duan on 14-7-28.
//  Copyright (c) 2014年 tencent. All rights reserved.
//#import <UIKit/UIKit.h>
#import "UIButton+CenterAlignment.h"typedef NS_ENUM(NSInteger, QZExpandAndFoldPasterButtonStatus) {kExpandAndFoldPasterButtonStatusExpand  = 0,kExpandAndFoldPasterButtonStatusFold    = 1,
};@interface QZExpandAndFoldPasterButton : UIButton- (void)setExpandAndFoldStatus:(QZExpandAndFoldPasterButtonStatus)status;@end
//
//  QZExpandAndFoldPasterButton.m
//  QZone
//
//  Created by Jones Duan on 14-7-28.
//  Copyright (c) 2014年 tencent. All rights reserved.
//#import "QZExpandAndFoldPasterButton.h"
#import "UIImage+Theme.h"
#import "QzoneThemeManager.h"@interface QZExpandAndFoldPasterButton ()
{UIImage     *_expandImage;UIImage     *_foldImage;
}@end@implementation QZExpandAndFoldPasterButton- (id)initWithFrame:(CGRect)frame
{self = [super initWithFrame:frame];if (self) {[self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];[self setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];self.titleLabel.font = [UIFont systemFontOfSize:14.5];self.backgroundColor = [UIColor clearColor];_expandImage = ImageInTheme(@"photoEdit/photo_edit_icon_pull_up.png");_foldImage = ImageInTheme(@"photoEdit/photo_edit_icon_pull_down.png");}return self;
}- (void)setExpandAndFoldStatus:(QZExpandAndFoldPasterButtonStatus)status
{switch (status) {case kExpandAndFoldPasterButtonStatusExpand:[self setImage:_foldImage forState:UIControlStateNormal];[self setTitle:@"贴纸" forState:UIControlStateNormal];[self horizontalCenterTitleAndImageWithSpacing:2.f];break;case kExpandAndFoldPasterButtonStatusFold:[self setImage:_expandImage forState:UIControlStateNormal];[self setTitle:@"贴纸" forState:UIControlStateNormal];[self horizontalCenterTitleAndImageWithSpacing:2.f];break;default:break;}
}@end

UIButton水平居中、垂直居中按钮 image 和 title相关推荐

  1. HTML 水平居中 垂直居中 垂直水平居中的几种实现方式

    文章目录 水平居中 垂直居中 垂直水平居中 方式1:绝对定位 方式二 定位+负margin 方式3:使用translate实现平移 方式4:通过设置bottom top left right marg ...

  2. html屏幕垂直居中显示,HTML+CSS,让div在屏幕中居中(水平居中+垂直居中)方法总结...

    水平居中直接加上 标签即可,或者设置margin:auto;当然也可以用下面的方法 下面说两种在屏幕正中(水平居中+垂直居中)的方法 放上示范的html代码: MAIN 方法一: div使用绝对布局, ...

  3. 让div在屏幕中居中(水平居中+垂直居中)的几种方法

    这里是修真院前端小课堂,本篇分析的主题是 [让div在屏幕中居中(水平居中+垂直居中)的几种方法] 水平居中方法: 1.inline,inline-block元素的水平居中,在父级块级元素中设置tex ...

  4. CSS:div内容水平居中/垂直居中设置(非line-height和padding方法)

    问题: 业务需求实现某段内容在指定大小的div内居中(水平居中+垂直居中)显示. 最初的实现方法通过设置line-height与外层相同div height相同实现,但内容过长时导致如下图异常状况. ...

  5. HTML+CSS,让div在屏幕中居中(水平居中+垂直居中)方法总结

    最近写网页经常需要将div在屏幕中居中显示,遂记录下几个常用的方法,都比较简单. 水平居中直接加上<center>标签即可,或者设置margin:auto;当然也可以用下面的方法 下面说两 ...

  6. android 按钮水平居中,Android笔记:Button居中|水平居中|垂直居中(总结)

    -  鉴于各位前辈都有关于居中的示例,今天小弟在这结合自己的理解总结一下. - 居中呢,这里分两种不同布局方式的居中!分别是 LinearLayout 和RelativeLayout. - 首先说的是 ...

  7. 块级元素 div水平居中 垂直居中

    水平居中 margin:0 auto; 解读 上面的代码值设置了两个值,那么第一个值就是元素的上下边距,第二个值就是左右边距.当元素的定义了width属性时,auto能实现水平居中的效果.这是固定写法 ...

  8. 03-CSS操作--CSS操作规范(书写技巧)+通栏布局+标签的注意事项+三大类标签+边距合并问题+图文混排+图像签名+水平居中+垂直居中

    一.CSS操作规范 1.CSS书写技巧 (1)最外层是一个<div id="bigDiv">,将所有元素都包进去 (2)去除<body>的外边距(body{ ...

  9. 仿抖音短视频APP源码html网页图片和文字水平居中垂直居中显示

    div相对于页面水平居中显示: 核心代码:margin:0 auto: /意思为:div的外边距上下为0px,左右居中显示;/ /前提是position为相对定位;不能为absolute绝对定位/ * ...

最新文章

  1. 步步为营 .NET 设计模式学习笔记 六、Adapter(适配器模式)
  2. 平流式初沉池贮砂斗计算_水处理相关计算软件大全,提高工作效率必备
  3. python 地址模糊匹配_使用python处理selenium中的xpath定位元素的模糊匹配问题
  4. SprintBoot开发官方指导文档
  5. Qt / 窗体设置 Qt::WA_TranslucentBackground 为全黑的原因
  6. Duilib教程-自动布局2
  7. java写dnf外掛_dnf卡盟_Java的泛型详解(一)
  8. javascript之生成一个指定范围内的随机数
  9. 中文技术文档写作规范(汇总整理版)
  10. 【安全牛学习笔记】端口扫描
  11. 【操作系统】CPU调度算法
  12. matlab 方位角 经纬度,经纬度转方位角matlab程序
  13. 降噪耳机哪个牌子好?给大家推荐几款质量比较好的降噪耳机品牌
  14. windows专业版以上使用自带远程连接(不限制于局域网)
  15. ws832设置虚拟服务器,华为WS832路由器设置教程 | 192路由网
  16. 特殊字符编码格式数据库不支持
  17. 哈佛参考文献注释及APA文献格式介绍
  18. X猜想:比尔离开后的微软帝国
  19. 【python与数据分析】Matplotlib数据可视化
  20. 开始使用linggle

热门文章

  1. TiDB数据库schema设计之表结构设计
  2. C语言的 a, *a, a
  3. Polyanskiy’s bound
  4. chrome怎么运行Android程序,ARCVM:Chrome OS 中运行 Android 应用程序的新方式
  5. 不应将商业行为政治化!
  6. MyISAM 与 InnoDB 的区别是什么?
  7. python + selenium 实现 华为商城自动抢购
  8. 【日本語勉強】「モデレート」とはどういう意味になるのでしょうか
  9. linux SIGSEGV信号 内存访问错误 Segmentation fault
  10. 关于pip下载很慢很慢