简体   繁体   English

如何滚动到UITextField中的最后一个字符?

[英]How to scroll to last character in UITextField?

I tried to rebuild the UITextField from Apple's "Phone" app. 我试图从Apple的“Phone”应用程序重建UITextField

I built a numpad where each button triggers a certain value (0-9, #, *) and appends it to the text field's text. 我构建了一个小键盘,每个按钮触发一个特定值(0-9,#,*)并将其附加到文本字段的文本中。

The textAlignment property is set to center . textAlignment属性设置为center However, when inserting numbers, the text field does not scroll or move to the last character. 但是,插入数字时,文本字段不会滚动或移动到最后一个字符。 F.ex., if there're more than 10 numbers and I'm adding the eleventh number, that one won't be displayed because it's off the screen. F.ex.,如果有超过10个数字并且我正在添加第11个数字,则不会显示该数字,因为它不在屏幕上。

Apple handles this issue by first shrinking the font size and then scrolling to the cursor position. Apple通过首先缩小字体大小然后滚动到光标位置来处理此问题。

Is there a simple trick to achieve the same result? 是否有一个简单的技巧来实现相同的结果?

I attached two images. 我附上了两张照片。

在此输入图像描述

在此输入图像描述

在此输入图像描述

Use this example. 使用此示例。 Sets cursor and scrolls to the end of UITextField . 将光标和滚动设置到UITextField的末尾。 Because of you do operations with UI, you have to do it in main thread 由于您使用UI进行操作,因此必须在主线程中执行此操作

DispatchQueue.main.async {[weak self] in
     self?.textField.becomeFirstResponder() // to focuse on this UITextField
    let newPosition = self?.textField.endOfDocument  ?? UITextPosition()
    self?.textField.selectedTextRange = self?.textField.textRange(from: newPosition, to: newPosition)
}

You need to set the minimum font size for the text view in storyboard and check the adjust to fit check box below it. 您需要在storyboard中设置文本视图的最小字体大小,并选中它下面的adjust to fit复选框。

在此输入图像描述

why using a textfield instead of using a uilabel? 为什么使用文本字段而不是使用uilabel?

This can easily fixed with 这很容易解决

label.adjustsFontSizeToFitWidth = true
label.textAlignment = .center
label.setFillSuperview(withPadding: UIEdgeInsets(top: 0, left: whatYouNeed, bottom: 0, right: whatYouNeed))

and add extension to UIView 并为UIView添加扩展

extension UIView {
    func setFillSuperview(withPadding padding: UIEdgeInsets = .zero) {
        translatesAutoresizingMaskIntoConstraints = false
        if let superviewLeadingAnchor = superview?.leadingAnchor {
            leadingAnchor.constraint(equalTo: superviewLeadingAnchor, constant: padding.left).isActive = true
        }

        if let superviewTrailingAnchor = superview?.trailingAnchor {
            trailingAnchor.constraint(equalTo: superviewTrailingAnchor, constant: -padding.right).isActive = true
        }

        if let superviewBottomAnchor = superview?.bottomAnchor {
            bottomAnchor.constraint(equalTo: superviewBottomAnchor, constant: -padding.bottom).isActive = true
        }

        if let superviewTopAnchor = superview?.topAnchor {
            topAnchor.constraint(equalTo: superviewTopAnchor, constant: padding.top).isActive = true
        }
    }

Use this code snippet in your viewcontroller to shrink the text as it increases. 在viewcontroller中使用此代码段可在文本增加时缩小文本。

func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    myTextField.minimumFontSize = 4.0
    myTextField.setNeedsLayout()
    myTextField.layoutIfNeeded()
}

Although I don't like answering my own questions, there seems to be no way around since I've just found a solution. 虽然我不喜欢回答我自己的问题,但由于我刚刚找到解决方案,似乎没有办法解决。

Alastar was partly correct; Alastar部分正确; instead of using a UITextField , it's way simpler to use a UILabel . 而不是使用UITextField ,使用UILabel更简单。

However, the single line that did the trick for me was to set the line break mode to truncate the head: 但是,对我来说这个技巧的单行是设置换行模式来截断头部:

label.lineBreakMode = .byTruncatingHead

This way, 这条路,

The line is displayed so that the end fits in the container and the missing text at the beginning of the line is indicated by an ellipsis glyph. 显示该行以使末尾适合容器,并且行开头的缺失文本由省略号字形表示。 Although this mode works for multiline text, it is more often used for single line text. 虽然此模式适用于多行文本,但它更常用于单行文本。

This is the exact behaviour which Apple used in their "Phone" app (the dial screen). 这是Apple在其“电话”应用程序(拨号屏幕)中使用的确切行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM