以编程方式旋转UITextView

时间:2015-08-31 05:40:05

标签: swift cgaffinetransform

有一个奇怪的问题,如果你创建一个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))

结果: enter image description here

你知道吗?绝大多数都输了!但是如果你remove myTextView.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_2))并通过一个按钮运行它就可以了:

enter image description here

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

2 个答案:

答案 0 :(得分:2)

要解决此问题,请尝试将您的UITextView设置为另一个简单的UIView,然后轮转此UIView

它应该有所帮助,因为这样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)
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))
}

有点脏,但应该工作,用户可能会看到一个小剪辑,但大多数情况下他甚至都不应该看到它。

答案 1 :(得分:0)

请再试一次

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))