2011-05-23 73 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和/或添加子視圖以創建所需的外觀。

相關問題