6

I have a method that sets a NSAttributedString to be bold:

 func setBold(text: String) -> NSMutableAttributedString {

    guard let font = UIFont.CustomNormalBoldItalic() else {
        fatalError("font not found")
    }

    let string = NSMutableAttributedString(string:"\(text)", attributes: [NSFontAttributeName : font])

    self.setAttributedString(string)
    return self
}

And this is how it's called, which works normally:

let formattedString = NSMutableAttributedString()
formattedString.setBold("Your text here")

However I am trying to set the text of a substring of an NSLocalizedString to be bold. So I would try it like so:

let formattedString = NSMutableAttributedString()

return NSAttributedString(string: String.localizedStringWithFormat(
    NSLocalizedString("message", comment: ""), 
    formattedString.setBold(NSLocalizedString("message.day", comment: "")),
    NSLocalizedString("message.time", comment: "")
))

Instead of being "Today starting at 10pm", it gives the following output:

Today{
NSFont = "<UICTFont: 0x7fb75d4f1330> font-family: \"CustomText-MediumItalic\"; font-weight: normal; font-style: italic; font-size: 14.00pt";
} starting at 10pm {
}

Can anyone tell me where I am going wrong or how I can fix this? The reason I have another method is because I have many LocalizedStrings to set bold and thought this might be a simple solution. Open to other ideas/solutions that don't involve lots of repetition/lines of code.

coderdojo
  • 187
  • 1
  • 17
  • I'd go by creating 3 NSAttributedString (one for each translation), and append (the first one has to be a NSMutableAttributedString) them all at the end. You can choose which one you want to set bold or not. – Larme Jul 27 '16 at 16:26
  • 3
    Not all languages will put the 3 parts in the same order, or with the same punctuation... – Wain Jul 27 '16 at 16:35
  • @Wain In this case it's two parts or sub strings, i.e. %@ starting at %@. It's a fairly simple phrase and what I have in other languages matches the english, and has the same punctuation – coderdojo Jul 27 '16 at 22:30
  • @Larme I have tried that however it still does not work and gives me output of: Today{ NSFont = " font-family: \"CustomText-MediumItalic\"; font-weight: normal; font-style: italic; font-size: 14.00pt"; } starting at 10pm { } – coderdojo Jul 28 '16 at 13:25
  • https://github.com/cocodelabs/NSAttributedString-CCLFormat and https://gist.github.com/brentdax/11546648 may be helpful (some changes could be done). I don't know if they support the `%1$@` and `%$2@` stuff (placeholder index). – Larme Jul 29 '16 at 16:45

1 Answers1

2

I'd just make the outer string html and let AttributedString handle the heavy lifting. This is swift 3, but swift 2.3 should be just as straight-forward. There's also some optional handling to be added, but you get the gist of it.

// samples so I don't have to put a string resource in my playground, you
// could just as easily pull these from NSLocalizedString
let format = "<b>%1$@</b> starting at <b>%2$@</b>"
let day = "Today"
let time = "10 PM"
let raw = String(format:format, day, time)

let attr = AttributedString(
    html: raw.data(using: .utf8)!, 
    options: [NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType],
    documentAttributes:nil
)!
David Berry
  • 40,941
  • 12
  • 84
  • 95