2

i am trying to make a layout, where i don't want to rotate my button, but want to rotate only my text of that button.

So is there any way to do that.?

Niki
  • 1,566
  • 1
  • 19
  • 36
  • You can access button.titleLabel and do something with it. – Phyber Aug 02 '17 at 09:09
  • Yeah i already tried that.. but i just want to rotate the text of button. so that i dont need to take extra label for all my views.. cz there are bunch of them.. – Niki Aug 02 '17 at 09:09

2 Answers2

3

Here is the original label:

Rotate 90 degrees clockwise:

 yourLabelName.transform = CGAffineTransform(rotationAngle: CGFloat.pi / 2)

Rotate 180 degrees:

yourLabelName.transform = CGAffineTransform(rotationAngle: CGFloat.pi)

Rotate 90 degrees counterclockwise:

 yourLabelName.transform = CGAffineTransform(rotationAngle: -CGFloat.pi / 2)

There are 2π radians in a full circle (360 degrees).

Swift includes the useful constant CGFloat.pi.

CGFloat.pi = π = 180 degrees

CGFloat.pi / 2 = π/2 = 90 degrees

Anurag Mishra
  • 109
  • 1
  • 9
  • You can mention [source](https://stackoverflow.com/questions/28717634/swift-how-can-you-rotate-text-for-uibutton-and-uilabel) of this answer – Phyber Aug 02 '17 at 09:42
2

You can access button.titleLabel

example:

button.titleLabel?.transform = CGAffineTransform(rotationAngle: CGFloat.pi / 2)

opposite angle

button.titleLabel?.transform = CGAffineTransform(rotationAngle: -CGFloat.pi / 2)
Phyber
  • 1,368
  • 11
  • 25
  • But angle is not right.. i want the opposite of it.. if you have any idea regarding that.. – Niki Aug 02 '17 at 09:18
  • My text is clipping when i have more then 2 letters.. Do you have any idea how to solve it? – Niki Aug 02 '17 at 12:54
  • @ChaiBiskut Let us continue in [chat](https://chat.stackoverflow.com/rooms/150827/label-cliping-problem) – Phyber Aug 02 '17 at 12:56