iOS 7 Uitextview Vertical Alignment

iOS 7 UITextView vertical alignment

Try to call -sizeToFit after passing the text. This answer could be useful to Vertically align text within a UILabel.


[UPDATE]

I update this answer o make it more readable.

The issue is that from iOS7, container view controllers such as UINavigationController or UITabbarController can change the content insets of scroll views (or views that inherit from it), to avoid content overlapping. This happens only if the scrollview is the main view or the first subviews. To avoid that you should disable this behavior by setting automaticallyAdjustsScrollViewInsets to NO, or overriding this method to return NO.

UITextView Vertical Alignment in iOS 7.1

I had the same problem and what worked for me was using
[tv sizeThatFits:tv.bounds.size].height instead of tv.contentSize.height

Center the text in a UITextView, vertically and horizontally

I resolve this issue by observing the contentsize of UITextView, when there is any change in the contentSize, update the contentOffset.

Add observer as follows:

[textview addObserver:self forKeyPath:@"contentSize" options:(NSKeyValueObservingOptionNew) context:NULL];

Handle the observer action as follows:

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
UITextView *txtview = object;
CGFloat topoffset = ([txtview bounds].size.height - [txtview contentSize].height * [txtview zoomScale])/2.0;
topoffset = ( topoffset < 0.0 ? 0.0 : topoffset );
txtview.contentOffset = (CGPoint){.x = 0, .y = -topoffset};
}

To make the textview text horizontally center, select the textview from .xib class and go to the library and in that set Alignment as center.

Enjoy. :)

Center text vertically in a UITextView

First add an observer for the contentSize key value of the UITextView when the view is loaded:

- (void) viewDidLoad {
[textField addObserver:self forKeyPath:@"contentSize" options:(NSKeyValueObservingOptionNew) context:NULL];
[super viewDidLoad];
}

Then add this method to adjust the contentOffset every time the contentSize value changes:

-(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};
}


Related Topics



Leave a reply



Submit