猫猫学iOS之UIButton一行代码重写图片和标题位置源代码分享
来源:互联网 发布:linux vim 命令大全 编辑:程序博客网 时间:2024/04/30 20:22
猫猫分享,必须精品
原创文章,欢迎转载。转载请注明:翟乃玉的博客
地址:http://blog.csdn.net/u013357243
效果图
神马图片在上面title在下面的button之类的需求啊完全不需要考虑,直接来这个就可以了随意调
代码
UIButton+NYImageLocation.h
//// UIButton+NYImageLocation.h// ChangButton//// Created by ZNYCAT on 16/3/2.// Copyright © 2016年 com.znycat.com. All rights reserved.//根据图片的位置和图片文字的间距来重新设置button的image和title的排列//如果图片和文字大于button的大小,文字和图片显示的地方就会超出按钮#import <UIKit/UIKit.h>typedef NS_ENUM(NSInteger, NYImageLocation) { NYImageLocationLeft = 0, //图片在文字的左边,默认 NYImageLocationRight, //图片在文字的右边 NYImageLocationTop, //图片在文字的上边 NYImageLocationBottom, //图片在文字的下边};typedef NS_ENUM(NSInteger, NYOffSetDirection) { NYOffSetDirectionLeft = 0, //图片文字整体向左边偏移,默认 NYOffSetDirectionRight, //图片文字整体向右边偏移 NYOffSetDirectionTop, //图片文字整体向上边偏移 NYOffSetDirectionBottom, //图片文字整体向下边偏移};@interface UIButton (NYImageLocation)/** * 根据图片的位置和图片文字的间距来重新设置button的image和title的排列 * 如果图片和文字大于button的大小,文字和图片显示的地方就会超出按钮 * * @param location 图片位于文字的哪个方位 * @param spacing 图片和文字的间距离 */- (void)setImageLocation:(NYImageLocation)location spacing:(CGFloat)spacing;/** * 根据图片的位置和图片文字的间距来重新设置button的image和title的排列,根据offset来确定整体要偏移的方向以及偏移的数值 * 如果图片和文字大于button的大小,文字和图片显示的地方就会超出按钮 * * @param postion 图片在文字的哪个方向 * @param spacing 图片和文字的间隔 * @param offSetDirection 哪个方向偏移 * @param offSetVar 偏移多少 */- (void)setImageLocation:(NYImageLocation)location spacing:(CGFloat)spacing offSet:(NYOffSetDirection)offSetDirection offSetVar:(CGFloat)offSetVar;@end
UIButton+NYImageLocation.m
//// UIButton+NYImageLocation.m// ChangButton//// Created by ZNYCAT on 16/3/2.// Copyright © 2016年 com.znycat.com. All rights reserved.//#import "UIButton+NYImageLocation.h"@implementation UIButton (NYImageLocation)- (void)setImageLocation:(NYImageLocation)location spacing:(CGFloat)spacing { CGFloat imageWith = self.imageView.image.size.width; CGFloat imageHeight = self.imageView.image.size.height; CGFloat titleWidth = [self.titleLabel.text sizeWithFont:self.titleLabel.font].width; CGFloat titleHeight = [self.titleLabel.text sizeWithFont:self.titleLabel.font].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) { case NYImageLocationLeft: self.imageEdgeInsets = UIEdgeInsetsMake(0, -spacing/2, 0, spacing/2); self.titleEdgeInsets = UIEdgeInsetsMake(0, spacing/2, 0, -spacing/2); break; case NYImageLocationRight: self.imageEdgeInsets = UIEdgeInsetsMake(0, titleWidth + spacing/2, 0, -(titleWidth + spacing/2)); self.titleEdgeInsets = UIEdgeInsetsMake(0, -(imageHeight + spacing/2), 0, imageHeight + spacing/2); break; case NYImageLocationTop: self.imageEdgeInsets = UIEdgeInsetsMake(-imageOffsetY, imageOffsetX, imageOffsetY, -imageOffsetX); self.titleEdgeInsets = UIEdgeInsetsMake(labelOffsetY, -titleOffsetX, -labelOffsetY, titleOffsetX); break; case NYImageLocationBottom: self.imageEdgeInsets = UIEdgeInsetsMake(imageOffsetY, imageOffsetX, -imageOffsetY, -imageOffsetX); self.titleEdgeInsets = UIEdgeInsetsMake(-labelOffsetY, -titleOffsetX, labelOffsetY, titleOffsetX); break; default: break; }}- (void)setImageLocation:(NYImageLocation)location spacing:(CGFloat)spacing offSet:(NYOffSetDirection)offSetDirection offSetVar:(CGFloat)offSetVar{ CGFloat imageWith = self.imageView.image.size.width; CGFloat imageHeight = self.imageView.image.size.height; //title的宽度 CGFloat titleWidth = [self.titleLabel.text sizeWithFont:self.titleLabel.font].width; //title的高度 CGFloat titleHeight = [self.titleLabel.text sizeWithFont:self.titleLabel.font].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) { case NYImageLocationLeft: self.imageEdgeInsets = UIEdgeInsetsMake(0, -spacing/2, 0, spacing/2); self.titleEdgeInsets = UIEdgeInsetsMake(0, spacing/2, 0, -spacing/2); break; case NYImageLocationRight: self.imageEdgeInsets = UIEdgeInsetsMake(0, titleWidth + spacing/2, 0, -(titleWidth + spacing/2)); self.titleEdgeInsets = UIEdgeInsetsMake(0, -(imageHeight + spacing/2), 0, imageHeight + spacing/2); break; case NYImageLocationTop: self.imageEdgeInsets = UIEdgeInsetsMake(-imageOffsetY, imageOffsetX, imageOffsetY, -imageOffsetX); self.titleEdgeInsets = UIEdgeInsetsMake(labelOffsetY, -titleOffsetX, -labelOffsetY, titleOffsetX); break; case NYImageLocationBottom: 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){ case NYOffSetDirectionLeft: self.imageEdgeInsets = UIEdgeInsetsMake(imageTop, imageLeft - offSetVar, imageBottom, imageRight + offSetVar); self.titleEdgeInsets = UIEdgeInsetsMake(titleTop, titleLeft - offSetVar, titleBottom, titleRight + offSetVar); break; case NYOffSetDirectionRight: self.imageEdgeInsets = UIEdgeInsetsMake(imageTop, imageLeft + offSetVar, imageBottom, imageRight - offSetVar); self.titleEdgeInsets = UIEdgeInsetsMake(titleTop, titleLeft + offSetVar, titleBottom, titleRight - offSetVar); break; case NYOffSetDirectionTop: self.imageEdgeInsets = UIEdgeInsetsMake(imageTop - offSetVar , imageLeft, imageBottom + offSetVar, imageRight); self.titleEdgeInsets = UIEdgeInsetsMake(titleTop - offSetVar , titleLeft, titleBottom + offSetVar, titleRight); break; case NYOffSetDirectionBottom: self.imageEdgeInsets = UIEdgeInsetsMake(imageTop + offSetVar, imageLeft, imageBottom - offSetVar, imageRight); self.titleEdgeInsets = UIEdgeInsetsMake(titleTop + offSetVar, titleLeft, titleBottom - offSetVar, titleRight); break; default: break; }}@end
4 0
- 猫猫学iOS之UIButton一行代码重写图片和标题位置源代码分享
- 猫猫学iOS之UIButton一行代码重写图片和标题位置
- ios UIButton的图片和文字位置
- UIButton中显示的图片和标题位置的设置
- UIButton中显示的图片和标题位置的设置
- UIButton使用UIEdgeInsetsMake 图片 标题 位置 排版
- UIButton使用UIEdgeInsetsMake 图片 标题 位置 排版
- iOS经典讲解之UIButton改变图片和文字的位置
- iOS UIButton上同时加图片和标题,设置偏移
- iOS UIButton 自定义图片和文字位置详解
- 22.UIButton的标题和图片位置的调整/Image和BackgroundImage
- iOS UI_APPEARANCE_SELECTOR、appearence 和UIButton加图片并调整图片和字体的位置
- UIButton中设置图片与标题位置的解决方案
- IOS 设置按钮的的图片和标题位置
- iOS _ UIButton 标题字体大小颜色位置等
- uibutton 改变文字和图片的位置
- iOS UIButton的重写
- iOS 重写UIButton
- Socket.io的实时竞拍系统实现
- Java开发Web Service的简介
- php 学习笔记 —— 变量 数据类型 数组 (二)
- 设计模式之命令模式
- QT + Win7 SDK 中文时 乱码 常量中有换行符 问题
- 猫猫学iOS之UIButton一行代码重写图片和标题位置源代码分享
- Android Studio之Eclipse工程代码搬家
- 华硕 RT-AC54U路由器固件功能说明
- Git分支本地操作详解
- ios中TableView的长按触发事件
- 今日BBC
- JSP会话跟踪学习记录
- WINDOWS快捷方式(*.lnk文件)打开方式关联错误的解决方法
- 详解Oracle的几种分页查询语句