如何创建自定义UIButton?

时间:2011-11-20 22:29:24

标签: iphone objective-c cocoa-touch ipad

我想知道如何在内部创建一个带有UILabel的自定义UIButton image

2 个答案:

答案 0 :(得分:0)

查看有关CIMGF Fun with UIButtons and core animation layers

的本教程

或者:IphoneDevelopment: Programmatic gradient buttons

您基本上只需创建UIButton的子类即可实现自定义绘图。 然后你可以做任何你想做的事。这两个教程有助于创建您想要的样式,请记住,如果您不想过多地使用绘图代码,也可以使用图像。

答案 1 :(得分:0)

您可以执行以下操作:

int i=0;
UIButton *customButton=[UIButton buttonWithType:UIButtonTypeRoundedRect];

//Set the height of your button, that will depend on how much text you will be putting
//inside
[customButton setFrame:CGRectMake(3,40,315,130)];

for(i=0; i<7; i++){
    //Their position will change depending on which label you are adding
    UILabel *leftLabel=[[UILabel alloc] initWithFrame:CGRectMake(5, 5+(17*i), 150, 12)];
    UILabel *rightLabel=[[UILabel alloc] initWithFrame:CGRectMake(160, 5+(17*i), 150, 12)];

    //Set the text for each label
    [leftLabel setText:[NSString stringWithFormat:@"Left %d",i+1]];
    [rightLabel setText:[NSString stringWithFormat:@"Right %d",i+1]];

    //Set the backgroudn as clear to be able to see the background of your button
    [leftLabel setBackgroundColor:[UIColor clearColor]];
    [rightLabel setBackgroundColor:[UIColor clearColor]];

    //Add those labels
    [customButton addSubview:leftLabel];
    [customButton addSubview:rightLabel];

    //Do your memory management 
    [leftLabel release];
    [rightLabel release];
}

CIMGF链接将满足您的风格需求。