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

給Button分類添加屬性、改變Button內(nèi)圖片和文字的位置

#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- 動態(tài)的替換兩個方法
+ (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- 動態(tài)的添加屬性
- (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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,287評論 25 708
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 12,241評論 4 61
  • 收聽提示: 1.點擊之后若顯示“網(wǎng)頁無法打開”,可繼續(xù)點右上角,選擇“在瀏覽器中打開”,即可。 2.也可下載荔枝F...
    粘粘啊閱讀 136評論 7 4
  • 呼呼風(fēng)聲,絲絲涼意,衣不遮,心寒,冬。 裹衣行,家?,他鄉(xiāng)客,父思,母淚,思。 一日勞,日升起,日落息。 疲憊不堪...
    乀雜人閱讀 328評論 0 0
  • 不少男同學(xué)在開始健身前,都聽說過蛋白粉是很好的健身補(bǔ)劑(按不少網(wǎng)上鍵盤選手的言論,那什么州長,拉扎爾,都是吃蛋白粉...
    硬派健身閱讀 976評論 1 20