导致 UITextView 在结尾处换行的额外空白被忽略 [英] Extra whitespace that causes a newline by UITextView at end is ignored

查看:38
本文介绍了导致 UITextView 在结尾处换行的额外空白被忽略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

import UIKit

class ViewController: UIViewController {
    init() {
        super.init(nibName: nil, bundle: nil)

        let textView = UITextView(frame: .zero)
        textView.isScrollEnabled = false
        textView.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(textView)

        NSLayoutConstraint.activate([
           textView.widthAnchor.constraint(equalToConstant: 150),
            textView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            textView.centerYAnchor.constraint(equalTo: view.centerYAnchor)
        ])

        // As we can see, LOT'S of whitespace at the end.
        textView.text = "Some Random Text That Has Whitespaces At The End                                                        "

        view.backgroundColor = .yellow
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError()
    }
}

结果如下:

根据空格的数量,它应该创建一个空的换行符.然而,正如我们所见,情况并非如此.如果我在字符串的最后添加另一个字符,将显示换行符(但带有我不想要的字符).

By the amount of spaces, it should have created an empty newline. However, as we can see this wasn't the case. If I add another character at the very end of the string, the newline will be shown (but with the character, which I do not want).

如果需要,如何在 UITextView 中显示一个空的换行符?

How can I show an empty newline if needed in an UITextView?

推荐答案

使用这个:

引用中有一些invisible 字符,iOS 不将它们算作whiteSpace

There is some invisible characters inside the quote that iOS is not count them as whiteSpace

这篇关于导致 UITextView 在结尾处换行的额外空白被忽略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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