-
Re: NSAttributedString: left and right alignment
DelawareMathGuy Aug 14, 2019 10:16 AM (in response to Albinus)hi Albinus,
using some code i was looking at yesterday (you may recognize it), you can do this with tabStops. i did exactly what you asked, but placing the text in a 4-line label as white text on a blue background ("testLabel" was set up as an @IBOutlet in a storyboard and this code is executed on viewWillAppear())
testLabel.backgroundColor = UIColor.blue testLabel.numberOfLines = 4 let myStyle = NSMutableParagraphStyle() let labelWidth = testLabel.frame.size.width myStyle.tabStops = [NSTextTab(textAlignment: .left, location: 0.0, options: [:]), NSTextTab(textAlignment: .right, location: labelWidth, options: [:])] let tabChar = "\t" let returnChar = "\r" let content = "Into my heart an air that kills" + tabChar + "1" + returnChar + "From yon far country blows:" + tabChar + "2" + returnChar + "What are those blue remembered hills" + tabChar + "3" + returnChar + "What spires, what farms are those?" + tabChar + "4" + returnChar let attributes: [NSAttributedString.Key: Any] = [.foregroundColor: UIColor.white] let attrString = NSMutableAttributedString(string: content, attributes: attributes) attrString.addAttribute(.paragraphStyle, value: myStyle, range: NSRange(location: 0, length: content.count-1)) testLabel.attributedText = attrString
the results should be transferrable to a UITextView()
hope that helps,
DMG
-
Re: NSAttributedString: left and right alignment
Albinus Aug 14, 2019 10:33 AM (in response to DelawareMathGuy)I didn't realize that one could set tab stops in a paragraph style. That is exactly what I needed to know! It should solve all my problems. Many thanks.
-
-
Re: NSAttributedString: left and right alignment
Claude31 Aug 14, 2019 10:16 AM (in response to Albinus)For such precise presentation, I would use 2 textView and sync when scroll (if ever you scroll).
It is not totally trivial to do, but result is clean.
PS: it would be so much simpler to write numbers on the left (kidding ).
-
Re: NSAttributedString: left and right alignment
Albinus Aug 14, 2019 10:35 AM (in response to Claude31)I did try putting them on the left, but when they get too long (up to 4 digits in the Iliad, for example), they push the text out of alignment. But now that I'm aware of how to set tab stops in paragraph styles, that problem can be solved too.
-