Files
peko-ios/YuMi/CustomUI/MSRTL/UIButton+MSRTL.m
2024-04-11 17:05:27 +08:00

53 lines
1.7 KiB
Objective-C

//
// MSRTL.m
// YuMi
//
// Created by duoban on 2024/4/11.
//
#import "UIButton+MSRTL.h"
@implementation UIButton (MSRTL)
UIEdgeInsets RSRTLEdgeInsetsWithInsets(UIEdgeInsets insets) {
if (insets.left != insets.right && isMSRTL()) {
CGFloat temp = insets.left;
insets.left = insets.right;
insets.right = temp;
}
return insets;
}
+ (void)load{
if (isMSRTL()) {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Method oldMethod = class_getInstanceMethod(self, @selector(setContentEdgeInsets:));
Method newMethod = class_getInstanceMethod(self, @selector(msrtl_setContentEdgeInsets:));
method_exchangeImplementations(oldMethod, newMethod);
Method oldImageMethod = class_getInstanceMethod(self, @selector(setImageEdgeInsets:));
Method newImageMethod = class_getInstanceMethod(self, @selector(msrtl_setImageEdgeInsets:));
method_exchangeImplementations(oldImageMethod,newImageMethod);
Method oldTitleMethod = class_getInstanceMethod(self, @selector(setTitleEdgeInsets:));
Method newTitleMethod = class_getInstanceMethod(self, @selector(msrtl_setTitleEdgeInsets:));
method_exchangeImplementations(oldTitleMethod,newTitleMethod);
});
}
}
- (void)msrtl_setContentEdgeInsets:(UIEdgeInsets)contentEdgeInsets {
[self msrtl_setContentEdgeInsets:RSRTLEdgeInsetsWithInsets(contentEdgeInsets)];
}
- (void)msrtl_setImageEdgeInsets:(UIEdgeInsets)imageEdgeInsets {
[self msrtl_setImageEdgeInsets:RSRTLEdgeInsetsWithInsets(imageEdgeInsets)];
}
- (void)msrtl_setTitleEdgeInsets:(UIEdgeInsets)titleEdgeInsets {
[self msrtl_setTitleEdgeInsets:RSRTLEdgeInsetsWithInsets(titleEdgeInsets)];
}
@end