user1494994
user1494994

Reputation:

UITextView not starting from first line in iOS

I've added a UITextView and when it starts editing, it doesn't start at the first line instead from the second line. Anyone have any idea? Thanks!

P.S. This problem continues even I haven't yet implement any code to it.

Upvotes: 4

Views: 5766

Answers (5)

masouk
masouk

Reputation: 576

in the viewDidLoad method add

object-c:

self.automaticallyAdjustsScrollViewInsets = NO

swift:

self.automaticallyAdjustsScrollViewInsets = false

Upvotes: 3

chandan
chandan

Reputation: 2453

You can do this by 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};
}

Upvotes: 0

Hussain Shabbir
Hussain Shabbir

Reputation: 14995

You can do the same through binding as well. See the below screen shot:- enter image description here

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

enter image description here

And then this is the output above:-

Upvotes: 0

iPatel
iPatel

Reputation: 47049

For enter/start text at top of the UITextView you need to set contentInset such like

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

Generally contentInset is use for set the distance of the inset between the content view and the enclosing UITextView.

Upvotes: 2

TreeTree
TreeTree

Reputation: 557

The textView is being pushed down by the extended layout property when you added in the UINavigationController.

Try putting in your ViewDidLoad method

  [self setEdgesForExtendedLayout:UIRectEdgeNone];

Upvotes: 3

Related Questions