Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
337 views
in Technique[技术] by (71.8m points)

ios - NSAttributedString with icon and multiple alignments

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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...