0

I have a UITableview that gets data from the database and I display it in a UILabel. I want to make a portion of the text that says " ... Read Less" to be bold while leaving the rest of the text alone. The code below simply checks if the post has more than 120 characters if so then I append " ... Read Less " that statement appended I would like to make bold. Right now I have my entire post bold instead of just the appended string any suggestions would be great

func HomeProfilePlaceTVC(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "HomeTVC", for: indexPath) as! HomeTVC

    cell.post.tag = indexPath.row 

    if streamsModel.Posts[indexPath.row].count > 120 {
            cell.post.text = String(streamsModel.Posts[indexPath.row]).appending(" ... Read Less")
            cell.post.font =  UIFont(name:"HelveticaNeue-Bold", size: 15.0)
        }

        else {
             cell.post.text = streamsModel.Posts[indexPath.row]
        }
    return cell

}
Hitesh Surani
  • 12,733
  • 6
  • 54
  • 65
user1591668
  • 2,591
  • 5
  • 41
  • 84
  • Did you try using attributed string . Use the range of the string which you wanna be bold . This may help you https://stackoverflow.com/a/3586943/4489420 – Ananth Apr 12 '19 at 03:17
  • Are you want to bold this text **" ... Read Less"** by count dynamically of prefix text? – Nikunj Kumbhani Apr 12 '19 at 04:38
  • is the below answer helpful for you? Did it solved your query? @user1591668 – Anand Apr 15 '19 at 15:05

1 Answers1

1

You can create an extension like this.

extension String {
    func attributedString(with style: [NSAttributedString.Key: Any]? = nil,
                          and highlightedText: String,
                          with highlightedTextStyle: [NSAttributedString.Key: Any]? = nil) -> NSAttributedString {

        let formattedString = NSMutableAttributedString(string: self, attributes: style)
        let highlightedTextRange: NSRange = (self as NSString).range(of: highlightedText as String)
        formattedString.setAttributes(highlightedTextStyle, range: highlightedTextRange)
        return formattedString
    }
}

And call this method like this for making a string in the middle as Bold

let descriptionText = "This is a bold string"
let descriptionText = descriptionText.attributedString(with: [.font: UIFont.systemFont(ofSize: 12.0, weight: .regular),
                                                              .foregroundColor: .black],
                                                    and: "bold",
                                                    with: [.font: UIFont.systemFont(ofSize: 12.0, weight: .bold),
                                                        .foregroundColor: .black])

Output: "This is a bold string". You can set this to UILabel as follows.

 textLabel.attributedString = descriptionText

This method can be used to highlight any range of text in the entire string with different styles, say bold, italics etc. Please let me know if this helps.

Anand
  • 1,820
  • 2
  • 18
  • 25