旋转时,文本分为UITextView行前端的下一行

时间:2018-01-06 08:18:31

标签: ios swift rotation uitextview cgaffinetransform

我尝试创建一个文本视图,旋转它,并在其中显示一些文本。我可以创建和旋转UITextView,但文本在我的UITextView中的行尾之前突破到下一行。

    let textView = UITextView(frame:CGRect(x:0,y:0, width: 300, height: 200))
    textView.transform = CGAffineTransform(rotationAngle: CGFloat.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 = UIColor.red
    textView.center = self.view.center

    self.view.addSubview(textView)

问题:

Issue

识别问题

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

textView.transform = CGAffineTransform(rotationAngle: CGFloat.pi/2)

为什么文本只有在轮换时才会突破到UITextView中间的下一行?如何解决此问题?

1 个答案:

答案 0 :(得分:0)

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

  1. 创建新视图(myView)
  2. 创建文本视图(myTextView)
  3. 将myTextView添加到myView
  4. 旋转myView
  5. // 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)