UITextView does not start on first line in iOS

I added UITextView, and when it starts editing, it does not start on the first line instead of the second line. Does anyone have an idea? Thanks!

PS This problem continues, even I have not yet implemented any code.

+4
source share
5 answers

To enter / start text at the top UITextViewyou need to set contentInset, for example,

self.yourTextViewName.contentInset = UIEdgeInsetsMake(2.0,1.0,0,0.0); // set value as per your requirement.

Commonly contentInsetused to set the distance between the insertion between the content view and the conclusion UITextView.

+2
source

UINavigationController TextView .

ViewDidLoad

  [self setEdgesForExtendedLayout:UIRectEdgeNone];
+3

viewDidLoad

-:

self.automaticallyAdjustsScrollViewInsets = NO

:

self.automaticallyAdjustsScrollViewInsets = false
+2
source

You can do the same with binding. See screenshot below: enter image description here

Inside the control -> alignment, select the first option as above: -

enter image description here

And then this is the conclusion above: -

0
source

You can do it this way also

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

All Articles