我尝试创建一个 TextView ,旋转它并在其中显示一些文本。我可以创建并旋转 UITextView,但文本在 UITextView 中的行结束之前中断到下一行。
let textView = UITextView(frame: CGRect(x: 0, y: 0, width: 300, height: 200))
textView.transform = CGAffineTransform(rotationAngle: .pi / 2)
textView.text = "This is some long sample text to show the issue I am facing. Notice that the text is broken onto the next line well before the end of the UITextView."
textView.backgroundColor = .red
textView.center = view.center
view.addSubview(textView)
问题:
<小时/>确定问题
但是,如果我不旋转 TextView (通过注释掉上面的以下行),文本不会在 UITextView 结束之前中断到下一行。所以,这个问题似乎是由旋转图像引起的,但我不明白为什么或如何解决这个问题。
textView.transform = CGAffineTransform(rotationAngle: .pi / 2)
<小时/>
为什么只有在旋转时文本才会中断到 UITextView 中间的下一行,如何解决此问题?
最佳答案
您可以通过执行以下操作来防止此问题:
- 创建一个新 View (myView)
- 创建 TextView (myTextView)
- 将 myTextView 添加到 myView
- 旋转我的 View
// create a new view
let myView = UITextView(frame:CGRect(x: 0, y: 0, width: view.frame.height-200, height: view.frame.width-200)) // this frame will have 100px margin on the top & bottom as well as on the left & right once rotated
// create a new textview
let myTextView = UITextView(frame:CGRect(x: 0, y: 0, width: scrollOutlet.frame.height-messageMarginTop*2, height: scrollOutlet.frame.width-messageMarginLeft*2))
myTextView.text = "This is some long sample text to show the resolved issue. Notice that the text will no longer be broken onto the next line before the end of the UITextView.. Enjoy!"
// add the new text view to the myView
myView.addSubview(myTextView)
// rotate the myView
myView.transform = CGAffineTransform(rotationAngle: CGFloat.pi/2)
// center myView in ViewController's view
myView.center.x = view.center.x
myView.center.y = view.center.y
self.view.addSubview(myView)
关于ios - 旋转时,文本在 UITextView 行尾之前断到下一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48125529/