Reputation: 19303
When 2 or more lines are added to my UiTextView
it looks good but when the user enters just one line the text stays in the middle of the UiTextView
. How can I force it to stay on the top left side of the UiTextView
?
Upvotes: 1
Views: 1058
Reputation: 19303
I just found a much simpler solution. The next code will move just the text: [textView setContentInset:UIEdgeInsetsMake(5, 0, 5,0)]
Upvotes: 1
Reputation: 23278
You can check this blog (iOS: Vertical aligning text in a UITextView) which explains how to do center and bottom vertical alignment. Use similar logic and set contentOffset y param as zero in your case to make it top aligned.
This is the code from the blog,
- (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};
}
Upvotes: 1