Skip to content

Instantly share code, notes, and snippets.

@marshluca
Created July 21, 2011 09:54
Show Gist options
  • Save marshluca/1096889 to your computer and use it in GitHub Desktop.
Save marshluca/1096889 to your computer and use it in GitHub Desktop.
自定义UIButton: 左1/4图标,右3/4文字
#import <Foundation/Foundation.h>
@interface MButton : UIButton {
}
@end
#import "MButton.h"
@implementation MButton
- (CGRect)imageRectForContentRect:(CGRect)contentRect
{
return CGRectMake(0, 0, self.frame.size.width/4, self.frame.size.height);
}
- (CGRect)titleRectForContentRect:(CGRect)contentRect
{
return CGRectMake(self.frame.size.width/4, 0, self.frame.size.width*3/4, self.frame.size.height);
}
- (CGRect)backgroundRectForBounds:(CGRect)bounds
{
return self.frame;
}
@end
{
UIViewController *controller = [[UIViewController alloc] init];
MButton *button = [[MButton alloc] initWithFrame:CGRectMake(50, 100, 200, 100)];
[button setImage:[UIImage imageNamed:@"arrow.png"] forState:UIControlStateNormal];
[button setTitle:@"button" forState:UIControlStateNormal];
button.titleLabel.textAlignment = UITextAlignmentCenter;
[controller.view addSubview:button];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment