I wondered how to set the UITextView so that the text inside aligns to the bottom? Like, if you start typing, you will always type in the lowest row?
-
I used the method @Isaac Overacker recommended and it worked out quite well. – Aug 31 '11 at 07:28
-
Glad to hear it worked well for you. Would you mind marking my response as the answer to your question? Thanks! – Isaac Overacker Aug 31 '11 at 21:18
3 Answers
I don't believe there is a built-in way to vertically align the text in a UITextView
, but you should be able to use the contentOffset
property of the UITextView
class to accomplish this. See this blog post for more details:
http://imagineric.ericd.net/2011/03/10/ios-vertical-aligning-text-in-a-uitextview/
Update: since the above link appears to be dead, I used the Wayback Machine to retrieve an archived copy and have pasted the text of the article below.
The default and expected behavior of a UITextView (multi-line text) out of the box in iOS is to align the text top left in it’s container. That works well most of the time. However I recently had need to either center the text vertically within the control or have all the text align to the bottom of the control.
You can add an observer to the control and when it’s content size changes (text is set), fire a method. The method can then handle how the text is actually positioned in the control. If it doesn’t make sense to do the alignment, it will default to the normal behavior of the control (top vertical alignment).
Here we go:
- (void) viewDidLoad {
[textField addObserver:self forKeyPath:@"contentSize" options:(NSKeyValueObservingOptionNew) context:NULL];
[super viewDidLoad];
}
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
UITextView *tv = object;
//Center vertical alignment
//CGFloat topCorrect = ([tv bounds].size.height - [tv contentSize].height * [tv zoomScale])/2.0;
//topCorrect = ( topCorrect < 0.0 ? 0.0 : topCorrect );
//tv.contentOffset = (CGPoint){.x = 0, .y = -topCorrect};
//Bottom vertical alignment
CGFloat topCorrect = ([tv bounds].size.height - [tv contentSize].height);
topCorrect = (topCorrect <0.0 ? 0.0 : topCorrect);
tv.contentOffset = (CGPoint){.x = 0, .y = -topCorrect};
}
You can decide which you’d like to use, or flush the method out to take an argument for which type of vertical alignment you’d like to use. This works quite well and it would have been nice if the properties were built into the control to begin with.
If you can use it, enjoy.

- 1,385
- 10
- 22
-
1I had to use `CGSize contentSize = [tv sizeThatFits:CGSizeMake(tv.frame.size.width, FLT_MAX)];` because contentSize was giving me full height of the control. Other than that, that's a good solution. – Schultz9999 Apr 02 '14 at 04:45
-
3You should include the code in your answer so we don't miss it if the blog ever goes down – Jan Dec 12 '18 at 21:21
-
@biomiker I have updated the answer with the text of the article retrieved from the Wayback Machine. Cheers. – Isaac Overacker Dec 27 '19 at 19:29
Using autolayout worked for me:
1) Layout your UITextView
so that the height is large enough for a single line of text and the bottom is pinned to the bottom of the screen (or something underneath the text view).
2) Keep a reference in your view controller to the NSLayoutConstraint
for the text view height.
3) Setup your view controller to be a UITextViewDelegate
4) adjust the height of the text view when text is entered and force a layout. For example:
- (void)textViewDidChange:(UITextView *)textView;
{
self.textViewHeightConstraint.constant = textView.contentSize.height;
[self.textView layoutIfNeeded];
}

- 7,169
- 1
- 44
- 45
One method could be to use a multi-line UILabel then use a method like the following:
http://texnological.blogspot.com/2011/08/how-to-do-vertical-alignment-for.html
Credit to the author!

- 1,034
- 2
- 8
- 21
-
1The `UILabel` class cannot be directly typed into, though. You could do some work to manually update the `UILabel` instance as the user types, but I don't think this is what @Urban is asking for. – Isaac Overacker Aug 29 '11 at 20:31
-
Ah ok didnt read the typing part, could he not use a UITextField and then something like the following: textField.contentVerticalAlignment = UIControlContentVerticalAlignmentBottom; – Michael M Aug 29 '11 at 20:37