How to make the height of the cursor same with the

2019-03-01 06:11发布

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?

3条回答
神经病院院长
2楼-- · 2019-03-01 06:38

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

_uITextField.font = UIFont.systemFont(ofSize: 10)
查看更多
何必那么认真
3楼-- · 2019-03-01 06:40

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

查看更多
SAY GOODBYE
4楼-- · 2019-03-01 06:42

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;
}
查看更多
登录 后发表回答