I have an attributed string that I would like to present on one label with multiple alignments, e.g. one part on the left, one part in center and the other one on the right. I am doing this with
let text = "Pause
09:30 - 11:30
some centered text"
let at = NSMutableAttributedString(string: text)
let label = UILabel(frame: CGRect(x: 0, y: 0, width: 400, height: 60))
var p1 = NSMutableParagraphStyle()
var p2 = NSMutableParagraphStyle()
var p3 = NSMutableParagraphStyle()
p1.alignment = .left
p2.alignment = .right
p3.alignment = .center
p2.paragraphSpacingBefore = -label.font.lineHeight
p3.paragraphSpacingBefore = -label.font.lineHeight
at.addAttribute(.paragraphStyle, value: p1, range: NSRange(location: 0, length: 5))
at.addAttribute(.paragraphStyle, value: p3, range: NSRange(location: 20, length: 6))
at.addAttribute(.paragraphStyle, value: p2, range: NSRange(location: 6, length: 13))
Now I would like to add an image/icon into the label text on the right (the image should be after the text, i.e. close to the right border). How can this be achieved? What do I need to consider with the locations/ranges?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…