iOS UIButton文字和图片上下左右偏移封装,一个方法即可实现button上文字和图片不同位置的放置
来源:互联网 发布:java 获取前一天日期 编辑:程序博客网 时间:2024/05/22 12:19
开发中,几乎都会需要时按钮上的文字在图片的上面,下面,左面,右面,然后就再次封装! 慢慢的,就自己试着写了一个封装,只需要调用一个方法就能实现文字和图片的不同位置展示!下面是代码:
.h文件
//
// ZFSButton.h
// ZFSNetWorkRequest
//
// Created by HandsomeC on 16/12/12.
// Copyright © 2016年赵发生. All rights reserved.
//
#import <UIKit/UIKit.h>
/********
该自定的button是控制图片与文字未知的,有图片在上,文字在下,图片与文字左右适配等封装
*********/
typedef NS_ENUM(NSInteger, ZFSImageLocation) {
ZFSImageLocationLeft = 0, //图片在文字的左边,默认
ZFSImageLocationRight, //图片在文字的右边
ZFSImageLocationTop, //图片在文字的上边
ZFSImageLocationBottom, //图片在文字的下边
};
typedef NS_ENUM(NSInteger, ZFSOffSetDirection) {
ZFSOffSetDirectionLeft = 0, //图片文字整体向左边偏移,默认
ZFSOffSetDirectionRight, //图片文字整体向右边偏移
ZFSOffSetDirectionTop, //图片文字整体向上边偏移
ZFSOffSetDirectionBottom, //图片文字整体向下边偏移
};
@interface ZFSButton : UIButton
/**
* 根据图片的位置和图片文字的间距来重新设置button的image和title的排列
* 如果图片和文字大于button的大小,文字和图片显示的地方就会超出按钮
*
* @param location图片位于文字的哪个方位
* @param spacing 图片和文字的间距离
*/
- (void)setImageLocation:(ZFSImageLocation)location spacing:(CGFloat)spacing;
/**
* 根据图片的位置和图片文字的间距来重新设置button的image和title的排列,根据offset来确定整体要偏移的方向以及偏移的数值
* 如果图片和文字大于button的大小,文字和图片显示的地方就会超出按钮
*
* @param location 图片在文字的哪个方向
* @param spacing 图片和文字的间隔
* @param offSetDirection哪个方向偏移
* @param offSetVar 偏移多少
*/
- (void)setImageLocation:(ZFSImageLocation)location spacing:(CGFloat)spacing offSet:(ZFSOffSetDirection)offSetDirection offSetVar:(CGFloat)offSetVar;
@end
.m文件
//
// ZFSButton.m
// ZFSNetWorkRequest
//
// Created by HandsomeC on 16/12/12.
// Copyright © 2016年赵发生. All rights reserved.
//
#import "ZFSButton.h"
@implementation ZFSButton
- (void)setImageLocation:(ZFSImageLocation)location spacing:(CGFloat)spacing {
CGFloat imageWith =self.imageView.image.size.width;
CGFloat imageHeight =self.imageView.image.size.height;
CGSize texth_wXQ =CGSizeMake(MAXFLOAT,MAXFLOAT);
NSDictionary *dicText =@{NSFontAttributeName :self.titleLabel.font};
CGFloat titleWidth = [self.titleLabel.textboundingRectWithSize:texth_wXQoptions:NSStringDrawingUsesLineFragmentOriginattributes:dicText context:nil].size.width;
CGFloat titleHeight = [self.titleLabel.textboundingRectWithSize:texth_wXQoptions:NSStringDrawingUsesLineFragmentOriginattributes:dicText context:nil].size.height;
//image中心移动的x距离
CGFloat imageOffsetX = (imageWith + titleWidth) /2 - imageWith / 2;
//image中心移动的y距离
CGFloat imageOffsetY = imageHeight /2 + spacing / 2;
//title中心移动的x距离
CGFloat titleOffsetX = (imageWith + titleWidth /2) - (imageWith + titleWidth) /2;
//title中心移动的y距离
CGFloat labelOffsetY = titleHeight /2 + spacing / 2;
switch (location) {
caseZFSImageLocationLeft:
self.imageEdgeInsets =UIEdgeInsetsMake(0, -spacing/2,0, spacing/2);
self.titleEdgeInsets =UIEdgeInsetsMake(0, spacing/2,0, -spacing/2);
break;
caseZFSImageLocationRight:
self.imageEdgeInsets =UIEdgeInsetsMake(0, titleWidth + spacing/2,0, -(titleWidth + spacing/2));
self.titleEdgeInsets =UIEdgeInsetsMake(0, -(imageHeight + spacing/2),0, imageHeight + spacing/2);
break;
caseZFSImageLocationTop:
self.imageEdgeInsets =UIEdgeInsetsMake(-imageOffsetY, imageOffsetX, imageOffsetY, -imageOffsetX);
self.titleEdgeInsets =UIEdgeInsetsMake(labelOffsetY, -titleOffsetX, -labelOffsetY, titleOffsetX);
break;
caseZFSImageLocationBottom:
self.imageEdgeInsets =UIEdgeInsetsMake(imageOffsetY, imageOffsetX, -imageOffsetY, -imageOffsetX);
self.titleEdgeInsets =UIEdgeInsetsMake(-labelOffsetY, -titleOffsetX, labelOffsetY, titleOffsetX);
break;
default:
break;
}
}
- (void)setImageLocation:(ZFSImageLocation)location spacing:(CGFloat)spacing offSet:(ZFSOffSetDirection)offSetDirection offSetVar:(CGFloat)offSetVar{
CGFloat imageWith =self.imageView.image.size.width;
CGFloat imageHeight =self.imageView.image.size.height;
CGSize texth_wXQ =CGSizeMake(MAXFLOAT,MAXFLOAT);
NSDictionary *dicText =@{NSFontAttributeName :self.titleLabel.font};
CGFloat titleWidth = [self.titleLabel.textboundingRectWithSize:texth_wXQoptions:NSStringDrawingUsesLineFragmentOriginattributes:dicText context:nil].size.width;
CGFloat titleHeight = [self.titleLabel.textboundingRectWithSize:texth_wXQoptions:NSStringDrawingUsesLineFragmentOriginattributes:dicText context:nil].size.height;
//image中心移动的x距离
CGFloat imageOffsetX = (imageWith + titleWidth) /2 - imageWith / 2;
//image中心移动的y距离
CGFloat imageOffsetY = imageHeight /2 + spacing / 2;
//title中心移动的x距离
CGFloat titleOffsetX = (imageWith + titleWidth /2) - (imageWith + titleWidth) /2;
//title中心移动的y距离
CGFloat labelOffsetY = titleHeight /2 + spacing / 2;
switch (location) {
caseZFSImageLocationLeft:
self.imageEdgeInsets =UIEdgeInsetsMake(0, -spacing/2,0, spacing/2);
self.titleEdgeInsets =UIEdgeInsetsMake(0, spacing/2,0, -spacing/2);
break;
caseZFSImageLocationRight:
self.imageEdgeInsets =UIEdgeInsetsMake(0, titleWidth + spacing/2,0, -(titleWidth + spacing/2));
self.titleEdgeInsets =UIEdgeInsetsMake(0, -(imageHeight + spacing/2),0, imageHeight + spacing/2);
break;
caseZFSImageLocationTop:
self.imageEdgeInsets =UIEdgeInsetsMake(-imageOffsetY, imageOffsetX, imageOffsetY, -imageOffsetX);
self.titleEdgeInsets =UIEdgeInsetsMake(labelOffsetY, -titleOffsetX, -labelOffsetY, titleOffsetX);
break;
caseZFSImageLocationBottom:
self.imageEdgeInsets =UIEdgeInsetsMake(imageOffsetY, imageOffsetX, -imageOffsetY, -imageOffsetX);
self.titleEdgeInsets =UIEdgeInsetsMake(-labelOffsetY, -titleOffsetX, labelOffsetY, titleOffsetX);
break;
default:
break;
}
CGFloat imageTop =self.imageEdgeInsets.top;
CGFloat imageLeft =self.imageEdgeInsets.left;
CGFloat imageBottom =self.imageEdgeInsets.bottom;
CGFloat imageRight =self.imageEdgeInsets.right;
CGFloat titleTop =self.titleEdgeInsets.top;
CGFloat titleLeft =self.titleEdgeInsets.left;
CGFloat titleBottom =self.titleEdgeInsets.bottom;
CGFloat titleRight =self.titleEdgeInsets.right;
switch (offSetDirection){
caseZFSOffSetDirectionLeft:
self.imageEdgeInsets =UIEdgeInsetsMake(imageTop, imageLeft - offSetVar, imageBottom, imageRight + offSetVar);
self.titleEdgeInsets =UIEdgeInsetsMake(titleTop, titleLeft - offSetVar, titleBottom, titleRight + offSetVar);
break;
caseZFSOffSetDirectionRight:
self.imageEdgeInsets =UIEdgeInsetsMake(imageTop, imageLeft + offSetVar, imageBottom, imageRight - offSetVar);
self.titleEdgeInsets =UIEdgeInsetsMake(titleTop, titleLeft + offSetVar, titleBottom, titleRight - offSetVar);
break;
caseZFSOffSetDirectionTop:
self.imageEdgeInsets =UIEdgeInsetsMake(imageTop - offSetVar , imageLeft, imageBottom + offSetVar, imageRight);
self.titleEdgeInsets =UIEdgeInsetsMake(titleTop - offSetVar , titleLeft, titleBottom + offSetVar, titleRight);
break;
caseZFSOffSetDirectionBottom:
self.imageEdgeInsets =UIEdgeInsetsMake(imageTop + offSetVar, imageLeft, imageBottom - offSetVar, imageRight);
self.titleEdgeInsets =UIEdgeInsetsMake(titleTop + offSetVar, titleLeft, titleBottom - offSetVar, titleRight);
break;
default:
break;
}
}
@end
- iOS UIButton文字和图片上下左右偏移封装,一个方法即可实现button上文字和图片不同位置的放置
- iOS - UIButton(UIEdgeInsets) 设置button上的文字和图片
- iOS - UIButton(UIEdgeInsets)/设置button上的文字和图片 在定义位置
- 28.iOS - UIButton(UIEdgeInsets)/设置button上的文字和图片 在定义位置
- iOS - UIButton(UIEdgeInsets)/设置button上的文字和图片 在定义位置
- ios UIButton的图片和文字位置
- button上的图片和文字偏移量排版
- UIBUtton图片和文字的位置关系封装
- UIButton的UIEdgeInsets功能:设置button上的文字和图片的位置
- UIButton上的图片和文字位置调整
- UIButton上的图片和文字位置调整
- UIButton上的图片和文字位置调整
- 自定义UIButton上的文字和图片位置
- uibutton 改变文字和图片的位置
- iOS - UIButton(UIEdgeInsets)/设置button上的文字和图片上下垂直居中对齐
- iOS - UIButton(UIEdgeInsets)/设置button上的文字和图片上下垂直居中对齐
- iOS - UIButton(UIEdgeInsets)/设置button上的文字和图片上下垂直居中对齐
- iOS - UIButton(UIEdgeInsets)/设置button上的文字和图片上下垂直居中对齐
- Scrapy抓取W3C
- Service全方位了解,总有你不知道的一面
- Excel图纵坐标单位修改
- item点击失效
- Oracle SQL函数listagg实现多行字符串连接
- iOS UIButton文字和图片上下左右偏移封装,一个方法即可实现button上文字和图片不同位置的放置
- ASP.NET Core MVC 模型绑定用法及原理
- Mac os系统android studio无法识别部分安卓手机,无法进行调试
- 互联网架构(8):Socket网络通信编程--Netty
- 面试:----Maven项目同时使用lib下的Jar包
- 通过js来获取前多少天的或者后多少天的日期
- 10个值得深思的_PHP_面试问题
- kotlin初窥之Kotlin Android Extensions
- ASP.NET Core 中间件之压缩、缓存