Thursday, May 30, 2013

UIButton contentEdgeInsets example in Objective C (iOS).

UIButton contentEdgeInsets

The inset or outset margins for the rectangle surrounding all of the button’s content.

@property(nonatomic) UIEdgeInsets contentEdgeInsets

Discussion of [UIButton contentEdgeInsets]
Use this property to resize and reposition the effective drawing rectangle for the button content. The content comprises the button image and button title. You can specify a different value for each of the four insets (top, left, bottom, right). A positive value shrinks, or insets, that edge—moving it closer to the center of the button. A negative value expands, or outsets, that edge. Use the UIEdgeInsetsMake function to construct a value for this property. The default value is UIEdgeInsetsZero.

UIButton contentEdgeInsets example.
emailBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
emailBtn.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);

Example of [UIButton contentEdgeInsets].
-(void)touchDownConsonantButton:(id)sender {
isButtonDown = YES;

 NSMutableString *c = (NSMutableString *)[[(UIButton *)sender titleLabel] text];

 btnDown = (UIButton *)sender;
[btnDown setTitle:c forState:UIControlStateNormal];
btnDown.titleLabel.font = [UIFont fontWithName:@"Hanuman" size:35];
btnDown.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 30, 0);
btnDown.frame = CGRectMake(btnDown.frame.origin.x-11, btnDown.frame.origin.y-55, 50.0, 97.0);}

 - (void) touchUpConsonantButton:(UIButton*)sender {
isButtonDown = NO;

btnUp = (UIButton *)sender;

btnUp.titleLabel.font = [UIFont fontWithName:@"Hanuman" size:25];
btnUp.frame = CGRectMake(btnUp.frame.origin.x+12, btnUp.frame.origin.y+55, 27.0, 40.0);
//UIEdgeInsetsMake(top, left, bottom, right)
btnUp.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
[UIView animateWithDuration:0.1 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
    btnUp.transform = CGAffineTransformIdentity;
} completion:^(BOOL finished){

}];

if (onSetTextFieldCallBack!= nil) {

    NSMutableString *c = (NSMutableString *)[[(UIButton *)sender titleLabel] text];

    self.onSetTextFieldCallBack (c);

}}

UIButton contentEdgeInsets example.
-(void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = HEXCOLOR(0x000000);

    btnLogin = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btnLogin.frame = CGRectMake(0, 0, 180.f, 48);
    [btnLogin setTitle:@"Login" forState:UIControlStateNormal];
    [btnLogin addTarget:self action:@selector(LoginPressed)
       forControlEvents:UIControlEventTouchUpInside];
    btnLogin.tag=1;

    btnLogin.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [btnLogin setTitleColor: [UIColor whiteColor] forState:UIControlStateNormal];
    btnLogin.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);

    UIImage *btnImage = [UIImage imageNamed:@"nav_top.png"];
    [btnLogin setBackgroundImage:btnImage forState:UIControlStateNormal];
    [self.view addSubview:btnLogin];

    buttonUser = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    buttonUser.frame = CGRectMake(0, 0, 180.f, 48);
    //[buttonUser setTitle:_Email forState:UIControlStateNormal];
    [buttonUser addTarget:self action:@selector(EmailPressed)
         forControlEvents:UIControlEventTouchUpInside];
    buttonUser.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [buttonUser setTitleColor: [UIColor whiteColor] forState:UIControlStateNormal];
    buttonUser.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);

    UIImage *btnImage1 = [UIImage imageNamed:@"nav_top.png"];
    [buttonUser setBackgroundImage:btnImage1 forState:UIControlStateNormal];
    [self.view addSubview:buttonUser];
    buttonUser.hidden = YES;
}

End of UIButton contentEdgeInsets example article.