2011-05-23 21 views

回答

6

您可以创建一个UIButton类的类别。下面是你准系统设置:

在.h文件:

@interface UIButton (YourCategoryName) 
    - (void)setupButton:(NSString *)title image:(UIImage *)image description:(NSString *) description; 
@end 

在.m文件:

@implementation的UIButton(YourCategoryName)

- void)setupButton:(NSString *)title image:(UIImage *)image description:(NSString *) description { 
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 0, 200, 50)]]; 
    [self addSubview:titleLabel]; 
    [titleLabel release]; 

    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:image]; 
    [self addSubview:imageView]; 
    [imageView release]; 

    UILabel *descriptionLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 60, 200, 100)]; 
    [self addSubview:descriptionLabel]; 
    [descriptionLabel release]; 
} 

在上面代码,您可以根据需要调整帧。一旦设置完成,只需在视图/视图控制器中像正常一样创建按钮并调用上述方法即可:

UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
    myButton.frame = CGRectMake(0, 0, 300, 200); 
    [myButton setupButton:myTitle image:myImage description:myDesc]; 
2

子类UIControl并覆盖drawRect和/或添加子视图以创建所需的外观。

相关问题