iosswiftnsattributedstringnslocalizedstring

Making part of localised string bold swift


I have a string let's say " my name is %@ and i study in class %@" now I want to bold the placeholder text which i will be inserting , so that the result will look something like this:" My name is Harsh and i study in class 10" and i will display it on a label

I have already tried using NSAttributedString but since the string will be localised i am not able to use the range parameter of attributed string to make it bold.


Solution

  • let withFormat = "my name is %@ and i study in class %@"
    

    There are different ways to do so, but in my opinion, one of the easiest way would be to use tags:

    Use tags around the placeholders (and other parts if needed):

    let withFormat = "my name is <b>%@</b> and i study in class <b>%@</b>"
    let withFormat = "my name is [b]%@[/b] and i study in class [b]%@[/b]"
    let withFormat = "my name is **%@** and i study in class **%@**"
    

    Tags can be HTML, Markdown, BBCode, or any custom you'd like, then, replace the placeholder values:

    let localized = String(format: withFormat, value1, value2)
    

    Now, depending on how you want to do it, or which tag you used, you can use the init of NSAttributedString from HTML, Markdown, etc, or simply using NSAttributedString(string: localized), look yourself for the tags and apply the render effect needed.

    Here's a little example:

    let tv = UITextView(frame: CGRect(x: 0, y: 0, width: 300, height: 130))
    tv.backgroundColor = .orange
    
    let attributedString = NSMutableAttributedString()
    
    let htmled = String(format: "my name is <b>%@</b> and i study in class <b>%@</b>", arguments: ["Alice", "Wonderlands"])
    let markdowned = String(format: "my name is **%@** and i study in class **%@**", arguments: ["Alice", "Wonderlands"])
    let bbcoded = String(format: "my name is [b]%@[/b] and i study in class [b]%@[/b]", arguments: ["Alice", "Wonderlands"])
    
    let separator = NSAttributedString(string: "\n\n")
    let html = try! NSAttributedString(data: Data(htmled.utf8), options: [.documentType : NSAttributedString.DocumentType.html], documentAttributes: nil)
    attributedString.append(html)
    attributedString.append(separator)
    
    let markdown = try! NSAttributedString(markdown: markdowned, baseURL: nil) //iO15+
    attributedString.append(markdown)
    attributedString.append(separator)
    
    let bbcode = NSMutableAttributedString(string: bbcoded)
    let regex = try! NSRegularExpression(pattern: "\\[b\\](.*?)\\[\\/b\\]", options: [])
    let matches = regex.matches(in: bbcode.string, options: [], range: NSRange(location: 0, length: bbcode.length))
    let boldEffect: [NSAttributedString.Key: Any] = [.font: UIFont.boldSystemFont(ofSize: 12)]
    //We use reversed() because if you replace the first one, you'll remove [b] and [/b], meaning that the other ranges will be affected, so the trick is to start from the end
    matches.reversed().forEach { aMatch in
        let valueRange = aMatch.range(at: 1) //We use the regex group
        let replacement = NSAttributedString(string: bbcode.attributedSubstring(from: valueRange).string, attributes: boldEffect)
        bbcode.replaceCharacters(in: aMatch.range, with: replacement)
    }
    attributedString.append(bbcode)
    
    tv.attributedText = attributedString
    

    Output:

    enter image description here