How to make the height of the cursor same with the height of text in UITextField?
Asked Answered
M

4

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?

Mandeville answered 2/3, 2017 at 4:22 Comment(1)
Your problem may be same as this #20208461Fundament
B
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

Bronchopneumonia answered 2/3, 2017 at 4:38 Comment(4)
Thank you very much! The cursor's height seems to be same with the height of the textField. So I set a constraint on the textfield's height and increase the font size.Mandeville
@Bronchopneumonia i have some problem in another question if you are free so please help meGazehound
@john - sure, .Bronchopneumonia
@Bronchopneumonia check this question #41425311Gazehound
C
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;
}
Crosspollination answered 30/10, 2017 at 16:56 Comment(2)
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 & SwiftBigname
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 methodFlorey
F
0

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

_uITextField.font = UIFont.systemFont(ofSize: 10)
Flitch answered 2/3, 2017 at 4:31 Comment(0)
B
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

Bronchopneumonia answered 2/3, 2017 at 4:38 Comment(4)
Thank you very much! The cursor's height seems to be same with the height of the textField. So I set a constraint on the textfield's height and increase the font size.Mandeville
@Bronchopneumonia i have some problem in another question if you are free so please help meGazehound
@john - sure, .Bronchopneumonia
@Bronchopneumonia check this question #41425311Gazehound
F
-1

in 2021 we have properties

TextFormField(
cursorHeight: 13,
cursorColor: Colors.black)
Ferd answered 2/6, 2021 at 8:42 Comment(1)
this is not UIKit / iOSL

© 2022 - 2024 — McMap. All rights reserved.