# CCButton Class Reference

Inherits from | CCControl : CCNode : CCResponder : NSObject
Declared in | CCButton.h

## Overview

The CCButton represents a button on the screen. The button is presented with a stretchable background image and/or a title label. Different images, colors and opacity can be set for each of the buttons different states.

Methods for setting callbacks for the button is inherited from CCControl through the setTarget:selector: method or the block property.

## Tasks

### Creating Buttons

- `+ buttonWithTitle:`
- `+ buttonWithTitle:fontName:fontSize:`
- `+ buttonWithTitle:spriteFrame:`
- `+ buttonWithTitle:spriteFrame:highlightedSpriteFrame:disabledSpriteFrame:`
- `– initWithTitle:`
- `– initWithTitle:fontName:fontSize:`
- `– initWithTitle:spriteFrame:`
- `– initWithTitle:spriteFrame:highlightedSpriteFrame:disabledSpriteFrame:`
- `– setBackgroundColor:forState:`
- `– backgroundColorForState:`
- `– setBackgroundOpacity:forState:`
- `– backgroundOpacityForState:`
- `– setColor:`
- `– setLabelColor:forState:`
- `– labelColorForState:`
- `– setLabelOpacity:forState:`
- `– labelOpacityForState:`
- `– setBackgroundSpriteFrame:forState:`
- `– backgroundSpriteFrameForState:`

## Class Methods

### buttonWithTitle:

Creates a new button with a title and no background. Uses default font and font size.

`+ (id)buttonWithTitle:(NSString *)title`

#### Parameters

title

  The title text of the button.

#### Return Value

A new button.

#### Declared In

`CCButton.h`

### buttonWithTitle:fontName:fontSize:

Creates a new button with a title and no background.

`+ (id)buttonWithTitle:(NSString *)title fontName:(NSString *)fontName fontSize:(float)size`

#### Parameters

title

  The title text of the button.

fontName

  Name of the TTF font to use for the title label.

size

  Font size for the title label.

#### Return Value

A new button.

#### Declared In

`CCButton.h`

### buttonWithTitle:spriteFrame:

Creates a new button with the specified title for the label and sprite frame for its background.

`+ (id)buttonWithTitle:(NSString *)title spriteFrame:(CCSpriteFrame *)spriteFrame`

#### Parameters

title

  The title text of the button.

spriteFrame

  Stretchable background image.

#### Return Value

A new button.

#### Declared In

`CCButton.h`

### buttonWithTitle:spriteFrame:highlightedSpriteFrame:disabledSpriteFrame:

Creates a new button with the speicified title for the label, sprite frames for its background in different states.

`+ (id)buttonWithTitle:(NSString *)title spriteFrame:(CCSpriteFrame *)spriteFrame highlightedSpriteFrame:(CCSpriteFrame *)highlighted disabledSpriteFrame:(CCSpriteFrame *)disabled`

#### Parameters

title

  The title text of the button.

spriteFrame

  Stretchable background image for the normal state.

highlighted

  Stretchable background image for the highlighted state.

disabled

  Stretchable background image for the disabled state.

#### Return Value

A new button.

#### Declared In

`CCButton.h`

## Instance Methods

### backgroundColorForState:

Gets the background color for the specified state.

`- (CCColor *)backgroundColorForState:(CCControlState)state`

#### Parameters

state

  State to get the color for.

#### Return Value

Background color.

#### Declared In

`CCButton.h`

### backgroundOpacityForState:

Gets the background opacity for the specified state.

`- (CGFloat)backgroundOpacityForState:(CCControlState)state`

#### Parameters

state

  State to get the opacity for.

#### Return Value

Opacity.

#### Declared In

`CCButton.h`

### backgroundSpriteFrameForState:

Gets the background’s sprite frame for the specified state.

`- (CCSpriteFrame *)backgroundSpriteFrameForState:(CCControlState)state`

#### Parameters

state

  State to get the sprite frame for.

#### Return Value

Background sprite frame.

#### Declared In

`CCButton.h`

### initWithTitle:

