Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to set vertical align of uitextview from interface builder

I have a problem in my application design. I want to set vertical align middle of my UITextView. Please help me how to set vertical align of uitextview from interface builder.

like image 271
Shivomkara Chaturvedi Avatar asked Sep 14 '11 06:09

Shivomkara Chaturvedi


1 Answers

Got this to work by observing changes to the contentSize property and then setting contentOffset.

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

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    UITextView *myView = object;

    CGFloat offSet = ([myView bounds].size.height - [myView contentSize].height * [myView zoomScale])/2.0;
    offSet = offSet < 0.0 ? 0.0 : offSet;
    [myView setContentOffset:CGPointMake(0, -offSet) animated:NO]; 
}
like image 146
Zotter Avatar answered Oct 02 '22 22:10

Zotter