// // 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{ 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