【问题标题】:Bold a single part of a String in Swift 3在 Swift 3 中加粗字符串的单个部分
【发布时间】:2017-05-19 06:22:55
【问题描述】:

我已经为此苦苦挣扎了一段时间。但是我在搞乱远程通知,老实说,我似乎无法找到一种方法来加粗通知中的第一行。这是我的代码

  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"])

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

【问题讨论】:

标签: ios iphone swift3 xcode8


【解决方案1】:

像这样使用 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))

【讨论】:

    猜你喜欢
    • 2015-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-27
    相关资源
    最近更新 更多