gpt4 book ai didi

ios - 使选定的文本字符串 View 粗体、斜体、下划线像 iOS 的 native "Notes"应用程序

转载 作者:搜寻专家 更新时间:2023-11-01 05:52:07 25 4
gpt4 key购买 nike

是否有任何帮助使选定的文本字符串 View 像 iOS 的 native “Notes”应用程序一样粗体、斜体、下划线。请给我有用的链接。我厌倦了寻找一整天。非常感谢。

我附上了我的代码,使属性字符串粗体和斜体都像 iPhone 的 native 应用程序“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()

但它只提供斜体字符串,而不是粗体。我需要斜体和粗体。谢谢。

最佳答案

您的代码中的问题是您正在设置斜体并覆盖刚设置的粗体。您需要的是将 UIFontDescriptor 与 Symbolic Traits 一起使用,如您在此 SO answer 中所见.因此,只需初始化您的系统字体,获取其字体描述符并为其添加 traitBold 和 traitItalic。然后你只需要使用 UIFont 初始化器初始化你的新字体 init(descriptor: UIFontDescriptor, size pointSize: CGFloat):

Swift 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.length))
}
attributedString.endEditing()

关于ios - 使选定的文本字符串 View 粗体、斜体、下划线像 iOS 的 native "Notes"应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48070137/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com