Haze
Haze

Reputation: 53

Having a portrait label in a landscape view

I've created a .xib with a landscape orientation UIView.

The problem I'm trying to solve is that I want a UILabel running vertically along the side with text reading from bottom of the view to the top, but I can't figure out how to do that. Is that possible?

Image to show what I mean

enter image description here

Upvotes: 0

Views: 155

Answers (2)

Charlie Price
Charlie Price

Reputation: 1376

You need to rotate and then translate it to put it where you want it. Rotation happens around the center of the label which is why you then need to translate it. If you laid out the text label with the upper left corner where you wanted it to end up (i.e. the displayed label looks like it rotates around the upper left corner point of the label), you'd use code something like:

- (void)viewDidLayoutSubviews {
    CGAffineTransform transform = CGAffineTransformMakeRotation(3 * M_PI_2);
    CGAffineTransform transform2 = CGAffineTransformConcat(transform, CGAffineTransformMakeTranslation(floor(-self.label.bounds.size.width / 2), floor(-self.label.bounds.size.width / 2)));
    self.label.transform = transform2;
}

You might need to adjust the translation values slightly to get what you want, but you definitely want them to be integers (which I've done with floor) so the label is crisp.

Upvotes: 2

pdrcabrod
pdrcabrod

Reputation: 1477

Of course you can. You need to do a transform.

yourlabel.transform = CGAffineTransformMakeRotation(-M_PI/2);

Upvotes: 2

Related Questions