Showing posts with label UIButton example. Show all posts
Showing posts with label UIButton example. Show all posts

Thursday, May 30, 2013

UIButton UIButtonTypeContactAdd example in Objective C (iOS).

UIButton UIButtonTypeContactAdd

Specifies the style of a button.

typedef enum {
UIButtonTypeCustom = 0,
UIButtonTypeRoundedRect,
UIButtonTypeDetailDisclosure,
UIButtonTypeInfoLight,
UIButtonTypeInfoDark,
UIButtonTypeContactAdd,
} UIButtonType;

Constants
UIButtonTypeCustom
No button style.
UIButtonTypeRoundedRect
A rounded-rectangle style button.
UIButtonTypeDetailDisclosure
A detail disclosure button.
UIButtonTypeInfoLight
An information button that has a light background.
UIButtonTypeInfoDark
An information button that has a dark background.
UIButtonTypeContactAdd
A contact add button.

UIButton UIButtonTypeContactAdd example.
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
    UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(-100, 0, 420, 480)]; // start 100 pixels to the left
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeContactAdd];
    btn.frame = CGRectMake(0, 10, btn.bounds.size.width, btn.bounds.size.height);
    [myView addSubview:btn];

    UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [btn1 setTitle:@"DoIt" forState:UIControlStateNormal];
    btn1.frame = CGRectMake(100, 100, 50, 50);
    [btn1 addTarget:self action:@selector(doit:) forControlEvents:UIControlEventTouchUpInside];
    [myView addSubview:btn1];
    self.view = myView;
}

Example of [UIButton UIButtonTypeContactAdd].
[super viewDidLoad];
offscreenView = [[UIView alloc] initWithFrame:CGRectMake(-100, 0, 200, 200)];
offscreenView.backgroundColor = [UIColor blueColor];
UIButton * btn = [UIButton buttonWithType:UIButtonTypeContactAdd];
//note we put this at 10, 10 *relative to the button's superview*
btn.frame = CGRectMake(10.0, 10.0, 50.0, 25.0);
[btn addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[offscreenView addSubview:btn];
[self.view addSubview:offscreenView];
[UIView beginAnimations:@"animateVIew" context:self];
offscreenView.frame = CGRectMake(0.0, 0.0, 200, 200);
[UIView setAnimationDuration:2.0];
[UIView commitAnimations];

UIButton UIButtonTypeContactAdd example.
UIButton *contactAddButton = [UIButton buttonWithType:UIButtonTypeContactAdd];
[contactAddButton setUserInteractionEnabled:FALSE];
cell.accessoryView = contactAddButton;

End of UIButton UIButtonTypeContactAdd example article.

UIButton UIButtonTypeInfoDark example in Objective C (iOS).

UIButton UIButtonTypeInfoDark

Specifies the style of a button.

typedef enum {
UIButtonTypeCustom = 0,
UIButtonTypeRoundedRect,
UIButtonTypeDetailDisclosure,
UIButtonTypeInfoLight,
UIButtonTypeInfoDark,
UIButtonTypeContactAdd,
} UIButtonType;

Constants
UIButtonTypeCustom
No button style.
UIButtonTypeRoundedRect
A rounded-rectangle style button.
UIButtonTypeDetailDisclosure
A detail disclosure button.
UIButtonTypeInfoLight
An information button that has a light background.
UIButtonTypeInfoDark
An information button that has a dark background.
UIButtonTypeContactAdd
A contact add button.

UIButton UIButtonTypeInfoDark example.
// Info button
UIButton* infoButton = [UIButton buttonWithType: UIButtonTypeInfoDark];
[infoButton addTarget:self action:@selector(showInfoView:) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:infoButton];

Example of [UIButton UIButtonTypeInfoDark].
- (void) viewDidLoad {
  UIButton * info = [UIButton buttonWithType: UIButtonTypeInfoDark];
  // set your button's target and selector for whatever action here.
  self.navigationItem.rightBarButtonItem.customView = info;
}

UIButton UIButtonTypeInfoDark example.
UIButton* infoButton = [UIButton buttonWithType: UIButtonTypeInfoDark];
[infoButton addTarget:self action:@selector(infoButtonAction) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *modalButton = [[UIBarButtonItem alloc] initWithCustomView:infoButton];
[self.navigationItem setRightBarButtonItem:modalButton animated:YES];

End of UIButton UIButtonTypeInfoDark example article.

UIButton UIButtonTypeInfoLight example in Objective C (iOS).

UIButton UIButtonTypeInfoLight

Specifies the style of a button.

typedef enum {
UIButtonTypeCustom = 0,
UIButtonTypeRoundedRect,
UIButtonTypeDetailDisclosure,
UIButtonTypeInfoLight,
UIButtonTypeInfoDark,
UIButtonTypeContactAdd,
} UIButtonType;

Constants
UIButtonTypeCustom
No button style.
UIButtonTypeRoundedRect
A rounded-rectangle style button.
UIButtonTypeDetailDisclosure
A detail disclosure button.
UIButtonTypeInfoLight
An information button that has a light background.
UIButtonTypeInfoDark
An information button that has a dark background.
UIButtonTypeContactAdd
A contact add button.

UIButton UIButtonTypeInfoLight example.
// Info button
UIButton* infoButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
[infoButton addTarget:self action:@selector(showInfoView:) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:infoButton];

Example of [UIButton UIButtonTypeInfoLight].
- (void) viewDidLoad {
  UIButton * info = [UIButton buttonWithType:UIButtonTypeInfoLight];
  // set your button's target and selector for whatever action here.
  self.navigationItem.rightBarButtonItem.customView = info;
}

UIButton UIButtonTypeInfoLight example.
UIButton* infoButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
[infoButton addTarget:self action:@selector(infoButtonAction) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *modalButton = [[UIBarButtonItem alloc] initWithCustomView:infoButton];
[self.navigationItem setRightBarButtonItem:modalButton animated:YES];

End of UIButton UIButtonTypeInfoLight example article.

UIButton UIButtonTypeDetailDisclosure example in Objective C (iOS).

UIButton UIButtonTypeDetailDisclosure

Specifies the style of a button.

typedef enum {
UIButtonTypeCustom = 0,
UIButtonTypeRoundedRect,
UIButtonTypeDetailDisclosure,
UIButtonTypeInfoLight,
UIButtonTypeInfoDark,
UIButtonTypeContactAdd,
} UIButtonType;

Constants
UIButtonTypeCustom
No button style.
UIButtonTypeRoundedRect
A rounded-rectangle style button.
UIButtonTypeDetailDisclosure
A detail disclosure button.
UIButtonTypeInfoLight
An information button that has a light background.
UIButtonTypeInfoDark
An information button that has a dark background.
UIButtonTypeContactAdd
A contact add button.

UIButton UIButtonTypeDetailDisclosure example.
#import 

UIButton* btn = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
NSString* info = @"foobar";
objc_setAssociatedObject(btn, "info", info, OBJC_ASSOCIATION_RETAIN);
//Later somewhere
NSString* btnInfo = (NSString*)objc_getAssociatedObject(btn, "info");

Example of [UIButton UIButtonTypeDetailDisclosure].
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id)annotation{

MKPinAnnotationView *mypin = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"current"];
mypin.pinColor = MKPinAnnotationColorPurple;
mypin.backgroundColor = [UIColor clearColor];
UIButton *goToDetail = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
mypin.rightCalloutAccessoryView = myBtn;
mypin.draggable = NO;
mypin.highlighted = YES;
mypin.animatesDrop = TRUE;
mypin.canShowCallout = YES;
return mypin;
}

UIButton UIButtonTypeDetailDisclosure example.
-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:
(id )annotation {    
    MKAnnotationView* ann = nil;   
    if(annotation != mapView.userLocation)
    {
        ann = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"try"];   
        ann.canShowCallout = YES;   
        ann.image = [UIImage imageNamed:@"arrow.png"];       
        UIButton* butt = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];   
        [butt addTarget:self action:@selector(displayArrow:) forControlEvents:    
        UIControlEventTouchUpInside];       
        ann.rightCalloutAccessoryView = butt;   
    }
    return ann;
}

