44 lines
1.2 KiB
Objective-C
44 lines
1.2 KiB
Objective-C
//
|
|
// MSRTL.m
|
|
// YuMi
|
|
//
|
|
// Created by duoban on 2024/4/11.
|
|
//
|
|
|
|
#import "UILabel+MSRTL.h"
|
|
|
|
@implementation UILabel (MSRTL)
|
|
|
|
+ (void)load {
|
|
|
|
Method oldInitMethod = class_getInstanceMethod(self,@selector(initWithFrame:));
|
|
Method newInitMethod = class_getInstanceMethod(self, @selector(msrtl_initWithFrame:));
|
|
method_exchangeImplementations(oldInitMethod, newInitMethod); //交换成功
|
|
|
|
Method oldTextMethod = class_getInstanceMethod(self,@selector(setTextAlignment:));
|
|
Method newTextMethod = class_getInstanceMethod(self, @selector(msrtl_setTextAlignment:));
|
|
method_exchangeImplementations(oldTextMethod, newTextMethod); //交换成功
|
|
}
|
|
|
|
- (instancetype)msrtl_initWithFrame:(CGRect)frame {
|
|
if ([self msrtl_initWithFrame:frame]) {
|
|
self.textAlignment = NSTextAlignmentNatural;
|
|
}
|
|
return self;
|
|
}
|
|
|
|
- (void)msrtl_setTextAlignment:(NSTextAlignment)textAlignment {
|
|
if (isMSRTL()) {
|
|
if (textAlignment == NSTextAlignmentNatural || textAlignment == NSTextAlignmentLeft) {
|
|
textAlignment = NSTextAlignmentRight;
|
|
} else if (textAlignment == NSTextAlignmentRight) {
|
|
textAlignment = NSTextAlignmentLeft;
|
|
}
|
|
}
|
|
[self msrtl_setTextAlignment:textAlignment];
|
|
}
|
|
|
|
|
|
|
|
@end
|