glaszig / SZTextView

A drop-in UITextView replacement which gives you: a placeholder.
MIT License
683 stars 102 forks source link

Vertical Alignment for text #39

Closed pranavss11 closed 8 years ago

pranavss11 commented 9 years ago

I am trying to center my text vertically using:

http://stackoverflow.com/questions/12591192/center-text-vertically-in-a-uitextview

but it seems like it doesn't work. Any idea how I can accomplish this?

glaszig commented 9 years ago

register the observer on [textView valueForKey:@"_placeholderTextView"] and not the SZTextView instance itself?

pranavss11 commented 9 years ago
 [[self.textView valueForKey:@"_placeholderTextView"] addObserver:self forKeyPath:@"contentSize" options:(NSKeyValueObservingOptionNew) context:NULL];

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

Still doesn't work. Strange. Text is still aligned to at top.

glaszig commented 9 years ago

does the observer even get called? does the contentSize even change? Either way, UIKit is not really KVO-compliant, afaik.