7

I have UITextField with too long cursor(the cursor for "123123" in the following image)

enter image description here

How to make the height of the cursor same with the height of text?

Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
jybsuper
  • 179
  • 2
  • 10
  • Your problem may be same as this http://stackoverflow.com/questions/20207961/ios-uitextview-linespacing-make-cursor-height-not-same – menq Mar 02 '17 at 04:27

4 Answers4

17

I accidentally stumbled across this question and even though it is a little old I feel compelled to answer it since the accepted answer actually isn't correct.

You can indeed change the height (or width) of the cursor. Just subclass the UITextField and override this method:

- (CGRect)caretRectForPosition:(UITextPosition *)position {
    CGRect rect = [super caretRectForPosition:position];
    rect.size.height = 42;
    return rect;
}
pajevic
  • 4,607
  • 4
  • 39
  • 73
  • hi, i tried your answer and it worked so +1 for you, but when i changed the keyboard (from English(indian) to Japanes(kana) ) , the caret moves up when i enter a single character. **Xcode 10.1 & Swift** – Vatsal Shukla Dec 07 '18 at 04:57
  • Thank you, it is a great answer! It should be selected as correct. It is far more flexible and reliable solution. in my case, I also needed to change cursor's position according to different conditions. I could achieve it through this method – Artemis Shlesberg May 17 '21 at 18:12
0

Suppose your UITextField name is uiTextField. To set font size add the code below

_uITextField.font = UIFont.systemFont(ofSize: 10)
Dilip Jangid
  • 754
  • 1
  • 10
  • 20
0

we can't change the cursor height , but we can do some trick , select your textfield and change your textfield border style as UITextBorderStyleNone

you get the out put as

enter image description here

there after increase the font size of your textfield whatever you want , then you get the output as

enter image description here

Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
0

in 2021 we have properties

TextFormField(
cursorHeight: 13,
cursorColor: Colors.black)
awaik
  • 10,143
  • 2
  • 44
  • 50