objective-c - iOS如何使用runtime对富文本进行国际化
PHPz
PHPz 2017-04-18 09:05:24
0
1
259

使用runtime对程序进行国际化之后发现之前的富文本没有更改,后来单独对富文本重写方法,但是进行国际化之后原本的富文本字符串就变回了普通的字符串,下面是我偷换方法的代码

    
SEL selA1 = @selector(changeTitle:);
Method methodA1 = class_getInstanceMethod(self, selA1);
Method methodB1 = class_getInstanceMethod(self, @selector(setAttributedText:));
method_exchangeImplementations(methodA1, methodB1);
    
- (void)changeTitle:(NSAttributedString *)title{
    NSString *anotherString=[title string];
    NSString *str = NSLocalizedStringFromTable(anotherString, @"File", nil);
    NSMutableAttributedString *attributedString=[[NSMutableAttributedString alloc] initWithString:str attributes:nil];
    // [attributedString setAttributes:@{NSForegroundColorAttributeName:[NSColor redColor]} range:NSMakeRange(0, 5)];
    [self changeTitle:attributedString];
}

当前类是UILabel类,我偷换的是setAttributedText:方法.还有一个问题就是即时我对富文本本地化成功了,但是富文本的格式也会变化,range会改变,请问如何解决此问题

PHPz
PHPz

学习是最好的投资!

全部回覆(1)
大家讲道理

= = 個人是不建議這麼做的。 runtime 這個東西使用一定要有節制,感覺用來做在地化可以說得上是濫用了。

如果要用的話,建議也只是在原方法的基礎上稍作修改,最後還是調一下原方法。而不是把整個方法的意義都改了。像你的程式碼裡, NSString *anotherString=[title string];這一行就失去了 attributes,變成普通 string 了。

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!