Maysam
Maysam

Reputation: 7367

Rotating UITextView programmatically

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: enter image description here

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:

enter image description here

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

Upvotes: 4

Views: 1289

Answers (2)

Jamil
Jamil

Reputation: 2999

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

Upvotes: 0

Tancrede Chazallet
Tancrede Chazallet

Reputation: 7255

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.

Upvotes: 4

Related Questions