以编程方式旋转 UITextView

Rotating UITextView programmatically

有一个奇怪的问题,如果你创建一个UITextView,然后在创建后立即旋转它,一些线条或字符将不可见!试试这个:

myTextView.font = UIFont.boldSystemFontOfSize(20)
myTextView.text = "Hello world wht the hell\nhello mrs lorem ipum!"
let maxsize = CGSizeMake(700, 700)
let frame = myTextView.sizeThatFits(maxsize)
myTextView.frame = CGRectMake(200, 200, frame.width, frame.height)     
view.addSubview(myTextView)
myTextView.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_2))

结果:

看到了吗?大部分都丢失了!但是,如果您通过按钮 remove myTextView.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_2)) 和 运行 就可以了:

您可以在git上查看代码:https://github.com/maysamsh/rotateTextView

作为解决方法,请尝试将 UITextView 设置为另一个简单的 UIView,然后旋转此 UIView

它应该有所帮助,因为这样,UITextView 应该像它没有旋转一样做出反应(因此您可以将其设置为超级视图的插入约束)

那你只需要关心superview的形式就可以了

其他解决方案

myTextView.font = UIFont.boldSystemFontOfSize(20)
myTextView.text = "Hello world wht the hell\nhello mrs lorem ipum!"
let maxsize = CGSizeMake(700, 700)
let frame = myTextView.sizeThatFits(maxsize)
myTextView.frame = CGRectMake(200, 200, frame.width, frame.height)     
view.addSubview(myTextView)
let delayTime = dispatch_time(DISPATCH_TIME_NOW, Int64(0.1 * Double(NSEC_PER_SEC)))
dispatch_after(delayTime, dispatch_get_main_queue()) {  
    myTextView.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_2))
}

有点脏,但应该可以用,用户可能会看到一个小剪裁,但大多数时候他甚至不应该看到它。

请再试一次

var myTextView = UITextView() as UITextView
    myTextView.font = UIFont.boldSystemFontOfSize(20)
    myTextView.text = "Hello world wht the hell\nhello mrs lorem ipum!"
    let maxsize = CGSizeMake(700, 700)
    let frame = myTextView.sizeThatFits(maxsize)

    myTextView.frame = CGRectMake(200, 200, frame.width, frame.height)
    self.view.addSubview(myTextView)


    let newframe = myTextView.sizeThatFits(frame)
    let ration = newframe.width/newframe.height
    myTextView.frame = CGRectMake(200, 200, ration*newframe.height, newframe.width)
    myTextView.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_2))