End of UIButton UIButtonTypeDetailDisclosure example article.

UIButton UIButtonTypeRoundedRect example in Objective C (iOS).

UIButton UIButtonTypeRoundedRect

Specifies the style of a button.

typedef enum {
UIButtonTypeCustom = 0,
UIButtonTypeRoundedRect,
UIButtonTypeDetailDisclosure,
UIButtonTypeInfoLight,
UIButtonTypeInfoDark,
UIButtonTypeContactAdd,
} UIButtonType;

Constants
UIButtonTypeCustom
No button style.
UIButtonTypeRoundedRect
A rounded-rectangle style button.
UIButtonTypeDetailDisclosure
A detail disclosure button.
UIButtonTypeInfoLight
An information button that has a light background.
UIButtonTypeInfoDark
An information button that has a dark background.
UIButtonTypeContactAdd
A contact add button.

UIButton UIButtonTypeRoundedRect example.
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];

[myButton addTarget:self action:@selector(dismissView:) forControlEvents:UIControlEventTouchUpInside];

myButton.frame = CGRectMake(0, 0, 60, 30);

[myButton setTitle:@"Done" forState:UIControlStateNormal];

myButton.backgroundColor = [UIColor colorWithHue:1.0/12 saturation:2.0/3 brightness:4.0/10 alpha:1.0];

UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithCustomView:myButton];

[[self navigationItem] setLeftBarButtonItem:button animated:YES];

Example of [UIButton UIButtonTypeRoundedRect].
UIButton *b = [UIButton buttonWithType:UIButtonTypeRoundedRect] ;   
b. frame = CGRectMake(0, 0, 100, 100);

[b setTitle:@"Testing" forState:UIControlStateNormal];
[b setTitleColor: [UIColor blackColor] forState: UIControlStateNormal];
[self addSubview:b];

UIButton UIButtonTypeRoundedRect example.
UIButton *b=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
b=[UIButton buttonWithType:UIButtonTypeRoundedRect];

[b setTitle:@"Button Title Here" forState:UIControlStateNormal];
[b setTitleColor:[UIColor blackColor] forState: UIControlStateNormal];
[self.view addSubview:b];

End of UIButton UIButtonTypeRoundedRect example article.

UIButton UIButtonTypeCustom example in Objective C (iOS).

UIButton UIButtonTypeCustom

Specifies the style of a button.

typedef enum {
UIButtonTypeCustom = 0,
UIButtonTypeRoundedRect,
UIButtonTypeDetailDisclosure,
UIButtonTypeInfoLight,
UIButtonTypeInfoDark,
UIButtonTypeContactAdd,
} UIButtonType;

Constants
UIButtonTypeCustom
No button style.
UIButtonTypeRoundedRect
A rounded-rectangle style button.
UIButtonTypeDetailDisclosure
A detail disclosure button.
UIButtonTypeInfoLight
An information button that has a light background.
UIButtonTypeInfoDark
An information button that has a dark background.
UIButtonTypeContactAdd
A contact add button.

