UIButton(干货)--教你如何利用runtime自定义UIButton的titleLabel以及image的frame
来源:互联网 发布:淘宝鹊桥量大吗 编辑:程序博客网 时间:2024/05/21 00:49
本文来自简书,原文地址:http://www.jianshu.com/p/e5fdc00b8e31
最近做项目,会用到许多的类似美团首页button的效果,之前做法都是自定义view,view上添加button以及label来做出相同的效果,但是今天我教你极为简便的方法,只需要自定义button就能实现同一个bt上titleLabel与image互换!
想要的效果:
实际效果:哈哈,需要各位大佬们自己去美化
传送门:https://github.com/JonesCxy/UIButton
不废话,直接上代码:
category.h
#import <UIKit/UIKit.h>@interface UIButton (Layout)@property (nonatomic,assign) CGRect titleRect;@property (nonatomic,assign) CGRect imageRect;@end
category.m
#import "UIButton+Layout.h"#import <objc/runtime.h>@interface UIButton ()@end@implementation UIButton (Layout)#pragma mark - ************* 通过运行时动态添加关联 ******************//定义关联的Keystatic const char * titleRectKey = "yl_titleRectKey";- (CGRect)titleRect { return [objc_getAssociatedObject(self, titleRectKey) CGRectValue];}- (void)setTitleRect:(CGRect)rect { objc_setAssociatedObject(self, titleRectKey, [NSValue valueWithCGRect:rect], OBJC_ASSOCIATION_RETAIN);}//定义关联的Keystatic const char * imageRectKey = "yl_imageRectKey";- (CGRect)imageRect { NSValue * rectValue = objc_getAssociatedObject(self, imageRectKey); return [rectValue CGRectValue];}- (void)setImageRect:(CGRect)rect { objc_setAssociatedObject(self, imageRectKey, [NSValue valueWithCGRect:rect], OBJC_ASSOCIATION_RETAIN);}#pragma mark - ************* 通过运行时动态替换方法 ******************+ (void)load { MethodSwizzle(self,@selector(titleRectForContentRect:),@selector(override_titleRectForContentRect:)); MethodSwizzle(self,@selector(imageRectForContentRect:),@selector(override_imageRectForContentRect:));}void MethodSwizzle(Class c,SEL origSEL,SEL overrideSEL){ Method origMethod = class_getInstanceMethod(c, origSEL); Method overrideMethod= class_getInstanceMethod(c, overrideSEL); //运行时函数class_addMethod 如果发现方法已经存在,会失败返回,也可以用来做检查用: if(class_addMethod(c, origSEL, method_getImplementation(overrideMethod),method_getTypeEncoding(overrideMethod))) { //如果添加成功(在父类中重写的方法),再把目标类中的方法替换为旧有的实现: class_replaceMethod(c,overrideSEL, method_getImplementation(origMethod), method_getTypeEncoding(origMethod)); } else { //addMethod会让目标类的方法指向新的实现,使用replaceMethod再将新的方法指向原先的实现,这样就完成了交换操作。 method_exchangeImplementations(origMethod,overrideMethod); }}- (CGRect)override_titleRectForContentRect:(CGRect)contentRect { if (!CGRectIsEmpty(self.titleRect) && !CGRectEqualToRect(self.titleRect, CGRectZero)) { return self.titleRect; } return [self override_titleRectForContentRect:contentRect];}- (CGRect)override_imageRectForContentRect:(CGRect)contentRect { if (!CGRectIsEmpty(self.imageRect) && !CGRectEqualToRect(self.imageRect, CGRectZero)) { return self.imageRect; } return [self override_imageRectForContentRect:contentRect];}- (void)setTitleRect:(CGRect )titleRect ImageRect:(CGRect )imageRect { self.titleRect = titleRect; self.imageRect = imageRect;}@end
自定义Bt.h
#import <UIKit/UIKit.h>@interface CxyButton : UIButton@property(assign,nonatomic)CGRect titleRect;@property(assign,nonatomic)CGRect imageRect;@end
自定义Bt.m
#import "CxyButton.h"@implementation CxyButton- (instancetype)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self) { } return self;}-(CGRect)titleRectForContentRect:(CGRect)contentRect{ if (!CGRectIsEmpty(self.titleRect) && !CGRectEqualToRect(self.titleRect, CGRectZero)) { return self.titleRect; } return [super titleRectForContentRect:contentRect];}-(CGRect)imageRectForContentRect:(CGRect)contentRect{ if (!CGRectIsEmpty(self.imageRect)&&!CGRectEqualToRect(self.imageRect, CGRectZero)) { return self.imageRect; } return [super imageRectForContentRect:contentRect];}@end
ViewControll.m
- (void)viewDidLoad { [super viewDidLoad]; CxyButton *bt = [CxyButton buttonWithType:(UIButtonTypeCustom)]; bt.imageRect = CGRectMake(0, 0, 20, 20); bt.titleRect = CGRectMake(20, 0, 80, 20); bt.frame = CGRectMake(100, 100, 100, 20); [bt setImage:[UIImage imageNamed:@"heart-1"] forState:(UIControlStateNormal)]; [bt setTitle:@"我就是我" forState:(UIControlStateNormal)]; bt.backgroundColor = [UIColor redColor]; [self.view addSubview:bt]; CxyButton *bt2 = [CxyButton buttonWithType:(UIButtonTypeCustom)]; bt2.imageRect = CGRectMake(0, 0, 30, 30); bt2.titleRect = CGRectMake(0, 20, 30, 80); bt2.frame = CGRectMake(100, 150, 30, 100); [bt2 setImage:[UIImage imageNamed:@"heart-2"] forState:(UIControlStateNormal)]; [bt2 setTitle:@"我" forState:(UIControlStateNormal)]; bt2.backgroundColor = [UIColor greenColor]; [self.view addSubview:bt2];}
简介:利用运行时将bt的titleLabel与image位置动态改变,在给bt赋值时,只需要重写title与image的rect即可.
阅读全文
0 0
- UIButton(干货)--教你如何利用runtime自定义UIButton的titleLabel以及image的frame
- UIButton.titleLabel.text和[UIButton setTitle:],UIButton.imageView.image和[UIButton setImage]的区别
- UIButton如何正确调整imageView及titleLabel的位置
- UIButton如何正确调整imageView及titleLabel的位置
- UIButton如何正确调整imageView及titleLabel的位置
- iOS UIButton如何正确调整imageView及titleLabel的位置
- 自定义UIButton图片和文字的frame
- UIButton的titleLabel修改后没有反应
- UIButton的ImageView与TitleLabel对齐
- iOS-精确控制UIButton 的image view与titlelabel的相对位置
- 自定义UIButton的title和image位置
- UIButton,送给多数开发者的释疑文章,关于如何正确调整imageView及titleLabel的位置
- iOS 中UIButton的 settitle 和 titlelabel的使用情况
- iOS UIButton的titleLabel和imageView的位置调整
- **uibutton的titlelabel和settile的使用误区**
- iOS 中UIButton的 settitle 和 titlelabel的使用误区
- iOS 中UIButton的 settitle 和 titlelabel的使用误区
- 编辑UIButton的titleLabel和imageView的位置
- Java多线程详解生产者和消费者
- 让tomcat支持https
- MTK6735 Android之Camera驱动移植篇
- 在SecureCRT中设置上传下载目录
- linux下挂载ntfs文件系统
- UIButton(干货)--教你如何利用runtime自定义UIButton的titleLabel以及image的frame
- webpack 前端构建性能优化策略小结
- yum配置
- 深入理解程序构造
- EOF的本质
- win7与ubuntu14.04双系统下修改默认启动项
- Java EE开发系列教程
- 分布式存储系统可靠性如何估算?
- BZOJ1797: [Ahoi2009]Mincut 最小割