How to align text horizontally & vertically in UITextView? I want align text in UITextView with horizontal alignment & vertical alignment. Is there any custom way? Please help me out.....
4 Answers
This is a Swift solution and is different now since content insets and offsets have changed slightly. This solution works in Xcode 7 beta 6.
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
textView.addObserver(self, forKeyPath: "contentSize", options: NSKeyValueObservingOptions.New, context: nil)
}
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
textView.removeObserver(self, forKeyPath: "contentSize")
}
Apple has changed how content offsets and insets work this slightly modified solution is now required to set the top on the content inset instead of the offset.
/// Force the text in a UITextView to always center itself.
override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
let textView = object as! UITextView
var topCorrect = (textView.bounds.size.height - textView.contentSize.height * textView.zoomScale) / 2
topCorrect = topCorrect < 0.0 ? 0.0 : topCorrect;
textView.contentInset.top = topCorrect
}

- 1,354
- 1
- 21
- 34
-
1This worked great for me with IOS 9/ Swift 2 thanks! – Kitson Oct 18 '15 at 17:08
-
This worked but if I have more than one UITextField, it only applies to one of them – Ashish Musale Jun 04 '16 at 08:56
-
2not working in Xcode 8. does anyone have any ideas how to do this? – George Asda Jan 14 '17 at 12:06
-
Anybody achieved this with iOS 10, Swift 3 & Xcode 8? – gaskbr Jan 27 '17 at 17:54
-
Working fine for me on iOS 10, Swift 3, Xcode 8. (Slight syntax changes needed) – David Schmoecker May 09 '17 at 20:15
-
In swift 4.2, override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) { let textView = object as! UITextView var topCorrect = (textView.bounds.size.height - textView.contentSize.height * textView.zoomScale) / 2 topCorrect = topCorrect < 0.0 ? 0.0 : topCorrect; textView.contentInset.top = topCorrect } – iOSDev May 07 '20 at 12:07
As far as I know there is no built in method for vertical alignment of a UITextView
. However, by updating the the contentOffset
you can get vertically centered text:
[textView setTextAlignment:NSTextAlignmentCenter];
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[textView addObserver:self forKeyPath:@"contentSize" options:(NSKeyValueObservingOptionNew) context:NULL];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[textView removeObserver:self forKeyPath:@"contentSize"];
}
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
UITextView *tv = object;
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};
}
-
-
@Cindeselia The way the code is currently written, you would need to add it where your text view is located. – Gad Jun 22 '14 at 08:26
-
Note that changing contentOffset only seems to work if the text view's "Selectable" flag is checked – Maiaux Oct 01 '14 at 13:09
Vertically aligning middle with Swift:
In viewDidLoad:
textField.addObserver(self, forKeyPath: "contentSize", options: NSKeyValueObservingOptions.New, context: nil)
Then somewhere else in your view controller:
override func observeValueForKeyPath(keyPath: String, ofObject object: AnyObject, change: [NSObject : AnyObject], context: UnsafeMutablePointer<Void>) {
var topCorrect : CGFloat = (textField.frame.height - textField.contentSize.height);
topCorrect = topCorrect < 0.0 ? 0.0 : topCorrect / 2
textField.contentOffset = CGPoint(x: 0, y: -topCorrect)
}
where textField is wired up to the actual text field in your view.

- 61
- 3
- 3
-
that works properly but somehow it doesn't work with an unwind segue... any idea why? – SKYnine Sep 06 '15 at 19:16
You should provide the context on what you mean to do when you want to change the horizontal and vertical alignment,
The UITextview has a container and a frame property that you can align with another UITextview/ Imageview, etc. The frame property has an origin and a size that you can modify to align your views.
The text within the UITextview has a property called the textalignment which you can set to NSTextAlignmentLeft, NSTextAlignmentJustified, As suggested, you may also adjust the content offset property of the container but I find that adjusting the frame is more straight forward.

- 1,245
- 10
- 8