UIButton UIButtonTypeCustom example.
UIButton *buttonUserName = [UIButton buttonWithType:UIButtonTypeCustom];
buttonUserName.frame = CGRectMake(10, 10, 280, 20);
[buttonUserName setTitle:@"test" forState:UIControlStateNormal];
[buttonUserName setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[buttonUserName setEnabled:YES];
buttonUserName.userInteractionEnabled = YES;
[buttonUserName addTarget:self action:@selector(user:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:buttonUserName];

Example of [UIButton UIButtonTypeCustom].
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(x, y, width, height)];
[button setTitle:@"Button" forState:UIControlStateNormal];
// Set visible values
[button setBackgroundColor:[UIColor greenColor]];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[someSuperview addSubview:button];

UIButton UIButtonTypeCustom example.
UIButton *test = [UIButton buttonWithType:UIButtonTypeCustom];
test.frame = CGRectMake(30, 30, 49, 49);

End of UIButton UIButtonTypeCustom example article.

UIButton titleRectForContentRect example in Objective C (iOS).

UIButton titleRectForContentRect

Returns the rectangle in which the receiver draws its title.

- (CGRect)titleRectForContentRect:(CGRect)contentRect

Parameters
contentRect
The content rectangle for the receiver.

Return Value of [UIButton titleRectForContentRect]
The rectangle in which the receiver draws its title.

UIButton titleRectForContentRect example.
@implementation UIButtonSubclass

- (CGRect)imageRectForContentRect:(CGRect)contentRect
{
    CGRect frame = [super imageRectForContentRect:contentRect];
    frame.origin.x = CGRectGetMaxX(contentRect) - CGRectGetWidth(frame) -  self.imageEdgeInsets.right + self.imageEdgeInsets.left;
    return frame;
}

- (CGRect)titleRectForContentRect:(CGRect)contentRect
{
    CGRect frame = [super titleRectForContentRect:contentRect];
    frame.origin.x = CGRectGetMinX(frame) - CGRectGetWidth([self imageRectForContentRect:contentRect]);
    return frame;
}

@end

Example of [UIButton titleRectForContentRect].
@interface PositionTitleButton : UIButton
@property (nonatomic) CGPoint titleOrigin;
@end

@implementation PositionTextButton
- (CGRect)titleRectForContentRect:(CGRect)contentRect {
  contentRect.origin = titleOrigin;
  return contentRect;
}
@end

UIButton titleRectForContentRect example.
tabLog.frame=CGRectMake(10, 400, 32, 53);

    [tabLog titleRectForContentRect:CGRectMake(0, 32, 32, 21)];
    [tabLog imageRectForContentRect:CGRectMake(0, 0, 32, 32)];
    [tabLog setTitle:@"Test" forState:UIControlStateNormal];
    [tabLog setImage:[UIImage imageNamed:@"log.png"] forState:UIControlStateNormal];

End of UIButton titleRectForContentRect example article.

UIButton titleForState example in Objective C (iOS).

UIButton titleForState

Returns the title associated with the specified state.

- (NSString *)titleForState:(UIControlState)state

Parameters
state
The state that uses the title. The possible values are described in UIControlState.

Return Value of [UIButton titleForState]
The title for the specified state. If no title has been set for the specific state, this method returns the title associated with the UIControlStateNormal state.

UIButton titleForState example.
- (void)myButtonClicked:(id)sender{
    UIButton * clickedButton = (UIButton *)sender;
    NSString * buttonTitle = [clickedButton titleForState:UIControlStateNormal];
    NSLog(@"title: %@",buttonTitle);
    UILabel * myLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 250, 100,80)];
    myLabel.text = buttonTitle;
    [window addSubview:myLabel];
}

Example of [UIButton titleForState].
//Right-align the button image
CGSize size = [[myButton titleForState:UIControlStateNormal] sizeWithFont:myButton.titleLabel.font];
[myButton setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, -size.width)];
[myButton setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, myButton.imageView.image.size.width + 5)];

UIButton titleForState example.
-(void)changeButton:(NSString*)button withURL:(NSString*)url {
timetableURL = url;

UIImage *btnImage;

if ([button titleForState:UIControlStateNormal] isEqualToString:@"Safari"]) {
    btnImage = [UIImage imageNamed:@"browser-button.png"];
    [safariBtn addTarget:self action:@selector(onSafariButtonPress:) forControlEvents:UIControlEventTouchUpInside];
}
else if ([button titleForState:UIControlStateNormal] isEqualToString:@"Timetable"]) {
    btnImage = [UIImage imageNamed:@"browser-timetable.png"]; 
    [safariBtn addTarget:self action:@selector(onTimetableButtonPress:) forControlEvents:UIControlEventTouchUpInside];
}

[safariBtn setImage:btnImage forState:UIControlStateNormal];
[btnImage release];
}

End of UIButton titleForState example article.

UIButton titleColorForState example in Objective C (iOS).

UIButton titleColorForState

Returns the title color used for a state.

- (UIColor *)titleColorForState:(UIControlState)state

Parameters
state
The state that uses the title color. The possible values are described in UIControlState.

Return Value of [UIButton titleColorForState]
The color of the title for the specified state.

UIButton titleColorForState example.
    UIControlState mixedState = UIControlStateSelected | UIControlStateHighlighted;
    [button setImage:[button imageForState:state] forState:state];
    [button setBackgroundImage:[button backgroundImageForState:state] forState:state];
    [button setTitleColor:[button titleColorForState:state] forState:state];

Example of [UIButton titleColorForState].
label.textColor = [button titleColorForState: UIControlStateNormal];

UIButton titleColorForState example.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
if ([[cell contentView] viewWithTag:25])
{
    [[[cell contentView] viewWithTag:25] removeFromSuperview];
}


    CustomButton*cButton= [[[self dataSource] otherButtonArr] objectAtIndex:indexPath.row];
    cell.textLabel.text = [cButton titleForState:UIControlStateNormal];
    cell.textLabel.textColor = [cButton titleColorForState:UIControlStateNormal];
    cell.accessoryType =   (cButton.selected)?UITableViewCellAccessoryCheckmark:UITableViewCellAccessoryNone;


  return cell;
  }

  #pragma mark - Table view delegate

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
 {
CustomButton*cButton= [[[self dataSource] otherButtonArr] objectAtIndex:indexPath.row];
[self didSelectedOption:[cButton titleForState:UIControlStateNormal]];
}

End of UIButton titleColorForState example article.

UIButton setTitleShadowColor forState example in Objective C (iOS).

UIButton setTitleShadowColor forState

Sets the color of the title shadow to use for the specified state.

- (void)setTitleShadowColor:(UIColor *)color forState:(UIControlState)state

