5

The solution below works correctly only for names that fit on one line

        cell.accessoryTitleLabel.text = data.title
        cell.accessoryTitleLabel.sizeToFit()
        cell.discountIcon.frame.origin.x = cell.accessoryTitleLabel.frame.maxX + 7
        cell.discountIcon.hidden = discount == 0

But I need to put a discount icon at the end of the last line: enter image description here

Sergey
  • 672
  • 1
  • 8
  • 16

1 Answers1

5

The best way is to insert your image directly on label by NSTextAttachment and resizing the image as per requirement, in that way you don't have to calculate any spacing and width.


Swift 3 solution

var img_attachment = NSTextAttachment()
img_attachment.image = UIImage(named: "name_of_image")
img_attachment.bounds = CGRect(x: CGFloat(0), y: CGFloat(0), width: CGFloat(ImgWidth), height:CGFloat(ImgHeight)) // you can specify the size and bounds of discount image
var attributedString = NSAttributedString(attachment: img_attachment)
var lblString = NSMutableAttributedString(string: "your text here")
lblString.append(attributedString)
cell.accessoryTitleLabel.attributedText = lblString

Vizllx
  • 9,135
  • 1
  • 41
  • 79