Codesen Codesen - 16 days ago 6
iOS Question

UIButton Image + Text IOS

I need a UIButton with image & text. Image should be in the top & text comes under the image both should be clickable.

Answer

I have create a category for UIButton with a method solved my problem

-(void) setLabel:(NSString*)inTitle 
{

    [self setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self setTitle:inTitle forState:UIControlStateNormal];
    [self.titleLabel setFont:[UIFont fontWithName:@"Arial" size:11]];

    self.titleLabel.lineBreakMode = UILineBreakModeWordWrap;
    self.titleLabel.numberOfLines = 2;
    self.titleLabel.textAlignment = UITextAlignmentCenter;

    [self.imageView.layer setBorderWidth:0.0];
    [self.imageView.layer setBorderColor:[[UIColor colorWithWhite:0.3 alpha:0.7] CGColor]];

    [self.titleLabel.layer setBorderWidth:0.0];
    [self.titleLabel.layer setBorderColor:[[UIColor colorWithWhite:0.3 alpha:0.7] CGColor]];


    // get the size of the elements here for readability
    CGSize imageSize = self.imageView.frame.size;



    // lower the text and push it left to center it
    self.titleEdgeInsets = UIEdgeInsetsMake(
                                              0.0, - imageSize.width , - (imageSize.height + 7), 0.0);


    // raise the image and push it right to center it

    self.imageEdgeInsets = UIEdgeInsetsMake(-25, 0.0, 0.0, 0);


}
Comments