Manipulating UIButton contents
On iOS, the
UIButton class presents a title, image, or both as a clickable element. By default, the text is to the right of the image, but you can adjust the position with the
titleEdgeInsets properties. So far, so good.
The icons for my buttons are all the same size and the image is centered by default, so I am able to use constant insets for the image to move it to the upper half of the button. The text is a differnet story, though! Depending on the language, the text might split to more lines in its default location than it will need in the final spot. A simple static inset won’t work for me. If the insets need to be dynamic, I need to know what those insets are inset from. The
titleLabel has a frame, which seems like the right thing to us, but it turns out not to be stable. After rotating the device, the text was all messed up.
After much gnashing of teeth, I discovered that the
titleLabel.frame isn’t a true frame at all. It’s the frame with the insets already applied! Solution: do some math to un-apply the insets or set them to zero before checking the frame.