UIButton -> Set Text on Bottom

Discussion in 'iOS Programming' started by CarlosH, Feb 5, 2009.

  1. CarlosH macrumors member

    Joined:
    Apr 9, 2008
    Location:
    San Francisco, CA
    #1
    Hi. How can I set the text on the bottom, and keep the image on the center? Using setContextVerticalAlignment sets both.

    Thanks.
     
  2. jnic macrumors 6502a

    Joined:
    Oct 24, 2008
    Location:
    Cambridge
    #2
    Does contentVerticalAlignment affect the background image? Simplest fix is probably some transparent padding on your image to the height of the button.

    You could do it in code with something like:

    Code:
    UIImage *yourImage = [UIImage imageNamed:@"whatever.png"];
    CGSize yourButtonSize = yourButton.frame.size;
    
    UIGraphicsBeginImageContext(yourButtonSize);
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    // Draw the image vertically centered in the button's frame.
    [yourImage drawInRect:CGRectMake(0.0, floor((yourButton.height - yourImage.height) / 2.0), yourImage.width, yourImage.height)];
    
    // New button background image.
    UIImage *yourPaddedImage = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    or just tweak them by hand if it's only a couple of images.
     
  3. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #3
    UIButton.imageEdgeInsets and .titleEdgeInsets ?
     
  4. jnic macrumors 6502a

    Joined:
    Oct 24, 2008
    Location:
    Cambridge
    #4
    Now I feel foolish :p
     

Share This Page