How to apply both bold and italic font to an NSAttributedString?

前端 未结 1 791
终归单人心
终归单人心 2020-12-17 19:07

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:

相关标签:
1条回答
  • 2020-12-17 19:40

    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];
    
    0 讨论(0)
提交回复
热议问题