Parameters of [UIButton setTitleShadowColor forState]
color
The color of the title shadow to use for the specified state.
state
The state that uses the specified color. The possible values are described in UIControlState.

Discussion of [UIButton setTitleShadowColor forState]
In general, if a property is not specified for a state, the default is to use the UIControlStateNormal value. If the UIControlStateNormal value is not set, then the property defaults to a system value. Therefore, at a minimum, you should set the value for the normal state.

UIButton setTitleShadowColor forState example.
[button setTitleShadowColor:[UIColor blueColor] forState:UIControlStateNormal];

Example of [UIButton setTitleShadowColor forState].
for (id subView in theNavigationBar.subviews) {
    if ([subView isKindOfClass:[UIButton class]]) {
        [(UIButton *)subView setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [(UIButton *)subView setTitleShadowColor:[UIColor clearColor] forState:UIControlStateNormal];
    }

}

UIButton setTitleShadowColor forState example.
+ (UIBarButtonItem *)createSquareBarButtonItemWithTitle:(NSString *)t target:(id)tgt action:(SEL)a
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    // Since the buttons can be any width we use a thin image with a stretchable center point
    UIImage *buttonImage = [[UIImage imageNamed:@"SquareButton.png"] stretchableImageWithLeftCapWidth:5 topCapHeight:0];
    UIImage *buttonPressedImage = [[UIImage imageNamed:@"SquareButton_pressed.png"] stretchableImageWithLeftCapWidth:5 topCapHeight:0];

    [[button titleLabel] setFont:[UIFont boldSystemFontOfSize:12.0]];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
    [button setTitleShadowColor:[UIColor colorWithWhite:1.0 alpha:0.7] forState:UIControlStateNormal];
    [button setTitleShadowColor:[UIColor clearColor] forState:UIControlStateHighlighted];
    [[button titleLabel] setShadowOffset:CGSizeMake(0.0, 1.0)];

    CGRect buttonFrame = [button frame];
    buttonFrame.size.width = [t sizeWithFont:[UIFont boldSystemFontOfSize:12.0]].width + 24.0;
    buttonFrame.size.height = buttonImage.size.height;
    [button setFrame:buttonFrame];

    [button setBackgroundImage:buttonImage forState:UIControlStateNormal];
    [button setBackgroundImage:buttonPressedImage forState:UIControlStateHighlighted];

    [button setTitle:t forState:UIControlStateNormal];

    [button addTarget:tgt action:a forControlEvents:UIControlEventTouchUpInside];

    UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc] initWithCustomView:button];

    return [buttonItem autorelease];
}

End of UIButton setTitleShadowColor forState example article.

UIButton setTitleShadowColor example in Objective C (iOS).

UIButton setTitleShadowColor

Sets the color of the title shadow to use for the specified state.

- (void)setTitleShadowColor:(UIColor *)color forState:(UIControlState)state

Parameters
color
The color of the title shadow to use for the specified state.
state
The state that uses the specified color. The possible values are described in UIControlState.

Discussion of [UIButton setTitleShadowColor]
In general, if a property is not specified for a state, the default is to use the UIControlStateNormal value. If the UIControlStateNormal value is not set, then the property defaults to a system value. Therefore, at a minimum, you should set the value for the normal state.

UIButton setTitleShadowColor example.
[button setTitleShadowColor:[UIColor blueColor] forState:UIControlStateNormal];

Example of [UIButton setTitleShadowColor].
for (id subView in theNavigationBar.subviews) {
    if ([subView isKindOfClass:[UIButton class]]) {
        [(UIButton *)subView setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [(UIButton *)subView setTitleShadowColor:[UIColor clearColor] forState:UIControlStateNormal];
    }

}

UIButton setTitleShadowColor example.
+ (UIBarButtonItem *)createSquareBarButtonItemWithTitle:(NSString *)t target:(id)tgt action:(SEL)a
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    // Since the buttons can be any width we use a thin image with a stretchable center point
    UIImage *buttonImage = [[UIImage imageNamed:@"SquareButton.png"] stretchableImageWithLeftCapWidth:5 topCapHeight:0];
    UIImage *buttonPressedImage = [[UIImage imageNamed:@"SquareButton_pressed.png"] stretchableImageWithLeftCapWidth:5 topCapHeight:0];

    [[button titleLabel] setFont:[UIFont boldSystemFontOfSize:12.0]];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
    [button setTitleShadowColor:[UIColor colorWithWhite:1.0 alpha:0.7] forState:UIControlStateNormal];
    [button setTitleShadowColor:[UIColor clearColor] forState:UIControlStateHighlighted];
    [[button titleLabel] setShadowOffset:CGSizeMake(0.0, 1.0)];

    CGRect buttonFrame = [button frame];
    buttonFrame.size.width = [t sizeWithFont:[UIFont boldSystemFontOfSize:12.0]].width + 24.0;
    buttonFrame.size.height = buttonImage.size.height;
    [button setFrame:buttonFrame];

    [button setBackgroundImage:buttonImage forState:UIControlStateNormal];
    [button setBackgroundImage:buttonPressedImage forState:UIControlStateHighlighted];

    [button setTitle:t forState:UIControlStateNormal];

    [button addTarget:tgt action:a forControlEvents:UIControlEventTouchUpInside];

    UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc] initWithCustomView:button];

    return [buttonItem autorelease];
}

End of UIButton setTitleShadowColor example article.

UIButton setTitleColor forState example in Objective C (iOS).

UIButton setTitleColor forState

Sets the color of the title to use for the specified state.

- (void)setTitleColor:(UIColor *)color forState:(UIControlState)state

Parameters of [UIButton setTitleColor forState]
color
The color of the title to use for the specified state.
state
The state that uses the specified color. The possible values are described in UIControlState.

Discussion of [UIButton setTitleColor forState]
In general, if a property is not specified for a state, the default is to use the UIControlStateNormal value. If the UIControlStateNormal value is not set, then the property defaults to a system value. Therefore, at a minimum, you should set the value for the normal state.

