This actually isn't too hard to do. You need to create two IBActions in your class, onTouchDown and onTouchUp. Create an IBOutlet for your button too. Then, go to Interface Builder and wire up the button to the IBOutlet and wire up the action for "Touch Down" to "onTouchDown" and the actions for "Touch Up Inside" and "Touch Up Outside" to "onTouchUp."
onTouchDown is where you'll set your highlighted font. onTouchUp is where you'll reset your button.
Your header file will end up looking something like this:
@interface TestViewController : UIViewController {
UIButton *testButton;
}
@property (nonatomic, retain) IBOutlet UIButton *testButton;
- (IBAction)onTouchDown;
- (IBAction)onTouchUp;
Now, inside the "onTouchDown" function here's what you're gonna do:
1) Store the current dimensions of the button
2) Change the font size
3) Tell the button to auto-resize (THIS IS THE KEY)
4) Center the button based on the old size
You should have a function that ends up looking something like this:
- (IBAction)onTouchDown:(id)sender
{
CGRect oldBtnRect = testButton.frame;
testButton.titleLabel.font = [UIFont systemFontOfSize:36];
[testButton sizeToFit];
testButton.frame = CGRectMake(testButton.frame.origin.x - ((testButton.frame.size.width - oldBtnRect.size.width)/2), testButton.frame.origin.y - ((testButton.frame.size.height - oldBtnRect.size.height)/2), testButton.frame.size.width, testButton.frame.size.height);
}
Note, the font size is 36.
In your touch up function, it'll look something like this:
- (IBAction)onTouchUp:(id)sender
{
CGRect oldBtnRect = testButton.frame;
testButton.titleLabel.font = [UIFont systemFontOfSize:15];
[testButton sizeToFit];
testButton.frame = CGRectMake(testButton.frame.origin.x - ((testButton.frame.size.width - oldBtnRect.size.width)/2), testButton.frame.origin.y - ((testButton.frame.size.height - oldBtnRect.size.height)/2), testButton.frame.size.width, testButton.frame.size.height);
}
The font size should be whatever your default font is. In this case, I made it 15.
This should get you a button that resizes from the center instead of just resizing.
Now, the code isn't intended to be perfect. It's just a general IDEA of what you're doing here. I think this is a great candidate for some code reuse by combining these into one function and passing in what size you want the text to be. I just didn't feel like doing it myself ;)