6

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.....

JKMania
  • 239
  • 1
  • 6
  • 17

4 Answers4

26

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
}
Hamer
  • 1,354
  • 1
  • 21
  • 34
  • 1
    This 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
  • 2
    not 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
7

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};
}
ita9178
  • 65
  • 1
  • 6
Gad
  • 2,867
  • 25
  • 35
5

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.

maxmzd
  • 61
  • 3
  • 3
0

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.

Paulo
  • 1,245
  • 10
  • 8