2

I would like to know if is it possible, on a UILabel that uses NSMutableAttributedString, to remove the colored background that is set in the empty spaces on the "beginning" of each line, when the align is set to right.

Obs.: everything works fine when the align is set to left

enter image description here

enter image description here

This is what I expect:

enter image description here


Extra question: Is it possible to leave a space between the lines without a color?

Augusto Carmo
  • 4,386
  • 2
  • 28
  • 61

2 Answers2

1

Set backgroundColor of your label to White or Clear color and apply the background color to your NSMutableAttributedString as like below,

//In Swift
let mutableAttributedString = NSMutableAttributedString() //Initialize your string here
mutableAttributedString.addAttribute(NSAttributedString.Key.backgroundColor, value: UIColor.yellow, range: NSMakeRange(0, mutableAttributedString.length))

//In Objective C
NSMutableAttributedString *mutableAttributedString; //Initialize your string here
[mutableAttributedString addAttribute:NSBackgroundColorAttributeName value:[UIColor yellowColor] range:NSMakeRange(0, mutableAttributedString.length)];

If you are using storyBoard change backgroundColor of attributed string as show in attached image. enter image description here

Just realized that above solution will work for below scenarios,

  1. Maximum two lines with Left alignment
  2. Maximum one line with Right and Center alignments

Please follow this post for complete solution.

Natarajan
  • 3,241
  • 3
  • 17
  • 34
1

So I did spend 45 minutes just to find a solution to this problem. I've been using my own UILabel extension for highlighting texts but unfortunately it highlights the empty spaces or rather the indent spaces when the textAlignment is set to right, like this problem of yours.

I realised I couldn't find any other solution rather than using this pod: TTTAttributedLabel https://github.com/TTTAttributedLabel/TTTAttributedLabel

This handles setting attributed string to the UILabel very well. The pod though was written in Objective-C, so you'll need a bridging header for that. I suppose you already know how to port Objective-C codes into your Swift projects.

override func viewDidLoad() {
    super.viewDidLoad()

    self.view.backgroundColor = .gray

    let label = TTTAttributedLabel(frame: .zero)
    label.textAlignment = .right
    label.numberOfLines = 0


    let attr: [NSAttributedString.Key: Any] = [
        .font: UIFont.boldSystemFont(ofSize: 16.0),
        .foregroundColor: UIColor.black,
        .backgroundColor: UIColor.green
    ]

    label.text = NSAttributedString(string: "How can I Remove\nthe space at the\nbeginning?", attributes: attr)

    self.view.addSubview(label)
    label.translatesAutoresizingMaskIntoConstraints = false

    self.view.addConstraint(NSLayoutConstraint(item: label, attribute: .top, relatedBy: .equal, toItem: self.view, attribute: .top, multiplier: 1.0, constant: 300))
    self.view.addConstraint(NSLayoutConstraint(item: label, attribute: .leading, relatedBy: .equal, toItem: self.view, attribute: .leading, multiplier: 1.0, constant: 0))
    self.view.addConstraint(NSLayoutConstraint(item: label, attribute: .trailing, relatedBy: .equal, toItem: self.view, attribute: .trailing, multiplier: 1.0, constant: 0))

}

Result: enter image description here

I hope this helps!

Glenn Posadas
  • 12,555
  • 6
  • 54
  • 95