UITextField 中插入符号/光标的奇怪行为 [英] Weird behaviour of Caret/Cursor in UITextField

查看:17
本文介绍了UITextField 中插入符号/光标的奇怪行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(Xcode10.1、Swift、iOS 11&12)
问题:如何增加 UITextField 中插入符号/光标的高度

到目前为止我尝试过的:
我已经这样做了,问题解决了.

What i've tried so far:
i've done this, and problem solved.

class BPTextField: UITextField {
    override func caretRect(for position: UITextPosition) -> CGRect {
        //CGRect rect = [super caretRectForPosition:position];
        var rect = super.caretRect(for: position)
        let h = self.frame.height - 5
        let y_ = (self.frame.height - h)/2
        rect.origin.y = y_
        rect.size.height = h;
        return rect;
    }
}

但是,当我更改键盘语言(从英语-印度语到日语-假名)并输入单个字符时,插入符号/光标在 TextField 中向上移动.

请帮帮我

我已经浏览了这些链接,
如何使光标高度与 UITextField 中的文本高度一致?
UITextView lineSpacing 使光标高度不同

i've gone through these links,
How to make the height of the cursor same with the height of text in UITextField?
UITextView lineSpacing make cursor height not same

编辑我正在使用 YYText 库,我将此 BPTextField 作为 NSAttributedStringAttachment 插入到 YYLabel 中作为 NSAttributedStringAttachment

EDIT i'm using YYText library, i'm inserting this BPTextField into YYLabel as a NSAttributedStringAttachment

这里是 NSAttributedString,

here is NSAttributedString,

let main = NSMutableAttributedString(string: text_)
        main.yy_color = .darkGray
        main.yy_font = questionFont
        main.yy_lineSpacing = 5.0
        main.yy_lineBreakMode = .byWordWrapping

//附件

let attachment = NSMutableAttributedString.yy_attachmentString(withContent: self.tf, contentMode: UIView.ContentMode.center, attachmentSize: self.tf.frame.size, alignTo: questionFont, alignment: YYTextVerticalAlignment.bottom)
        attachment.yy_baselineOffset = -5.0
        root.append(attachment)

//BPTextField

//BPTextField

self.tf = BPTextField(frame: CGRect(origin: CGPoint.init(x: 0, y: 13), size: CGSize(width: tfWidth.width + 7.5, height: tfWidth.height + 13.0)))
        self.tf.borderStyle = UITextField.BorderStyle.none
        self.tf.tag = 0
        self.tf.font = questionFont
        self.tf.autocorrectionType = UITextAutocorrectionType.no
        self.tf.textColor = .darkGray
        self.tf.setRoundedCorners(corners: UIRectCorner.allCorners, withRadius: 4.0)
//        self.tf.setLeftPaddingPoints(5.0)
//        self.tf.setRightPaddingPoints(0.0)
        self.tf.backgroundColor = #colorLiteral(red: 0.9058823529, green: 0.9529411765, blue: 1, alpha: 1)
        self.tf.tintColor = #colorLiteral(red: 0.2196078431, green: 0.6156862745, blue: 1, alpha: 1)
        self.tf.returnKeyType = .done
        self.tf.delegate = self

推荐答案

你可以计算origin y如下,

override func caretRect(for position: UITextPosition) -> CGRect {
    var rect = super.caretRect(for: position)
    let size = CGSize(width: rect.size.width, height: self.frame.height - 5)
    // 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
}

这篇关于UITextField 中插入符号/光标的奇怪行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