Objective-C - How to use UIButton on ios develop

tag: Objective-C    date: 2018-10-08

create a new uibutton on uiview

UIButton *button = [[UIButton alloc] initWithFrame:(CGRect){10,40,300,50}];
[button setTitle:@"Test Button" forState:UIControlStateNormal];
[button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[self.view addSubview:button];
create uibutton

uibutton change font

[button setFont:[UIFont fontWithName:@"AmericanTypewriter" size:21]];
uibutton change font

uibutton change text color

[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
uibutton change text color

uibutton change background color

[button setBackgroundColor:[UIColor colorWithRed:0.3 green:0.5 blue:0.7 alpha:1]];
uibutton change background color

uibutton set text alignment - left align text

button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
button.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
uibutton set text alignment

uibutton set multiline text

button.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
button.titleLabel.textAlignment = NSTextAlignmentCenter;
[button setTitle: @"Line 1\nLine 2" forState: UIControlStateNormal];
uibutton set multiline text

uibutton set text underline

NSMutableAttributedString* attributedString = [[NSMutableAttributedString alloc] initWithString:@"Button Title"];
[attributedString addAttribute:NSUnderlineStyleAttributeName
                      			     value:@(NSUnderlineStyleDouble)
                                         range:(NSRange){0,attributedString.length}];
[attributedString addAttribute:NSUnderlineColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, attributedString.length)];
[button setAttributedTitle:attributedString forState:UIControlStateNormal];
uibutton set text underline

uibutton set image

[button setImage:[UIImage imageNamed:@"btn-normal"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:@"btn-highlighted"] forState:UIControlStateHighlighted];
uibutton set image

uibutton set border and border color

#import <QuartzCore/QuartzCore.h>
button.layer.borderWidth = 2.0f;
button.layer.borderColor = [UIColor redColor].CGColor;
uibutton set border

uibutton set border radius

button.layer.cornerRadius = 8.0f;
button.layer.masksToBounds = YES;
uibutton set border radius

uibutton set shadow

button.layer.shadowRadius = 4.0f;
button.layer.shadowColor = [UIColor blackColor].CGColor;
button.layer.shadowOffset = CGSizeMake(0.0f, 1.0f);
button.layer.shadowOpacity = 0.4f;
button.layer.masksToBounds = NO;
uibutton set shadow

uibutton add action programmatically - handle click event

[button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchDown];
-(void) click:(id) sender {
    [[[UIAlertView alloc] initWithTitle:@"demo"
                                message:@"Button Click"
                               delegate:self
                      cancelButtonTitle:@"cancel"
                      otherButtonTitles:nil] show];
}
uibutton add action

uibutton handle long press event

UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
[button addGestureRecognizer:longPress];
-(void) longPress:(UILongPressGestureRecognizer*)gesture{
    if ( gesture.state == UIGestureRecognizerStateEnded ) {
        [[[UIAlertView alloc] initWithTitle:@"demo"
                                    message:@"Button Long Press"
                                   delegate:self
                          cancelButtonTitle:@"cancel"
                          otherButtonTitles:nil] show];
    }
}
uibutton long press

more articles
Copyright © 2018
This Site is Licensed under a Creative Commons License. Powerd by Java