Initializes a new button with a title and no background. Uses default font and font size.

`- (id)initWithTitle:(NSString *)title`

#### Parameters

title

  The title text of the button.

#### Return Value

A new button.

#### Declared In

`CCButton.h`

### initWithTitle:fontName:fontSize:

Initializes a new button with a title and no background.

`- (id)initWithTitle:(NSString *)title fontName:(NSString *)fontName fontSize:(float)size`

#### Parameters

title

  The title text of the button.

fontName

  Name of the TTF font to use for the title label.

size

  Font size for the title label.

#### Return Value

A new button.

#### Declared In

`CCButton.h`

### initWithTitle:spriteFrame:

Initializes a new button with the specified title for the label and sprite frame for its background.

`- (id)initWithTitle:(NSString *)title spriteFrame:(CCSpriteFrame *)spriteFrame`

#### Parameters

title

  The title text of the button.

spriteFrame

  Stretchable background image.

#### Return Value

A new button.

#### Declared In

`CCButton.h`

### initWithTitle:spriteFrame:highlightedSpriteFrame:disabledSpriteFrame:

Initializes a new button with the speicified title for the label, sprite frames for its background in different states.

`- (id)initWithTitle:(NSString *)title spriteFrame:(CCSpriteFrame *)spriteFrame highlightedSpriteFrame:(CCSpriteFrame *)highlighted disabledSpriteFrame:(CCSpriteFrame *)disabled`

#### Parameters

title

  The title text of the button.

spriteFrame

  Stretchable background image for the normal state.

highlighted

  Stretchable background image for the highlighted state.

disabled

  Stretchable background image for the disabled state.

#### Return Value

A new button.

#### Declared In

`CCButton.h`

### labelColorForState:

Gets the label’s color for the specified state.

`- (CCColor *)labelColorForState:(CCControlState)state`

#### Parameters

state

  State to get the color for.

#### Return Value

Label color.

#### Declared In

`CCButton.h`

### labelOpacityForState:

Gets the label’s opacity for the specified state.

`- (CGFloat)labelOpacityForState:(CCControlState)state`

#### Parameters

state

  State to get the opacity for.

#### Return Value

Label opacity.

#### Declared In

`CCButton.h`

### setBackgroundColor:forState:

Sets the background color for the specified state. The color is multiplied into the background sprite frame.

`- (void)setBackgroundColor:(CCColor *)color forState:(CCControlState)state`

#### Parameters

color

  Color applied to background image.

state

  State to apply the color to.

#### Declared In

`CCButton.h`

### setBackgroundOpacity:forState:

Sets the background’s opacity for the specified state.

`- (void)setBackgroundOpacity:(CGFloat)opacity forState:(CCControlState)state`

#### Parameters

opacity

  Opacity to apply to the background image

state

  State to apply the opacity to.

#### Declared In

`CCButton.h`

### setBackgroundSpriteFrame:forState:

Sets the background’s sprite frame for the specified state. The sprite frame will be stretched to the preferred size of the label. If set to `NULL` no background will be drawn.

`- (void)setBackgroundSpriteFrame:(CCSpriteFrame *)spriteFrame forState:(CCControlState)state`

#### Parameters

spriteFrame

  Sprite frame to use for drawing the background.

state

  State to set the background for.

#### Declared In

`CCButton.h`

### setColor:

Will set the label’s color for the normal state.

`- (void)setColor:(CCColor *)color`

#### Parameters

color

  Color applied to the label.

#### Declared In

`CCButton.h`

### setLabelColor:forState:

Sets the label’s color for the specified state.

`- (void)setLabelColor:(CCColor *)color forState:(CCControlState)state`

#### Parameters

color

  Color applied to the label.

state

  State to set the color for.

#### Declared In

`CCButton.h`

### setLabelOpacity:forState:

Sets the label’s opacity for the specified state.

`- (void)setLabelOpacity:(CGFloat)opacity forState:(CCControlState)state`

#### Parameters

opacity

  Opacity applied to the label.

state

  State to set the opacity for.

#### Declared In

`CCButton.h`