Rotating UITextView programmatically

女生的网名这么多〃 提交于 2019-12-24 04:18:08

问题


There is a weird problem, if you create a UITextView and rotate it right after creating it, some lines or characters will not be visible! Try this:

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

Result:

You see? most of is lost! But if you remove myTextView.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_2)) and run it through a button it's fine:

You can view the code on git: https://github.com/maysamsh/rotateTextView


回答1:


As a workaround, try to set your UITextView in another simple UIView, and then rotate this UIView.

It should help because this way, UITextView should react like it's not rotated (so you can set it with inset constraints to superview)

Then you just have to care about the form of the superview.

Other solution

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

A bit dirty, but should work, the user might see a small clipping, but mostly he shouldn't even see it.




回答2:


Please try with this again

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


来源:https://stackoverflow.com/questions/32304605/rotating-uitextview-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!