Center Text Vertically in TextView?

I am trying to figure out if there is a way to center text in a TextView so that it centered on the page.


I created my textView, then I go out and get a string from a file and I load that string. Sometimes the string takes up 3 lines, and sometimes it takes up 5 or 6 lines. I want the text to always be centered on the middle of the page, and I can't figure out how to accomplish that.


Thanks in advance

let a = UITextView(frame: CGRect(x: 0, y: 0, width: 300, height: 300))
let style = NSMutableParagraphStyle()
style.alignment = .center
let text = NSAttributedString(string: yourtextcontentstring,
                              attributes: [NSParagraphStyleAttributeName:style])
a.attributedText = text

Thanks for your help. I put this into my code and now my text doesnt display at all. I am brand new at this and am trying to learn and thought this would be something that I could do to make one of my practice excercises have a little more meaning for myself and look better.


my UITextView name is myView and my content string is myString which I am getting from a random number generator hitting against an array.


is "a' a brand new UITextView that is being added to my page? If I had one set up already "myView" can i just use that one? So here is what I have:


myString = movies?[randomMovies]

let a = UITextView(frame: CGRect(x: 0, y: 0, width: 300, height: 300))

let style = NSMutableParagraphStyle()

style.alignment = .center

let text = NSAttributedString(string: myString!,

attributes: [NSParagraphStyleAttributeName:style])

a.attributedText = text

//myView.text = myString


Thanks

I have been playing around with this some more and I think I asked the question wrong or did not provide enough details.


if I am correct then these lines set the alignment of the text, so it will be aligned on the center running from top to bottom.

let style = NSMutableParagraphStyle()

style.alignment = .center

let text = NSAttributedString(string: myString!,

attributes: [NSParagraphStyleAttributeName:style])


What I want is the whole block of text to be centered within the UITextView so for example. If my movie is "Grease" then it would appear on one line in the middle of the TextView. If my moive was "The Nightmare of Friday the 13th Part 9: Jason versus Freddy versus Alien versus Predator" then that would be centered in the textview as well but it would span more lines.

You have use the one you created before:


let style = NSMutableParagraphStyle()
style.alignment = .center
let text = NSAttributedString(string: myString!,
                              attributes: [NSParagraphStyleAttributeName:style])
myView.attributedText = text
Accepted Answer

If you want to center the text vertically, you should probably be using UILabel. You can still use the same NSAttributedString to center the text horizontally. UILabel centers the text vertically by default, but you have to set the number of lines to make it show several lines. If you set it to 0, it will be as large as it needs to be:


myView.numberOfLines = 0
Center Text Vertically in TextView?
 
 
Q