6


i'm working on a project(Swift4,Xcode 9.2) which has a feature to get text input and the blinking bar/line should be of big size (it should be Square instead of bar/line), so i placed a UITextField for Text but i don't understand how to change the size of that blinking line/bar.

So, is it possible to change the size of line/bar? and if Yes then how to do it? i know how to change the color of that line/bar but this is something different.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Vatsal Shukla
  • 1,274
  • 12
  • 25
  • If you do this, your app may get rejected during the review. Look at this SO question. https://stackoverflow.com/questions/12747664/uitextfield-custom-cursor-image – Mani Apr 23 '18 at 05:53
  • You may be right, but i've done this Color Changing thing earlier and my app is still living on AppStore. – Vatsal Shukla Apr 23 '18 at 05:59
  • Related: [How to make the height of the cursor same with the height of text in UITextField?](https://stackoverflow.com/questions/42546820/how-to-make-the-height-of-the-cursor-same-with-the-height-of-text-in-uitextfield) and [UITextView lineSpacing make cursor height not same](https://stackoverflow.com/questions/20207961/ios-uitextview-linespacing-make-cursor-height-not-same) – Cœur Sep 25 '18 at 09:21

3 Answers3

7

You can change the size by overriding the frame method for cursor as follows,

class CustomTextField: UITextField {

    override func caretRect(for position: UITextPosition) -> CGRect {
        var rect = super.caretRect(for: position)
        let size = CGSize(width: 10, height: 50)
        // Calculating center y
        let y = rect.origin.y - (size.height - rect.size.height)/2
        rect = CGRect(origin: CGPoint(x: rect.origin.x, y: y), size: size)
        return rect
    }
}

Set CustomTextField class in xib/storyboard identity inspector for that textField.

Kamran
  • 14,987
  • 4
  • 33
  • 51
  • Hi, can you please help me on this **https://stackoverflow.com/questions/53665841/weird-behaviour-of-caret-cursor-in-uitextfield** – Vatsal Shukla Dec 19 '18 at 06:50
1

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

Check the below link which is already given answer

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

Cœur
  • 37,241
  • 25
  • 195
  • 267
Naresh
  • 13
  • 5
  • Hi, can you please help me on this **https://stackoverflow.com/questions/53665841/weird-behaviour-of-caret-cursor-in-uitextfield** – Vatsal Shukla Dec 19 '18 at 06:51
1

There are some unnecessary lines of codes, so this is the revised:

class CustomTextField: UITextField {
    override func caretRect(for position: UITextPosition) -> CGRect {
        var rect = super.caretRect(for: position)
        rect = CGRect(x: rect.origin.x, y: .zero, width: 15, height: 30)
        return rect
    }
}
Elham
  • 19
  • 2