How to vertically center cursor in UITextView

I am trying to vertically center my text cursor in a UITextView.

// creating the inputText
[inputTextView removeFromSuperview];
inputTextView = [[UITextView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(searchIconsButton.frame) + 3 , 0, buttomView.frame.size.width * 0.78 , buttomView.frame.size.height *0.80)];
inputTextView.layer.borderColor = [UIColor lightGrayColor].CGColor;
inputTextView.layer.borderWidth = 0.6;
inputTextView.center = CGPointMake(inputTextView.center.x, buttomView.frame.size.height / 2);
inputTextView.autocorrectionType = UITextAutocorrectionTypeNo;
[inputTextView.layer setCornerRadius:6];
[inputTextView setTintColor:[UIColor blackColor]]; // set the cursor color to black
inputTextView.textAlignment = UIControlContentVerticalAlignmentCenter;

      

I am trying on the last line to make the UIControlContentVerticalAlignmentCenter but it still doesn't work.

You can see the cursor is hiding in the Textview.

Is there a way to solve this problem?

+3


source to share


2 answers


Use a property textContainerInset

UITextView

available in iOS 7.0 and later.

inputTextView.textContainerInset = UIEdgeInsetsMake(-2,0,0,0); // Move cursor up 2

      



Play with the values โ€‹โ€‹until they suit your needs.

+5


source


The solution above is perfect, but for anyone using swift 3, I translated to the following ad that it works great.

  override func editingRect(forBounds bounds: CGRect) -> CGRect {
        return bounds.insetBy(dx: 0, dy: 2);
    }

      



Overriding UITextField ofcouse

0


source







All Articles