ios - UITextField 中 Caret/Cursor 的奇怪行为

标签 ios swift uitextfield

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

到目前为止我尝试了什么:
我这样做了,问题解决了。

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 中移到上侧。

请帮帮我

我浏览了这些链接,
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

这里是 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)

//BP文本字段

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
}

关于ios - UITextField 中 Caret/Cursor 的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53665841/

相关文章:

iPhone:安全容器

iphone - 如何将用户发送到 App Store 以安装应用程序,或将用户发送到带有网站变量的应用程序?

c# - 异常 : Application initializing document picker is missing the iCloud entitlement. 是否设置了 com.apple.developer.icloud-container-identifiers?

ios - 快速 xcode 错误 : "instance member ' name' cannot be used on type 'clothing_category'

iphone - 将 3 个 UItextfield 中的数据分配到 3 个不同的字符串中

ios - 自定义单元格中的 UITextField 文本在滚动 UITableView 时发生变化

ios - 响应 : unauthorized (401) 的 AFNetworking 问题

ios - 为什么文本标签是空的,而调试器说它不是? swift 应用程序?

ios - 在 NSUserDefaults 中保存一个元组

iOS -Textfield 的 shouldChangeCharactersIn 范围正在禁用 .editingChanged 的​​目标方法