Solution 1
Créer une méthode qui crée NSAttributedString chaque fois que vous devez mettre à jour le contenu de textView
- (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];
}
Utilisation:
[self setDoctorText:NSLocalizedString(@"your string", @"")];
au lieu de cette :
_doctorTextView.attributedText = @"your string";
Solution 2:
Peut-être pas la solution la plus élégante, mais vous pouvez créer NSMutableAttributedString
propriété et définir ses attributs une fois dans viewDidLoad
. Ensuite, chaque fois que vous avez besoin de mettre à jour le texte de textView
, il vous suffit de le faire via le texte attribué mutable stocké.
@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
Le code que vous avez publié ne sera même pas compilé. Veuillez poster un code valide dans votre question. Vous ne pouvez pas affecter 'NSString' à une propriété qui attend' NSAttributedString'. – rmaddy
@rmaddy C'est en fait là que réside le problème. Attributs de la chaîne NSString après localisation car elle ne peut renvoyer qu'un NSString mais j'ai besoin d'un NSAttributedString pour le attributeText. –