Ios 在本地化NSAttributedString时,是否仍有保留其属性的方法?
我正在尝试本地化NSAttribute字符串 但是,我发现NSString的属性在本地化后都消失了 是否仍有保留这些属性的方法Ios 在本地化NSAttributedString时,是否仍有保留其属性的方法?,ios,objective-c,localization,nslocalizedstring,Ios,Objective C,Localization,Nslocalizedstring,我正在尝试本地化NSAttribute字符串 但是,我发现NSString的属性在本地化后都消失了 是否仍有保留这些属性的方法 self.doctorUITextView.attributedText = NSLocalizedString([doctorNSMutableAttributedString string], nil); 解决方案1 创建一个方法,该方法在每次需要更新textView的内容时创建NSAttributedString - (void) setDoctorText:
self.doctorUITextView.attributedText = NSLocalizedString([doctorNSMutableAttributedString string], nil);
解决方案1 创建一个方法,该方法在每次需要更新
textView
的内容时创建NSAttributedString
- (void) setDoctorText: (NSString *) string {
//create your attributes dict
UIFont *keyFont = [UIFont fontWithName:@"Courier" size:16];
NSDictionary *attributes = [NSDictionary dictionaryWithObject:keyFont forKey:NSFontAttributeName];
_doctorTextView.attributedText = [[NSAttributedString alloc] initWithString:string attributes:attributes];
}
用法:
[self setDoctorText:NSLocalizedString(@"your string", @"")];
与此相反:
_doctorTextView.attributedText = @"your string";
解决方案2:
也许不是最优雅的解决方案,但您可以创建nsmutableAttributeString
属性,并在viewDidLoad
中设置它的属性。然后,每当需要更新textView
的文本时,只需通过存储的可变属性文本即可
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextView *doctorTextView;
@property (nonatomic, strong) NSMutableAttributedString *doctorAttributedString;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIFont *keyFont = [UIFont fontWithName:@"Courier" size:16];
NSDictionary *attributes = [NSDictionary dictionaryWithObject:keyFont forKey:NSFontAttributeName];
_doctorAttributedString = [[NSMutableAttributedString alloc] initWithString:@" " attributes:attributes];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
_doctorAttributedString.mutableString.string = NSLocalizedString(@"your string", @"");
_doctorTextView.attributedText = _doctorAttributedString;
}
@end
你发布的代码甚至无法编译。请在问题中输入有效代码。您不能将
NSString
分配给需要NSAttributedString
@rmaddy的属性这实际上就是问题所在。本地化之后,NSString的属性都消失了,因为它只能返回NSString,但我需要一个NSAttributedText的NSAttributedString。