如何更改特定 UITextField 的闪烁条/线的大小?

发布于 2024-08-26 19:51:58 字数 236 浏览 11 评论 0原文


我正在开发一个项目(Swift4,Xcode 9.2),该项目具有获取文本输入的功能,并且闪烁的条/线应该是大尺寸(它应该是正方形而不是条/线),所以我为文本放置了一个 UITextField,但我不明白如何更改闪烁的线/条的大小。

那么,是否可以更改线条/条形的大小?如果是的话该怎么做? 我知道如何更改该线/条的颜色,但这是不同的。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

盛装女皇 2024-09-02 19:51:59

您可以通过重写光标的frame方法来更改大小,如下所示,

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
    }
}

中设置CustomTextField类xib/storyboardtextField 的身份检查器。

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.

不弃不离 2024-09-02 19:51:59

我们无法更改光标高度,但我们可以做一些技巧,选择文本字段并将文本字段边框样式更改为 UITextBorderStyleNone

检查下面的链接,

增加后 已经给出了答案无论你想要什么,你的文本字段的字体大小,然后你得到的输出为

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

千笙结 2024-09-02 19:51:59

有一些不必要的代码行,所以修改如下:

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
    }
}

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
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文