ios - 旋转时,文本在 UITextView 行尾之前断到下一行

标签 ios swift rotation uitextview cgaffinetransform

我尝试创建一个 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)

问题:

Issue

<小时/>

确定问题

但是,如果我不旋转 TextView (通过注释掉上面的以下行),文本不会在 UITextView 结束之前中断到下一行。所以,这个问题似乎是由旋转图像引起的,但我不明白为什么或如何解决这个问题。

textView.transform = CGAffineTransform(rotationAngle: .pi / 2)
<小时/>

为什么只有在旋转时文本才会中断到 UITextView 中间的下一行,如何解决此问题?

最佳答案

您可以通过执行以下操作来防止此问题:

  1. 创建一个新 View (myView)
  2. 创建 TextView (myTextView)
  3. 将 myTextView 添加到 myView
  4. 旋转我的 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/

相关文章:

swift - 多个网站的多个 Alamofire 请求的正确模型

swift - 通用数字参数

c++ - 使用opencv warpaffine时如何保持白色背景

CSS在使用变换矩阵旋转后定位矩形

ios - 无法接收 iOS 的 FCM 推送通知

ios - 如何在 iOS 9 中打开电话拨号器?

ios - 继续使用 didSelectRowAtIndexPath

ios - 从另一个 VC : Swift 向 JSQCollectionView 添加消息

CSS3 变换旋转与使用图像

ios - 继承自 MyClass : UIViewController causes error 'Cannot find interface declaration'