是否有任何帮助,使选定的文本视图字符串Bold,Italic,Underline像iOS的原生"Notes"应用程序.请给我一些有用的链接.我厌倦了整整一天的搜索.非常感谢.
我附加了我的代码,使属性字符串Bold和Italic都像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()
Run Code Online (Sandbox Code Playgroud)
但它只给出斜体字符串,而不是粗体字.我需要Italic和Bold.谢谢.
您的代码中的问题是您正在设置斜体字体并覆盖您刚刚设置的粗体字体.你需要的是使用 UIFontDescriptor符号特征,你可以在这个SO 答案中看到.因此,只需初始化您的系统字体,获取其字体描述符并向其添加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.string.utf16.count))
}
attributedString.endEditing()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1084 次 |
| 最近记录: |