0

I've tried many of the solutions in the popular question (Center text vertically in a UITextView)

However, when I try to add the code (my projecr is Obj-C), (from s.ka's answer):

- (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;
    CGFloat topCorrect = ([tv bounds].size.height - [tv     contentSize].height * [tv zoomScale])/2.0;
    topCorrect = ( topCorrect < 0.0 ? 0.0 : topCorrect );
    [tv setContentInset:UIEdgeInsetsMake(topCorrect,0,0,0)];
}

I get the following error: "use of undeclared identifier 'observeValueforKeyPath' "

I'm sure it's just a stupid error I'm making, but I can't figure it out.

On the side: It appears this is the best answer for centering text iOS 7-9. If it won't work on iOS 7-9 (I only have iOS 9 devices to test with), let me know.

I'm running the latest version of XCode.

Update: I was able to get the error to go away (thanks, Iman) but now it just doesn't do anything. Text shows, but it's not centered. If it helps, my textview is named quote_text, if that means I should revise the code. Neither solution works. I'm at a loss.

Thanks!
Branch

Community
  • 1
  • 1
Branch
  • 387
  • 5
  • 16

2 Answers2

0

UPDATE

i think because you are not using Autolayout, Anyhow use this code in your viewDidLoad method

CGFloat topCorrect = ([quote_text bounds].size.height - [quote_text contentSize].height * [quote_text zoomScale])/2.0;
topCorrect = ( topCorrect < 0.0 ? 0.0 : topCorrect );
[quote_text setContentInset:UIEdgeInsetsMake(topCorrect,0,0,0)];
Iman
  • 1,097
  • 2
  • 11
  • 19
  • I'm not sure how this fixes my main issue? – Branch Apr 09 '16 at 14:34
  • @Branch what i got from your question, is that you want to center your text vertically inside the textview. i made an update. But can you elaborate more of what is the problem you are exactly facing ? – Iman Apr 09 '16 at 14:38
  • So that will work in place of all of the OP code? Where in my existing code do I add your code? @Iman – Branch Apr 09 '16 at 14:42
  • @Branch place it in viewdidload and remove the rest of the code.(if you want to center your text vertically inside the textview) – Iman Apr 09 '16 at 14:44
  • Now I get '"use of undeclared identifier 'stringHeight' @Iman. I don't care which of the two codes I use, but I just need one of them to be error free. – Branch Apr 09 '16 at 14:49
  • When I implanted that, it broke the whole textview (nothing showed). also, 'sizeWithFont:constrainedToSize:' is depreciated as of iOS 7, according to XCode. @Iman – Branch Apr 09 '16 at 15:02
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/108709/discussion-between-iman-and-branch). @Branch – Iman Apr 09 '16 at 15:03
  • @Branch its deprecated, but it should be working. let me know if you want me to send you a sample project for it – Iman Apr 09 '16 at 15:06
  • I just get a blank textview. Many other people in the link in OP said the code in OP worked... why do I get an error with that? I think I may need the observer since my textview is NOT static @Iman – Branch Apr 09 '16 at 15:16
  • @Branch no i dont know the other solution, i made an editing to the original answer, i believe it will work with you but it will need some enhancements. – Iman Apr 09 '16 at 15:35
  • Ok. to be honest, I'm not super-techy so I can't just "make some enhancements to the code". I'll see if there's anyone else who can maybe help with the OP code. – Branch Apr 09 '16 at 15:36
  • 1
    This has nothing to do with the OPs question. The issue is a 'use of undeclared identifier' error. – Bamsworld Apr 09 '16 at 15:53
  • @Branch it looks like you have placed "-(void)observeValueForKeyPath:" method inside another method. copy and paste all of your class here if you couldnt find a solution. – Iman Apr 09 '16 at 16:02
  • Still does nothing :( i think i do use autolayout. idk why this won't work! @Iman – Branch Apr 09 '16 at 17:13
0
     [self.yourTextView 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;
           if (topCorrect < 0) {
                 topCorrect = 0.0;
         }
     tv.contentOffset = (CGPoint) { .x = 0, .y = -topCorrect };



Akash KR
  • 778
  • 3
  • 11