0

I found this which works perfectly on UITextViews linked to the viewController, but I can not figure out how to get it to work correctly for a UITextView created within a separate class.

https://stackoverflow.com/a/41387780/8635708

I want the view to be three units (character height) tall and the contents to be centered vertically.

This is what happens: enter image description here

On the other hand, if I make the view 4 units tall, it looks good - but I don't have the space for that. enter image description here

Here is what I did (which seems to work for the single line).

class TestClass: UIView {

    var titleView1 = UITextView()
    var titleView2 = UITextView()

    override func layoutSubviews() {
        titleView1.centerVertically()
        titleView2.centerVertically()
    }


    override func draw(_ rect: CGRect) {
        super.draw(rect)

        let h = 3 * GlobalConstants.fontSize // 3 * 14 = 42

        var title = "Single Line"
        var bounds = CGRect(x: 10, y: 10, width: 100, height: h)
        displayTitle(str: title, column: &titleView1, bounds: bounds)

        title = "First Line\nSecondLine"
        bounds = CGRect(x: 120, y: 10, width: 100, height: h)
        displayTitle(str: title, column: &titleView2, bounds: bounds)
    }


    func displayTitle(str: String, column: inout UITextView, bounds: CGRect) {
        
        let view = UITextView(frame: bounds)
        
        let attributes: [NSAttributedString.Key: Any] = [
            .foregroundColor: UIColor.blue,
            .font: UIFont.italicSystemFont(ofSize: 16)
        ]
        let attributedText = NSMutableAttributedString(string: str, attributes: attributes)

        view.attributedText = attributedText
        view.textAlignment = .center

        view.centerVertically()

        self.autoresizesSubviews = true
        self.addSubview(view)

    }

}

I don't believe vertical alignment can be done simply with an NSAttributedString but if possible, please let me know how!

Thanks!

Greg
  • 667
  • 8
  • 19
  • Is this going to be editable by the user? So, when the user types, the text will shift vertically as new lines are added? – DonMag Oct 01 '20 at 20:45
  • No, they will be titles to a table that will remain fixed. – Greg Oct 01 '20 at 20:49
  • 1
    Not editable? Hmmm... then why use `UITextView`? `UILabel` automatically vertically centers the text... – DonMag Oct 01 '20 at 20:52
  • Of course! Originally, I had the title and all the data in the table together but separated out the title to format it... Sometimes you have to come up for air and take another look at the big picture! Thank you! (Again!) – Greg Oct 01 '20 at 21:08
  • That easy solution aside, why does the above method not work? What if the text did in fact change? – Greg Oct 01 '20 at 21:10

1 Answers1

-1

I´m setting number of lines to 0 and using "\n" to move Text up or down.

Very easy to use!

"Single Line" + "\n\n\n" to move the text up 3 lines "\n\n\n" + "Single Line" to move the text down 3 lines

Ronald Hofmann
  • 1,390
  • 2
  • 15
  • 26