I'm trying to parse my own HTML string into an NSAttributedString for rendering in a UITextView.
So, for when the string appears as such:
<strong><em>
This should be both bold and italic</strong></em>
I want to apply a distinct bold and italic font to the eventual NSAttributedString, so that if the user ever removes either the bold or italic font from it during editing, one of the remaining fonts will stay intact.
This is the main reason why I don't simply want to apply a single font style like bold-italic, as other SO answers have suggested.
To make text bold, select and highlight the text first. Then hold down Ctrl (the control key) on the keyboard and press B on the keyboard. To make text italic, select and highlight the text first. Then hold down Ctrl (the control key) on the keyboard and then press the I on the keyboard.
To make your selected text italic or start writing text in italic, press the Ctrl + I keys on your keyboard.
let label = UILabel() label. attributedText = NSMutableAttributedString() . bold("Address: ") .
Changing an Attributed String First create an NSMutableAttributedString with a new font attribute. You don't use textView. text . Then append another attributed string that doesn't have any attributes set.
Here is my answer after looking through different sources:
UIFontDescriptor *fontDescriptor = [UIFontDescriptor preferredFontDescriptorWithTextStyle:UIFontTextStyleBody];
UIFontDescriptor *changedFontDescriptor;
NSDictionary *attributes;
uint32_t existingTraitsWithNewTrait = [fontDescriptor symbolicTraits] | UIFontDescriptorTraitBold | UIFontDescriptorTraitItalic;
changedFontDescriptor = [fontDescriptor fontDescriptorWithSymbolicTraits:existingTraitsWithNewTrait];
UIFont *updatedFont = [UIFont fontWithDescriptor:changedFontDescriptor size:0.0];
attributes = @{ NSFontAttributeName : updatedFont };
attributedString = [[NSAttributedString alloc] initWithString:yourString attributes:attributes];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With