[iOS]UIButton同时添加image和title后子视图位置管理
来源:互联网 发布:mac dps文件怎么打开 编辑:程序博客网 时间:2024/05/29 00:34
[iOS]UIButton同时添加image和title后子视图位置管理
本文产生原因:
自定义TableBar时,一般需要给Button设置Image和Title,以便改变点击效果及调用响应方法。
- (void)viewDidLoad { [super viewDidLoad]; /** UIEdgeInsets insets = {top, left, bottom, right}; top 距可变区域顶部的距离 left 距可变区域左侧的距离 bottom 距可变区域底部的距离 right 距可变区域右侧的距离 */ // 赋值 [_funBut setImage:[UIImage imageNamed:@"email.png"] forState:UIControlStateNormal]; [_funBut setTitle:@"首页非常长的一串字符串这里测试就随便添加了一段描述了不要介意" forState:UIControlStateNormal]; [_funBut.titleLabel setNumberOfLines:0]; [_funBut setTitleColor:[UIColor colorWithRed:116/255.0 green:116/255.0 blue:116/255.0 alpha:1] forState:UIControlStateHighlighted]; // 1.默认位置 [_funBut setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)]; [_funBut setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)]; // 2.居中位置 [_funBut setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, -_funBut.titleLabel.intrinsicContentSize.width)]; [_funBut setTitleEdgeInsets:UIEdgeInsetsMake(0, -_funBut.currentImage.size.width, 0, 0)]; // 3.需求位置 [_funBut setImageEdgeInsets:UIEdgeInsetsMake(-_funBut.titleLabel.intrinsicContentSize.height, 0, 0, -_funBut.titleLabel.intrinsicContentSize.width)]; [_funBut setTitleEdgeInsets:UIEdgeInsetsMake(0, -_funBut.currentImage.size.width, -_funBut.currentImage.size.height, 0)];}上面使用的切片大小是200x200,效果参照图1、图2、图3、图4。
下面再提供一个方法在不是真的情况下改变image的size,效果如图5。
- (void)viewDidLoad { [super viewDidLoad]; // 赋值 UIImage *image = [UIImage imageNamed:@"email.png"]; [_funBut setImage:[self originImage:image scaleToSize:CGSizeMake(30, 30)] forState:UIControlStateNormal]; [_funBut setTitle:@"一串字符串" forState:UIControlStateNormal]; [_funBut.titleLabel setNumberOfLines:0]; [_funBut setTitleColor:[UIColor colorWithRed:116/255.0 green:116/255.0 blue:116/255.0 alpha:1] forState:UIControlStateHighlighted]; // 1.默认位置 [_funBut setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)]; [_funBut setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)]; // 2.居中位置 [_funBut setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, -_funBut.titleLabel.intrinsicContentSize.width)]; [_funBut setTitleEdgeInsets:UIEdgeInsetsMake(0, -_funBut.currentImage.size.width, 0, 0)]; // 3.需求位置 [_funBut setImageEdgeInsets:UIEdgeInsetsMake(-_funBut.titleLabel.intrinsicContentSize.height, 0, 0, -_funBut.titleLabel.intrinsicContentSize.width)]; [_funBut setTitleEdgeInsets:UIEdgeInsetsMake(0, -_funBut.currentImage.size.width, -_funBut.currentImage.size.height, 0)];}/** * 压缩图片 * image:将要压缩的图片 * size: 压缩后的尺寸 */- (UIImage*)originImage:(UIImage *)image scaleToSize:(CGSize)size { // 参数一:区域大小,参数二:是否是非透明,参数三:屏幕密度; UIGraphicsBeginImageContextWithOptions(size, NO, [UIScreen mainScreen].scale); [image drawInRect:CGRectMake(0, 0, size.width, size.height)]; UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return scaledImage;}
示意图:
灰色区域为button的frame
1.默认位置
2.居中位置
3.需求位置
4.点击效果
5.按比例改变image大小后的点击效果:
0 0
- [iOS]UIButton同时添加image和title后子视图位置管理
- iOS UIButton同时设置title和image属性
- iOS UIButton同时设置title和image属性
- [iOS]详解调整UIButton的title和image的位置
- iOS 自定义UIButton的title和image位置
- iOS自定义UIButton的Image和Title位置
- iOS-调整UIButton的title和image的位置
- UIButton 的 image 和title 位置设置
- 自定义UIButton的title和image位置
- UIButton 调整title和image的位置
- 给UIButton添加title和image
- UIButton上image和title的位置调整
- UIButton通过UIEdgeInsetsMake设置image和title的位置
- 详解调整UIButton的title和image的位置
- UIButton将title和image放置在任意位置上
- UIBUtton title image 不能同时显示
- 设置UIButton的image title 位置
- IOS button按钮中添加image和title并修改其位置
- 根据字符串中出现的字符逐个打印各字符出现次数
- UTF—8与UTF—8(无bom)格式相比有什么不同
- 洛谷 P1781 宇宙总统
- selenium使用Xpath定位的四种方法
- PHP请求远程地址设置超时时间
- [iOS]UIButton同时添加image和title后子视图位置管理
- 听说2017你想写前端?
- nginx 配置多个tomcat共用80端口
- k8s容器调度策略
- 堆排序
- 探究为何:在onCreate中通过View.post能获取宽高
- android网络判断
- git ssh传输的原理与实践
- 设计模式深入学习--Proxy 代理模式(结构型模式)