使文本视图的选定字符串像本机"Notes"一样以粗体,斜体,下划线表示. iOS应用 [英] Make selected string of text view Bold, Italic, Underline like native "Notes" app of iOS
问题描述
是否有任何帮助使选定的文本视图字符串(如iOS的本机"Notes"应用程序)加粗,斜体,下划线.请给我有用的链接.我厌倦了寻找一整天.非常感谢.
Is there any help to make selected string of text view Bold, Italic, Underline like native "Notes" app of iOS. Please give me helpful links. I am tired of searching for the whole day. Many Thanks.
我已附加我的代码,以使属性字符串Bold和Italic都像iPhone"Notes"的本机应用程序一样.
I have attached my code, to make attributed string Bold and Italic both like native app of iPhone "Notes".
attributedString.beginEditing()
attributedString.addAttributes([NSFontAttributeName: UIFont.boldSystemFont(ofSize: CGFloat(app_delegate.settings.chatFontSize))], range: range)
attributedString.addAttributes([NSFontAttributeName: UIFont.italicSystemFont(ofSize: CGFloat(app_delegate.settings.chatFontSize))], range: range)
attributedString.endEditing()
但是它只给出斜体字符串,而不是粗体.我需要斜体和粗体.谢谢.
But its giving only Italic string, not also Bold. I need both Italic and Bold. Thanks.
推荐答案
代码中的问题是您正在设置斜体字体并覆盖刚设置的粗体字体.您需要的是将UIFontDescriptor
与符号特征一起使用,如在此SO answer 中所见.因此,只需初始化系统字体,获取其字体描述符,然后向其添加traitBold和traitItalic即可.然后,您只需要使用UIFont初始化程序init(descriptor: UIFontDescriptor, size pointSize: CGFloat)
:
The problem in your code is that you are setting the italic font and overwriting the bold one you've just set. What you need is to use UIFontDescriptor
with Symbolic Traits as you can see in this SO answer. So just initialize your system font, get its font descriptor and add traitBold and traitItalic to it. Then you just need to initialize your new Font using UIFont initializer init(descriptor: UIFontDescriptor, size pointSize: CGFloat)
:
快速4码:
attributedString.beginEditing()
let systemFont: UIFont = .systemFont(ofSize: 32)
if let descriptor = systemFont.fontDescriptor.withSymbolicTraits([.traitBold, .traitItalic]) {
let systemFontBoldAndItalic = UIFont(descriptor: descriptor, size: 32)
attributedString.addAttributes([.font: systemFontBoldAndItalic, .underlineStyle: NSUnderlineStyle.styleSingle.rawValue], range: NSRange(location: 0, length: attributedString.string.utf16.count))
}
attributedString.endEditing()
这篇关于使文本视图的选定字符串像本机"Notes"一样以粗体,斜体,下划线表示. iOS应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!