Esqarrouth Esqarrouth - 1 year ago 34
iOS Question

Is there a unicode way to make part of a string bold?

in Localizable.strings

"rulesText" = "No illegal posting! \n No weird stuff! \n There is no tolerance for objectionable content, they will be removed!";


Can I make part of this bold? Like No weird stuff! or something in this sense using unicode characters? Or some other way?

I use it like this:

textView.text = "\n\n " + NSLocalizedString("rulesText", comment: "")

Answer Source

Thanks to Martins previous answer I edited his solution to match my case and it works perfectly.
Check out and upvote his solution: create an attributed string out of plain (Android formated) text in swift for iOS

So this basically changes:

<a>hey</a> to size 14 bold  
<b>hey</b> to size 12 bold  
<u>hey</u> to underlined  

its easy to add more features to it.

//localizable.strings
"rulesText" = "\n\n<a>The following will be removed</a> \n\n<b><u>Harassment</u></b>\n\nOther Stuff"

//viewdidload
textView.font = UIFont(name: "HelveticaNeue-Light", size: 12) //This is here to set up rest of the texts font
textView.attributedText = convertText(NSLocalizedString("rulesText", comment: "")) 

//method for string conversation
func convertText(inputText: String) -> NSAttributedString {

    var attrString = NSMutableAttributedString(string: inputText)
    let boldFont = UIFont(name: "Helvetica-Bold", size: 12)
    let boldBigFont = UIFont(name: "Helvetica-Bold", size: 14)

    attrString = fixText(attrString, attributeName: NSFontAttributeName, attributeValue: boldFont!, propsIndicator: "<b>", propsEndIndicator: "</b>")
    attrString = fixText(attrString, attributeName: NSFontAttributeName, attributeValue: boldBigFont!, propsIndicator: "<a>", propsEndIndicator: "</a>")
    attrString = fixText(attrString, attributeName: NSUnderlineStyleAttributeName, attributeValue: NSUnderlineStyle.StyleDouble.rawValue, propsIndicator: "<u>", propsEndIndicator: "</u>")

    return attrString
}

func fixText(inputText:NSMutableAttributedString, attributeName:AnyObject, attributeValue:AnyObject, propsIndicator:String, propsEndIndicator:String)->NSMutableAttributedString{
    var r1 = (inputText.string as NSString).rangeOfString(propsIndicator)
    while r1.location != NSNotFound {
        let r2 = (inputText.string as NSString).rangeOfString(propsEndIndicator)
        if r2.location != NSNotFound  && r2.location > r1.location {
            let r3 = NSMakeRange(r1.location + r1.length, r2.location - r1.location - r1.length)
            inputText.addAttribute(attributeName as String, value: attributeValue, range: r3)
            inputText.replaceCharactersInRange(r2, withString: "")
            inputText.replaceCharactersInRange(r1, withString: "")
        } else {
            break
        }
        r1 = (inputText.string as NSString).rangeOfString(propsIndicator)
    }
    return inputText
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download