gpt4 book ai didi

ios - 在 Swift 3 中加粗字符串的单个部分

转载 作者:行者123 更新时间:2023-11-29 00:21:42 27 4
gpt4 key购买 nike

我一直在为这个问题苦苦挣扎。但是我正在处理远程通知,老实说,我似乎无法找到一种方法来将通知的第一行加粗。这是我的代码

  let boldUser = backendless!.userService.currentUser.name!

let string: NSString = "\(boldUser) \nMessage: \(message)" as NSString

let attributedString = NSMutableAttributedString(string: string as String, attributes: [NSFontAttributeName:UIFont.systemFont(ofSize: 15.0)])

let boldFontAttribute = [NSFontAttributeName: UIFont.boldSystemFont(ofSize: 15.0)]

// Part of string to be bold
attributedString.addAttributes(boldFontAttribute, range: string.range(of: "\(boldUser)" as String))

publishOptions.assignHeaders(["ios-alert" : "\(string)", "ios-badge" : "\(counter)", "ios-sound" : "message_tone_3.mp3"])

我不明白为什么这不起作用。据我所知,我做的一切都是正确的,但是当我测试时,没有一个通知是粗体的。我正在尝试将第一行加粗。

最佳答案

像这样使用 NSRange:

let string: NSString = "\(boldUser) \nMessage: \(message)" as NSString

let attributedString = NSMutableAttributedString(string: string as String, attributes: [NSFontAttributeName:UIFont.systemFont(ofSize: 15.0)])

let boldFontAttribute = [NSFontAttributeName: UIFont.boldSystemFont(ofSize: 15.0)]

// Part of string to be bold
// attributedString.addAttributes(boldFontAttribute, range: NSRange(location:0, length:StringArray[0].characters.count))


// Part of string to be bold
attributedString.addAttributes(boldFontAttribute, range: NSRange(location:0, length:StringArray[0].characters.count))

关于ios - 在 Swift 3 中加粗字符串的单个部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44062804/

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