UITextView text not starting from top

Nitish picture Nitish · May 29, 2012 · Viewed 36.8k times · Source

I have a UITextView. I want its text to start from top but I it is not coming from top. Please have a look at my code :

UITextView *myTextView = [[UITextView alloc] init];
myTextView.frame = rect;
myTextView.editable = NO;
myTextView.font = [UIFont fontWithName:@"Helvetica" size:MAIN_FONT_SIZE];
myTextView.backgroundColor = [UIColor clearColor];
myTextView.text = sourceNode.label;
myTextView.dataDetectorTypes = UIDataDetectorTypeAll;
[cell.contentView addSubview:myTextView];
[myTextView sizeToFit];
[self alignTextToTopTextView:myTextView]; 

alignTextToTopTextView:

-(void)alignTextToTopTextView :(UITextView*)textView{

    CGRect frame = textView.frame;
    frame.size.height = textView.contentSize.height;
    textView.frame = frame;
} 

Please see the below screenshot.

UITextView is on the right side.

enter image description here

Answer

Damitha Raveendra picture Damitha Raveendra · May 29, 2012

Set the Content Inset like this in your UITextView

youtextView.contentInset = UIEdgeInsetsMake(-7.0,0.0,0,0.0);

Adjust the Top value the way you want. this shoud fix your problem.

EDIT:

If you're having issues with iOS7 or above, try using...

[yourTextView setContentOffset: CGPointMake(x,y) animated:BOOL];