SURESH SANKE
SURESH SANKE

Reputation: 1663

Vertical Alignment for UITextView

I want to align vertically some text in UITextView.When I added text.It shows from top..But I need to show middle from top.Is it possible to achieve this.

Upvotes: 4

Views: 9605

Answers (2)

Naveed Rafi
Naveed Rafi

Reputation: 2633

- (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: 0

Maulik
Maulik

Reputation: 19418

You can be able to use the contentOffset property of the UITextView class to accomplish this.

See this Blog. I hope it helps you.

Upvotes: 4

Related Questions