// // MSRTL.m // YuMi // // Created by duoban on 2024/4/15. // #import "YYLabel+MSRTL.h" #import "NSMutableAttributedString+MSRTL.h" BOOL isRTLString(NSString *string) { if ([string hasPrefix:@"\u202B"]) { return YES; } return NO; } @implementation YYLabel (MSRTL) + (void)load { Method oldTextMethod = class_getInstanceMethod(self,@selector(setAttributedText:)); Method newTextMethod = class_getInstanceMethod(self, @selector(msrtl_setAttributedText:)); method_exchangeImplementations(oldTextMethod, newTextMethod); //交换成功 } -(void)msrtl_setAttributedText:(NSAttributedString *)attributedText{ if(attributedText == nil)return; NSMutableAttributedString * attributedString = [[NSMutableAttributedString alloc]init]; if(self.textAlignment == NSTextAlignmentCenter){ if (isMSRTL()) { [attributedString appendAttributedString:[NSMutableAttributedString createBlankAttributeToMSRTL]]; // [attributedString appendAttributedString:[NSMutableAttributedString createBlankAttributeToMSLTR]]; } [attributedString appendAttributedString:attributedText]; [self msrtl_setAttributedText:attributedString]; return; } if (isMSRTL()) { [attributedString appendAttributedString:[NSMutableAttributedString createBlankAttributeToMSRTL]]; // [attributedString appendAttributedString:[NSMutableAttributedString createBlankAttributeToMSLTR]]; } [attributedString appendAttributedString:attributedText]; [self msrtl_setAttributedText:attributedString]; if(isMSRTL()){ self.textAlignment = NSTextAlignmentRight; }else{ if (self.textAlignment == NSTextAlignmentNatural) { self.textAlignment = NSTextAlignmentLeft; } } } @end