这是我的代码:
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()
}
}
这是结果:
根据空格的数量,它应该创建了一个空的换行符。然而,正如我们所看到的,情况并非如此。如果我在字符串的最后添加另一个字符,则会显示换行符(但带有我不想要的字符)。
如果需要,我如何显示一个空的换行符UITextView
?