2

I would like to create a button with an image, title and description similar to the UITableViewCellStyleSubtitle. I would like to do this programmatically.

Does anyone know how I can accomplish this?

Atma
  • 29,141
  • 56
  • 198
  • 299

2 Answers2

6

You could create a category of the UIButton class. Here's a barebones setup for you:

In the .h file:

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

In the .m file:

@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];
}

In the above code, you can adjust the frames as needed. Once this has been setup, just create your button like normal in your view/viewcontroller and call the method above:

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

Subclass UIControl and override drawRect and/or add subviews to create the desired appearance.

titaniumdecoy
  • 18,900
  • 17
  • 96
  • 133