Search code examples
swiftuitableviewfooternsattributedstringnsattributedstringkey

How to have selected words in UITableView footer in bold typeface in Swift?


I am creating an array of String(s) to use with the titleForFooterInSection table view delegate method. Each String will span over several lines and some of the words need to be emphasised.

How can I have only selected words of a string have a bold typeface?

I would like to achieve what is in this picture: table view footer with bold

Thank you


Solution

  • What I did in some project is that I created an object like so:

    struct StringWithStyle {
        let font: UIFont
        let color: UIColor
        let text: String
        let backgroundcolor: UIColor
    
        init(font: UIFont,
             color: UIColor,
             text: String,
             backgroundColor: UIColor = .clear) {
            self.font = font
            self.color = color
            self.text = text
            self.backgroundcolor = backgroundColor
        }
    
        var mutableAttrString: NSMutableAttributedString {
            let attributes = [NSAttributedString.Key.font: font,
                              NSAttributedString.Key.foregroundColor: color,
                              NSAttributedString.Key.backgroundColor: backgroundcolor]
            return NSMutableAttributedString(string: text, attributes: attributes)
        }
    }
    

    You can of course set the font to stay same or create common styles used in your app.

    Then I have and extension to pass the text with the styles

    static func textWithMultipleStyles(_ styles: [StringWithStyle]) -> NSMutableAttributedString {
        var allTextStyles = styles
        let text = allTextStyles.removeFirst().mutableAttrString
        guard !allTextStyles.isEmpty else {
            return text
        }
        for nextText in allTextStyles {
            text.append(nextText.mutableAttrString)
        }
        return text
    }
    

    And to use you:

    let example = String.textWithMultipleStyles([StringWithStyle(font: UIFont.boldSystemFont(ofSize: 16.0),
                                                          color: .black,
                                                          text: "First String"),
                                              StringWithStyle(font: UIFont.systemFont(ofSize: 13, weight: .semibold),
                                                          color: .red,
                                                          text: "Second string")])
    

    Maybe there is better way, but for me like this I have 3-4 common styles used in the app and can construct multiple style strings easily.

    Else you can use ranges

    let boldText = "Some bold text"
    let message = "This is a sentence with bold text \(boldText)"
    let range = (message as NSString).rangeOfString(boldText)
    let attributedString = NSMutableAttributedString(string: message)
    attributedString.addAttribute(NSFontAttributeName, value: UIFont.boldSystemFontOfSize(label.font.pointSize), range: range)
    label.attributedText = attributedString