我像这样创建一个 NSMutableAttributedString
:
UIFont *font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
NSDictionary *attributes = @{ NSFontAttributeName: font };
NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:str attributes:attributes];
现在我想斜体化 attrStr 的特定范围,例如:
- (void)setAttributes:(NSDictionary *)attrs range:(NSRange)range;
但由于我从未要求过特定字体,所以我不确定该如何处理。
最佳答案
// Create the normal body style font
UIFont *font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
// Get the font's descriptor and add the italics trait
UIFontDescriptor *fontDesc = [font fontDescriptor];
fontDesc = [fontDesc fontDescriptorWithSymbolicTraits:UIFontDescriptorTraitItalic];
// Create the italicized font from the descriptor
UIFont *italicsFont = [UIFont fontWithDescriptor:fontDesc size:font.pointSize];
// Create the attributes dictionary that we'll use to italicize parts of our NSMutableAttributedString
NSDictionary *italicsAttributes = @{ NSFontAttributeName: italicsFont };
关于ios - 使用 +[UIFont preferredFontForTextStyle :xxx]? 时如何有选择地将文本设为斜体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25426189/