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
How Does Xcode Find Implicit Target Dependencies
How to Add Image in Uitableviewrowaction
Rotation Behaving Differently on iOS6
Decimal to Fraction Conversion in Swift
Segue in Skscene to Uiviewcontroller
Save Image Data to SQLite Database in Iphone
Could Not Load the "" Image Referenced from a Nib in the Bundle with Identifier
Instantiate View Controller from Storyboard VS. Creating New Instance
How to Resize an Image or Done as a Nsattributedstring Nstextattachment (Or Set Its Initital Size)
Swiftui: What Is @Appstorage Property Wrapper
Decode Base64Url to Base64 -- Swift
Keyboard and Cursor Show, But I Can't Type Inside Uitextfields and Uitextviews
Pixel Array to Uiimage in Swift
Add a Running Countup Display Timer to an iOS App, Like the Clock Stopwatch
Expand Uilabel Inside Uitableview with "More" Button Like Instagram
Xcode - Mpnowplayinginfocenter Info Is Not Displayed on iOS 8