When NSAttributedString
appended to NSMutableAttributedString
, it has its own attributes.
UITextView and NSMutableAttributedText
-
30-06-2022 - |
Domanda
UITextView
shows formatted text when I use NSAttributedString
:
NSAttributedString* s1 =
[[NSAttributedString alloc] initWithString:@"Hello there!"
attributes:@{NSForegroundColorAttributeName:[UIColor redColor], NSFontAttributeName: [UIFont preferredFontForTextStyle:UIFontTextStyleBody]}];
_textView.attributedText = s1;
However, when I use NSMutableAttributedString
I see appended attributed strings without formatting. Formatting will be shown for the string it was initialized only.
UIFont *font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody]
NSMutableAttributedString *emailBody = [[NSMutableAttributedString alloc] initWithString:@"" attributes:@{NSForegroundColorAttributeName:[UIColor redColor], NSFontAttributeName: font}];
if (_person.firstName.length > 0) {
[emailBody appendAttributedString:[[NSAttributedString alloc]initWithString:_person.firstName attributes:@{NSFontAttributeName: [UIFont preferredFontForTextStyle:UIFontTextStyleCaption1]}]];
[emailBody appendAttributedString:[[NSAttributedString alloc]initWithString:@"\n" attributes:nil]];
}
if (_person.lastName.length > 0) {
[emailBody appendAttributedString:[[NSAttributedString alloc]initWithString:_person.lastName attributes:@{NSFontAttributeName: [UIFont preferredFontForTextStyle:UIFontTextStyleCaption2]}]];
[emailBody appendAttributedString:[[NSAttributedString alloc]initWithString:@"\n" attributes:nil]];
}
Why?
Soluzione
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow