iOS-任意改变Button里面图片和文字的位置-Runtime使用

给Button分类添加属性、改变Button内图片和文字的位置

#import <UIKit/UIKit.h>
@interface UIButton (LayoutSubviews)
@property (assign, nonatomic) CGRect imageViewFrame;
@property (assign, nonatomic) CGRect titleLabelFrame;
@end

#import "UIButton+LPLayoutSubviews.h"
#import <objc/objc-runtime.h>
@implementation UIButton (LayoutSubviews)
+ (void)load{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
    [self swizzleMethod:@selector(layoutSubviews) swizzledSelector:@selector(aop_layoutSubviews)];
});
}

#pragma mark- 动态的替换两个方法
+ (void)swizzleMethod:(SEL)originalSelector swizzledSelector:(SEL)swizzledSelector{
Method originalMethod = class_getInstanceMethod([self class], originalSelector);
Method swizzledMethod = class_getInstanceMethod([self class], swizzledSelector);
if (class_addMethod([self class],originalSelector,method_getImplementation(swizzledMethod),method_getTypeEncoding(swizzledMethod))) {
    class_replaceMethod([self class],swizzledSelector,method_getImplementation(originalMethod),method_getTypeEncoding(originalMethod));
} else {
    method_exchangeImplementations(originalMethod, swizzledMethod);
}
}

#pragma mark- 动态的添加属性
- (CGRect)imageViewFrame{
return [objc_getAssociatedObject(self, @selector(imageViewFrame)) CGRectValue];
}
- (void)setImageViewFrame:(CGRect)imageViewFrame{
objc_setAssociatedObject(self, @selector(imageViewFrame), [NSValue valueWithCGRect:imageViewFrame], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (CGRect)titleLabelFrame{
return [objc_getAssociatedObject(self, @selector(titleLabelFrame)) CGRectValue];
}
- (void)setTitleLabelFrame:(CGRect)titleLabelFrame{
objc_setAssociatedObject(self, @selector(titleLabelFrame), [NSValue valueWithCGRect:titleLabelFrame], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

#pragma mark- 替换layoutSubviews的方法
- (void)aop_layoutSubviews{
[self aop_layoutSubviews];
[CATransaction begin];
[CATransaction setDisableActions:YES];

if (!CGRectEqualToRect(self.imageViewFrame, CGRectZero)) {
    if (!CGRectEqualToRect(self.imageView.frame, self.imageViewFrame)) {
        self.imageView.frame = self.imageViewFrame;
    }
}

if (!CGRectEqualToRect(self.titleLabelFrame, CGRectZero)) {
    if (!CGRectEqualToRect(self.titleLabel.frame, self.titleLabelFrame)) {
        self.titleLabel.frame = self.titleLabelFrame;
    }
}

[CATransaction commit];
}
@end

使用:

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setTitle:@"测试" forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:@"userPlaceIcon"] forState:UIControlStateNormal];
[self addSubview:button];

button.imageViewFrame = CGRectMake(0, 0, buttonW, buttonH*0.8);
button.titleLabelFrame = CGRectMake(0, buttonH*0.8, buttonW, buttonH*0.2);

效果展示

WechatIMG20481.jpeg
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,466评论 25 708
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,251评论 4 61
  • 收听提示: 1.点击之后若显示“网页无法打开”,可继续点右上角,选择“在浏览器中打开”,即可。 2.也可下载荔枝F...
    粘粘啊阅读 136评论 7 4
  • 呼呼风声,丝丝凉意,衣不遮,心寒,冬。 裹衣行,家䁔,他乡客,父思,母泪,思。 一日劳,日升起,日落息。 疲惫不堪...
    乀杂人阅读 328评论 0 0
  • 不少男同学在开始健身前,都听说过蛋白粉是很好的健身补剂(按不少网上键盘选手的言论,那什么州长,拉扎尔,都是吃蛋白粉...
    硬派健身阅读 981评论 1 20