UIButton setTitleColor forState example.
 UIButtin  *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
   [myButton setFrame:CGRectMake(165, 205, 65, 40)];
   [myButton setTitleColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:.77]  forState:UIControlStateNormal];
   [myButton setTitle:@"Hi....." forState:UIControlStateNormal];
   [myButton addTarget:self action:@selector(btnClicked:)     forControlEvents:UIControlEventTouchUpInside];
   [myButton setBackgroundImage:[UIImage imageNamed:@"mynewimage.png"] forState:UIControlStateNormal];
   [cell  addSubview:myButton];

Example of [UIButton setTitleColor forState].
button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setSelected:YES];
    button.frame = CGRectMake(x, y, width, height);
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blueColor] forState:UIControlStateSelected];
    [button setTitle:@"Button Title" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [button setBackgroundImage:[UIImage imageNamed:@"button.png"] forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageNamed:@"buttonActive.png"] forState:UIControlStateHighlighted];

UIButton setTitleColor forState example.
UIButton *b = [UIButton buttonWithType:UIButtonTypeRoundedRect] ;   
b. frame = CGRectMake(0, 0, 100, 100);

[b setTitle:@"Testing" forState:UIControlStateNormal];
[b setTitleColor: [UIColor blackColor] forState: UIControlStateNormal];
[self addSubview:b];

End of UIButton setTitleColor forState example article.

UIButton setTitleColor example in Objective C (iOS).

UIButton setTitleColor

Sets the color of the title to use for the specified state.

- (void)setTitleColor:(UIColor *)color forState:(UIControlState)state

Parameters
color
The color of the title to use for the specified state.
state
The state that uses the specified color. The possible values are described in UIControlState.

Discussion of [UIButton setTitleColor]
In general, if a property is not specified for a state, the default is to use the UIControlStateNormal value. If the UIControlStateNormal value is not set, then the property defaults to a system value. Therefore, at a minimum, you should set the value for the normal state.

UIButton setTitleColor example.
 UIButtin  *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
   [myButton setFrame:CGRectMake(165, 205, 65, 40)];
   [myButton setTitleColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:.77]  forState:UIControlStateNormal];
   [myButton setTitle:@"Hi....." forState:UIControlStateNormal];
   [myButton addTarget:self action:@selector(btnClicked:)     forControlEvents:UIControlEventTouchUpInside];
   [myButton setBackgroundImage:[UIImage imageNamed:@"mynewimage.png"] forState:UIControlStateNormal];
   [cell  addSubview:myButton];

Example of [UIButton setTitleColor].
button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setSelected:YES];
    button.frame = CGRectMake(x, y, width, height);
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blueColor] forState:UIControlStateSelected];
    [button setTitle:@"Button Title" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [button setBackgroundImage:[UIImage imageNamed:@"button.png"] forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageNamed:@"buttonActive.png"] forState:UIControlStateHighlighted];

UIButton setTitleColor example.
UIButton *b = [UIButton buttonWithType:UIButtonTypeRoundedRect] ;   
b. frame = CGRectMake(0, 0, 100, 100);

[b setTitle:@"Testing" forState:UIControlStateNormal];
[b setTitleColor: [UIColor blackColor] forState: UIControlStateNormal];
[self addSubview:b];

End of UIButton setTitleColor example article.

UIButton setTitle forState example in Objective C (iOS).

UIButton setTitle forState

Sets the title to use for the specified state.

- (void)setTitle:(NSString *)title forState:(UIControlState)state

Parameters of [UIButton setTitle forState]
title
The title to use for the specified state.
state
The state that uses the specified title. The possible values are described in UIControlState.

Discussion of [UIButton setTitle forState]
Use this method to set the title for the button. The title you specify derives its formatting from the button’s associated label object. If you set both a title and an attributed title for the button, the button prefers the use of the attributed title over this one.

At a minimum, you should set the value for the normal state. If a title is not specified for a state, the default behavior is to use the title associated with the UIControlStateNormal state. If the value for UIControlStateNormal is not set, then the property defaults to a system value.

UIButton setTitle forState example.
UIButton *backbtn=[[UIButton alloc] initWithFrame:CGRectMake(0, 0, 68, 38)];
[backbtn setTitle:@"Your string" forState:UIControlStateNormal];
//[backbtn setImage:[UIImage imageNamed:@"BackBtn.png"] forState:UIControlStateNormal];
[backbtn addTarget:self action:@selector(backBtnClicked) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:backbtn];

Example of [UIButton setTitle forState].
MyViewcontroller *myView = [[MyViewcontroller alloc] initWithNibName:@"MyViewcontroller" bundle:nil];
[scrollView addSubview:myView.view]; // view is loaded
[myView.imageButton setTitle:@"ddd" forState:UIControlStateNormal]; // imageButton is now wired

UIButton setTitle forState example.
UIButton *dateButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[dateButton setFrame:CGRectMake(240.0, 57.0, 60.0, 30.0)];
[dateButton setTitle:@"Date" forState:UIControlStateNormal];

[dateButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[dateButton setBackgroundImage:[UIImage imageNamed:@"bg_headline.png"] forState:UIControlStateNormal];
[dateButton addTarget:self action:@selector(GoToDateSettings:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:dateButton];

End of UIButton setTitle forState example article.

UIButton setTitle example in Objective C (iOS).

UIButton setTitle

Sets the title to use for the specified state.

- (void)setTitle:(NSString *)title forState:(UIControlState)state

Parameters
title
The title to use for the specified state.
state
The state that uses the specified title. The possible values are described in UIControlState.

Discussion of [UIButton setTitle]
Use this method to set the title for the button. The title you specify derives its formatting from the button’s associated label object. If you set both a title and an attributed title for the button, the button prefers the use of the attributed title over this one.[UIButton setTitle]

At a minimum, you should set the value for the normal state. If a title is not specified for a state, the default behavior is to use the title associated with the UIControlStateNormal state. If the value for UIControlStateNormal is not set, then the property defaults to a system value.

UIButton setTitle example.
UIButton *backbtn=[[UIButton alloc] initWithFrame:CGRectMake(0, 0, 68, 38)];
[backbtn setTitle:@"Your string" forState:UIControlStateNormal];
//[backbtn setImage:[UIImage imageNamed:@"BackBtn.png"] forState:UIControlStateNormal];
[backbtn addTarget:self action:@selector(backBtnClicked) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:backbtn];

Example of [UIButton setTitle].
MyViewcontroller *myView = [[MyViewcontroller alloc] initWithNibName:@"MyViewcontroller" bundle:nil];
[scrollView addSubview:myView.view]; // view is loaded
[myView.imageButton setTitle:@"ddd" forState:UIControlStateNormal]; // imageButton is now wired

UIButton setTitle example.
UIButton *dateButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[dateButton setFrame:CGRectMake(240.0, 57.0, 60.0, 30.0)];
[dateButton setTitle:@"Date" forState:UIControlStateNormal];

[dateButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[dateButton setBackgroundImage:[UIImage imageNamed:@"bg_headline.png"] forState:UIControlStateNormal];
[dateButton addTarget:self action:@selector(GoToDateSettings:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:dateButton];

End of UIButton setTitle example article.

UIButton setImage forState example in Objective C (iOS).

UIButton setImage forState

Sets the image to use for the specified state.

- (void)setImage:(UIImage *)image forState:(UIControlState)state

Parameters of [UIButton setImage forState]
image
The image to use for the specified state.
state
The state that uses the specified title. The values are described in UIControlState.

Discussion of [UIButton setImage forState]
In general, if a property is not specified for a state, the default is to use the UIControlStateNormal value. If the UIControlStateNormal value is not set, then the property defaults to a system value. Therefore, at a minimum, you should set the value for the normal state.

UIButton setImage forState example.
UIImage *listImage = [UIImage imageNamed:@"list_icon.png"];
UIButton *listButton = [UIButton buttonWithType:UIButtonTypeCustom];

// get the image size and apply it to the button frame
CGRect listButtonFrame = listButton.frame;
listButtonFrame.size = listImage.size;
listButton.frame = listButtonFrame;

[listButton setImage:listImage forState:UIControlStateNormal];
[listButton addTarget:self.navigationController.parentViewController action:@selector(revealToggle:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *jobsButton = [[UIBarButtonItem alloc] initWithCustomView:listButton];

self.navigationItem.leftBarButtonItem = jobsButton;

Example of [UIButton setImage forState].
UIButton *btnTwo = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btnTwo.frame = CGRectMake(40, 140, 240, 30);
[btnTwo setTitle:@"vc2:v1" forState:UIControlStateNormal];
[btnTwo addTarget:self action:@selector(goToOne) forControlEvents:UIControlEventTouchUpInside];


[btnTwo setImage:[UIImage imageNamed:@"name.png"] forState:UIControlStateNormal];

//OR setting as background image

[btnTwo setBackgroundImage:[UIImage imageNamed:@"name.png"] forState:UIControlStateNormal];

[self.view addSubview:btnTwo];

UIButton setImage forState example.
[button addTarget:self action:@selector(buttonTouchDown:) forControlEvents:UIControlEventTouchDown];
[button addTarget:self action:@selector(buttonTouchUp:) forControlEvents:UIControlEventTouchUpInside];


-(void)buttonTouchDown:(id)sender{
    UIButton *button=(UIButton *)sender;
    if(button.selected){
        [button setImage:[UIImage imageNamed:@"pressed.png"] forState:UIControlStateNormal];
    }
}

-(void)buttonTouchUp:(id)sender{
    UIButton *button=(UIButton *)sender;
    [button setImage:[UIImage imageNamed:@"normal.png"] forState:UIControlStateNormal];
}

End of UIButton setImage forState example article.

UIButton setImage example in Objective C (iOS).

UIButton setImage

Sets the image to use for the specified state.

- (void)setImage:(UIImage *)image forState:(UIControlState)state

Parameters of [UIButton setImage]
image
The image to use for the specified state.
state
The state that uses the specified title. The values are described in UIControlState.

Discussion of [UIButton setImage]
In general, if a property is not specified for a state, the default is to use the UIControlStateNormal value. If the UIControlStateNormal value is not set, then the property defaults to a system value. Therefore, at a minimum, you should set the value for the normal state.

UIButton setImage example.
UIImage *listImage = [UIImage imageNamed:@"list_icon.png"];
UIButton *listButton = [UIButton buttonWithType:UIButtonTypeCustom];

// get the image size and apply it to the button frame
CGRect listButtonFrame = listButton.frame;
listButtonFrame.size = listImage.size;
listButton.frame = listButtonFrame;

[listButton setImage:listImage forState:UIControlStateNormal];
[listButton addTarget:self.navigationController.parentViewController action:@selector(revealToggle:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *jobsButton = [[UIBarButtonItem alloc] initWithCustomView:listButton];

self.navigationItem.leftBarButtonItem = jobsButton;

Example of [UIButton setImage].
UIButton *btnTwo = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btnTwo.frame = CGRectMake(40, 140, 240, 30);
[btnTwo setTitle:@"vc2:v1" forState:UIControlStateNormal];
[btnTwo addTarget:self action:@selector(goToOne) forControlEvents:UIControlEventTouchUpInside];


[btnTwo setImage:[UIImage imageNamed:@"name.png"] forState:UIControlStateNormal];

//OR setting as background image

[btnTwo setBackgroundImage:[UIImage imageNamed:@"name.png"] forState:UIControlStateNormal];

[self.view addSubview:btnTwo];

UIButton setImage example.
[button addTarget:self action:@selector(buttonTouchDown:) forControlEvents:UIControlEventTouchDown];
[button addTarget:self action:@selector(buttonTouchUp:) forControlEvents:UIControlEventTouchUpInside];


-(void)buttonTouchDown:(id)sender{
    UIButton *button=(UIButton *)sender;
    if(button.selected){
        [button setImage:[UIImage imageNamed:@"pressed.png"] forState:UIControlStateNormal];
    }
}

-(void)buttonTouchUp:(id)sender{
    UIButton *button=(UIButton *)sender;
    [button setImage:[UIImage imageNamed:@"normal.png"] forState:UIControlStateNormal];
}

End of UIButton setImage example article.

UIButton setBackgroundImage forState example in Objective C (iOS).

UIButton setBackgroundImage forState

Sets the background image to use for the specified button state.

- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state

Parameters of [UIButton setBackgroundImage forState]
image
The background image to use for the specified state.
state
The state that uses the specified image. The values are described in UIControlState.

Discussion of [UIButton setBackgroundImage forState]
In general, if a property is not specified for a state, the default is to use the UIControlStateNormal value. If the UIControlStateNormal value is not set, then the property defaults to a system value. Therefore, at a minimum, you should set the value for the normal state.

UIButton setBackgroundImage forState example.
UIButton *btnClear = [[UIButton alloc] init];
btnClear = [[UIButton buttonWithType:UIButtonTypeCustom] retain];

btnClear.frame = CGRectMake(115, 200, 90, 40);

[btnClear setTitle:@"Clear" forState:UIControlStateNormal];

[btnClear setBackgroundImage:[UIImage imageNamed:@"blue_button.png"]
                            forState:UIControlStateNormal];

[btnClear addTarget:self action:@selector(clearAction:) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:btnClear];

Example of [UIButton setBackgroundImage forState].
+ (UIButton *)buttonWithTitle:(NSString *)title
                       target:(id)target
                     selector:(SEL)selector
                        frame:(CGRect)frame
                        image:(UIImage *)image
                 imagePressed:(UIImage *)imagePressed
                darkTextColor:(BOOL)darkTextColor
    {
   UIButton *button = [UIButton alloc] initWithFrame:frame;

   button.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
   button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;

   [button setTitle:title forState:UIControlStateNormal];
   [button setTitleColor:UIColor blackColor forState:UIControlStateNormal];

   UIImage *newImage = [image stretchableImageWithLeftCapWidth:12.0 topCapHeight:0.0];
   [button setBackgroundImage:newImage forState:UIControlStateNormal];

   UIImage *newPressedImage = [imagePressed stretchableImageWithLeftCapWidth:12.0 topCapHeight:0.0];
   [button setBackgroundImage:newPressedImage forState:UIControlStateHighlighted];

   [button addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];

   // in case the parent view draws with a custom color or gradient, use a transparent color
   button.backgroundColor = UIColor clearColor;

   return button;
}

UIButton setBackgroundImage forState example.
+ (id) customButtonWithImageNamed:(NSString *)imageName selectedImageNamed:(NSString *)selectedImageName leftCapWidth:(CGFloat)leftCapWidth edgeInsets:(UIEdgeInsets)edgeInsets title:(NSString *)title target:(id)target selector:(SEL)selector {

    UIButton* customButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [customButton addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
    customButton.titleLabel.font = [UIFont boldSystemFontOfSize:12.0f];
    customButton.titleLabel.shadowColor = [UIColor colorWithRed:0.0f/255.0f green:0.0f/255.0f blue:0.0f/255.0f alpha:0.25f];
    customButton.titleLabel.shadowOffset = CGSizeMake(0.0f, -1.0f);
    customButton.titleLabel.lineBreakMode = UILineBreakModeTailTruncation;
    customButton.titleEdgeInsets = edgeInsets;
    UIImage* navButtonBackgroundImage = [[UIImage imageNamed:imageName] stretchableImageWithLeftCapWidth:leftCapWidth topCapHeight:0.0f];
    UIImage* navButtonPressedBackgroundImage = [[UIImage imageNamed:selectedImageName] stretchableImageWithLeftCapWidth:leftCapWidth topCapHeight:0.0f];
    [customButton setBackgroundImage:navButtonBackgroundImage forState:UIControlStateNormal];
    [customButton setTitle:title forState:UIControlStateNormal];
    [customButton setBackgroundImage:navButtonPressedBackgroundImage forState:UIControlStateHighlighted];
    [customButton setBackgroundImage:navButtonPressedBackgroundImage forState:UIControlStateSelected];

    CGSize size = CGSizeMake(30.0f, 30.0f);
    if (title != nil) {
        size = [[NSString stringWithString:title] sizeWithFont:customButton.titleLabel.font];
    }
    customButton.frame = CGRectMake(0.0f, 0.0f, size.width + 20.0f, 30.0f);
    customButton.layer.shouldRasterize = YES;
    customButton.layer.rasterizationScale = [[UIScreen mainScreen] scale];
    return [[[UIBarButtonItem alloc] initWithCustomView:customButton] autorelease];
}

End of UIButton setBackgroundImage forState example article.

UIButton setBackgroundImage example in Objective C (iOS).

UIButton setBackgroundImage

Sets the background image to use for the specified button state.

- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state

Parameters
image
The background image to use for the specified state.
state
The state that uses the specified image. The values are described in UIControlState.

Discussion of [UIButton setBackgroundImage]
In general, if a property is not specified for a state, the default is to use the UIControlStateNormal value. If the UIControlStateNormal value is not set, then the property defaults to a system value. Therefore, at a minimum, you should set the value for the normal state.

UIButton setBackgroundImage example.
UIButton *btnClear = [[UIButton alloc] init];
btnClear = [[UIButton buttonWithType:UIButtonTypeCustom] retain];

btnClear.frame = CGRectMake(115, 200, 90, 40);

[btnClear setTitle:@"Clear" forState:UIControlStateNormal];

[btnClear setBackgroundImage:[UIImage imageNamed:@"blue_button.png"]
                            forState:UIControlStateNormal];

[btnClear addTarget:self action:@selector(clearAction:) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:btnClear];

Example of [UIButton setBackgroundImage].
+ (UIButton *)buttonWithTitle:(NSString *)title
                       target:(id)target
                     selector:(SEL)selector
                        frame:(CGRect)frame
                        image:(UIImage *)image
                 imagePressed:(UIImage *)imagePressed
                darkTextColor:(BOOL)darkTextColor
    {
   UIButton *button = [UIButton alloc] initWithFrame:frame;

   button.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
   button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;

   [button setTitle:title forState:UIControlStateNormal];
   [button setTitleColor:UIColor blackColor forState:UIControlStateNormal];

   UIImage *newImage = [image stretchableImageWithLeftCapWidth:12.0 topCapHeight:0.0];
   [button setBackgroundImage:newImage forState:UIControlStateNormal];

   UIImage *newPressedImage = [imagePressed stretchableImageWithLeftCapWidth:12.0 topCapHeight:0.0];
   [button setBackgroundImage:newPressedImage forState:UIControlStateHighlighted];

   [button addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];

   // in case the parent view draws with a custom color or gradient, use a transparent color
   button.backgroundColor = UIColor clearColor;

   return button;
}

UIButton setBackgroundImage example.
+ (id) customButtonWithImageNamed:(NSString *)imageName selectedImageNamed:(NSString *)selectedImageName leftCapWidth:(CGFloat)leftCapWidth edgeInsets:(UIEdgeInsets)edgeInsets title:(NSString *)title target:(id)target selector:(SEL)selector {

    UIButton* customButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [customButton addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
    customButton.titleLabel.font = [UIFont boldSystemFontOfSize:12.0f];
    customButton.titleLabel.shadowColor = [UIColor colorWithRed:0.0f/255.0f green:0.0f/255.0f blue:0.0f/255.0f alpha:0.25f];
    customButton.titleLabel.shadowOffset = CGSizeMake(0.0f, -1.0f);
    customButton.titleLabel.lineBreakMode = UILineBreakModeTailTruncation;
    customButton.titleEdgeInsets = edgeInsets;
    UIImage* navButtonBackgroundImage = [[UIImage imageNamed:imageName] stretchableImageWithLeftCapWidth:leftCapWidth topCapHeight:0.0f];
    UIImage* navButtonPressedBackgroundImage = [[UIImage imageNamed:selectedImageName] stretchableImageWithLeftCapWidth:leftCapWidth topCapHeight:0.0f];
    [customButton setBackgroundImage:navButtonBackgroundImage forState:UIControlStateNormal];
    [customButton setTitle:title forState:UIControlStateNormal];
    [customButton setBackgroundImage:navButtonPressedBackgroundImage forState:UIControlStateHighlighted];
    [customButton setBackgroundImage:navButtonPressedBackgroundImage forState:UIControlStateSelected];

    CGSize size = CGSizeMake(30.0f, 30.0f);
    if (title != nil) {
        size = [[NSString stringWithString:title] sizeWithFont:customButton.titleLabel.font];
    }
    customButton.frame = CGRectMake(0.0f, 0.0f, size.width + 20.0f, 30.0f);
    customButton.layer.shouldRasterize = YES;
    customButton.layer.rasterizationScale = [[UIScreen mainScreen] scale];
    return [[[UIBarButtonItem alloc] initWithCustomView:customButton] autorelease];
}

End of UIButton setBackgroundImage example article.

UIButton setAttributedTitle forState example in Objective C (iOS).

UIButton setAttributedTitle forState

Sets the styled title to use for the specified state.

- (void)setAttributedTitle:(NSAttributedString *)title forState:(UIControlState)state

Parameters of [UIButton setAttributedTitle forState]
title
The styled text string so use for the title.
state
The state that uses the specified title. The possible values are described in UIControlState.

Discussion of [UIButton setAttributedTitle forState]
Use this method to set the title of the button, including any relevant formatting information. If you set both a title and an attributed title for the button, the button prefers the use of the attributed title.

At a minimum, you should set the value for the normal state. If a title is not specified for a state, the default behavior is to use the title associated with the UIControlStateNormal state. If the value for UIControlStateNormal is not set, then the property defaults to a system value.

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

    // We want 2 lines for our buttons' title label
    [[self.button titleLabel] setNumberOfLines:2];

    // Setup the string
    NSMutableAttributedString *titleText = [[NSMutableAttributedString alloc] initWithString:@"This should be bold,\n and this should not."];

    // Set the font to bold from the beginning of the string to the ","
    [titleText addAttributes:[NSDictionary dictionaryWithObject:[UIFont boldSystemFontOfSize:14] forKey:NSFontAttributeName] range:NSMakeRange(0, 20)];

    // Normal font for the rest of the text
    [titleText addAttributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:14] forKey:NSFontAttributeName] range:NSMakeRange(20, 22)];

    // Set the attributed string as the buttons' title text
    [self.button setAttributedTitle:titleText forState:UIControlStateNormal];
}

Example of [UIButton setAttributedTitle forState].
NSMutableAttributedString *commentString = [[NSMutableAttributedString alloc] initWithString:@"The Quick Brown Fox"];

[commentString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:NSMakeRange(0, [commentString length])];

[button setAttributedTitle:commentString forState:UIControlStateNormal];

UIButton setAttributedTitle forState example.
NSString* infoString=@"This is an example of Attributed String";

    NSMutableAttributedString *attString=[[NSMutableAttributedString alloc] initWithString:infoString];
    NSInteger _stringLength=[infoString length];

    UIColor *_red=[UIColor redColor];
    UIFont *font=[UIFont fontWithName:@"Helvetica-Bold" size:20.0f];
    [attString addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, _stringLength)];
    [attString addAttribute:NSForegroundColorAttributeName value:_red range:NSMakeRange(0, _stringLength/2)];
    [self.attribButton setAttributedTitle:attString forState:UIControlStateNormal];
    self.attribButton.titleLabel.numberOfLines=2; 

End of UIButton setAttributedTitle forState example article.