ios代码调整button图片image文字title位置
来源:互联网 发布:linq过滤重复数据 编辑:程序博客网 时间:2024/05/19 04:05
自定义一个button,要调整 button中的image(注意,不是backgroundImage) 和 title 文字的位置,只需要重写 Button类独对应的两个方法即可:
首先,我们来创建一个 SuperButton继承自 UIButton
#import <UIKit/UIKit.h>@interface SuperButton : UIButton@end
实现文件#import "SuperButton.h"#import "UtilsFunctions.h"@interface SuperButton (){ CGRect boundingRect;}@end
@implementation SuperButton//自定义的初始化方法- (instancetype)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self) { [self setTitle:@"项目介绍" forState:UIControlStateNormal]; [self.titleLabel setFont:[UIFont boldSystemFontOfSize:font]]; [self setBackgroundImage:[UIImage imageNamed:@"cpxq_shang@3x.png"] forState:UIControlStateNormal]; [self setImage:[UIImage imageNamed:@"cpxq_jiantou2@3x.png"] forState:UIControlStateNormal]; boundingRect=[self.titleLabel.text boundingRectWithSize:CGSizeMake(320,font) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:font]} context:nil]; } return self;}
1.重写方法,改变 图片的位置 在 titleRect..方法后执行
- (CGRect)imageRectForContentRect:(CGRect)contentRect{ CGFloat imageX=self.frame.size.width/2+boundingRect.size.width/2; UIScreen *s=[UIScreen mainScreen]; CGRect rect=s.bounds; CGFloat imageY=contentRect.origin.y+14; CGFloat width=24; CGFloat height=24; return CGRectMake(imageX, imageY, width, height);}
2.改变title文字的位置,构造title的矩形即可
- (CGRect)titleRectForContentRect:(CGRect)contentRect{ CGFloat imageX=(self.frame.size.width-boundingRect.size.width)/2; CGFloat imageY=contentRect.origin.y+10; CGFloat width=220; CGFloat height=25; return CGRectMake(imageX, imageY, width, height);}@end
我们只要重写 上述的两个方法,就可以实现对 button按钮中的图片和文字的位置的调整
注意: 1.ios7和ios8系统上 上述两个方法 运行的次数会有差异,可以设置标志位,或者自定义一个 button(不要集成button)
2.代码是经过删减的,大家关键是重写上面的两个方法,重新绘制矩形,即可
1 0
- ios代码调整button图片image文字title位置
- ios代码调整button图片image文字title位置
- ios代码调整button图片image文字title位置
- Button调整按钮中title和image的位置(包含工具类)ios
- [iOS]详解调整UIButton的title和image的位置
- iOS-调整UIButton的title和image的位置
- 调整button上title的位置
- button中image和title的位置
- UIButton 调整title和image的位置
- iOS - 一行代码搞定button的图片与文字的位置
- IOS button按钮中添加image和title并修改其位置
- iOS-随心所欲控制button内image view和title view的位置
- ios中交换button的图片和文字的位置
- ios中交换button的图片和文字的位置
- iOS中button 的图片和文字的位置
- iOS Button 上文字图片位置的设置
- iOS开发-调整按钮的图片文字位置
- iOS -- UIButton上的文字、图片位置的调整
- 浏览器、apache的连接超时详解
- 响应有参方法
- android第四更 (DownManger 系统级下载文件)
- 工厂模式
- Android 开发中的日常积累
- ios代码调整button图片image文字title位置
- jquery自定义类和派生类写法一例
- Oracle当前时间与数据库时间进行计算
- 更改目录,返回指定文件夹(C/C++源程序)
- ios不支持
- 模板模式
- lua 与 php 通过AES数据加密进行通讯
- QCustomPlot之用滚动条控制坐标轴范围
- 水表图片测试的逻辑