ios – 如何在使用[UIFont preferredFontForTextStyle:xxx]时选择性地使文本斜体化?

ios – 如何在使用[UIFont preferredFontForTextStyle:xxx]时选择性地使文本斜体化?,第1张

概述我像这样创建一个NSMutableAttributedString: UIFont *font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];NSDictionary *attributes = @{ NSFontAttributeName: font };NSMutableAttributedString *attrStr 我像这样创建一个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 traitUIFontDescriptor *FontDesc = [Font FontDescriptor];FontDesc = [FontDesc FontDescriptorWithSymbolicTraits:UIFontDescriptorTraitItalic];// Create the italicized Font from the descriptorUIFont *italicsFont = [UIFont FontWithDescriptor:FontDesc size:Font.pointSize];// Create the attributes dictionary that we'll use to italicize parts of our NSMutableAttributedStringNSDictionary *italicsAttributes = @{ NSFontAttributename: italicsFont };
总结

以上是内存溢出为你收集整理的ios – 如何在使用[UIFont preferredFontForTextStyle:xxx]时选择性地使文本斜体化?全部内容,希望文章能够帮你解决ios – 如何在使用[UIFont preferredFontForTextStyle:xxx]时选择性地使文本斜体化?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1060668.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存