0

I have been struggling with this for a while. But I am messing around with remote notifications and I honestly can't seem to figure out a way to bold the first line in the notification. This is my code

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

I don't get why this is not working. From my knowledge, I am doing everything right, but when Im testing, none of the notification is bold at all. I am trying to get the first line bolded.

  • Possible duplicate of [Bold & Non-Bold Text In A Single UILabel?](http://stackoverflow.com/questions/3586871/bold-non-bold-text-in-a-single-uilabel) – Mo Abdul-Hameed May 19 '17 at 08:13

1 Answers1

1

Use NSRange like this:

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))
Jogendar Choudhary
  • 3,476
  • 1
  • 12
  • 26