uibutton class

32
UIButton Class Reference

Upload: adil054

Post on 10-Feb-2018

227 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: UIButton Class

7/22/2019 UIButton Class

http://slidepdf.com/reader/full/uibutton-class 1/32

UIButton Class Reference

Page 2: UIButton Class

7/22/2019 UIButton Class

http://slidepdf.com/reader/full/uibutton-class 2/32

Contents

UIButton Class Reference   4

Overview   4

Tasks   5

Creating Buttons   5

Configuring the Button Title   5

Configuring Button Presentation   5

Configuring Edge Insets   6

Getting the Current State   6

Getting Dimensions   7Deprecated Properties   7

Properties   8

adjustsImageWhenDisabled  8

adjustsImageWhenHighlighted  8

buttonType   8

contentEdgeInsets   9

currentAttributedTitle  9

currentBackgroundImage  10

currentImage   10currentTitle   11

currentTitleColor   11

currentTitleShadowColor  12

imageEdgeInsets   12

imageView   13

reversesTitleShadowWhenHighlighted   13

showsTouchWhenHighlighted   14

tintColor   14

titleEdgeInsets   15

titleLabel   15Class Methods   16

buttonWithType:   16

Instance Methods   17

attributedTitleForState:   17

backgroundImageForState:   17

backgroundRectForBounds:  18

2013-09-18 | Copyright © 2013 Apple Inc. All Rights Reserved.

2

Page 3: UIButton Class

7/22/2019 UIButton Class

http://slidepdf.com/reader/full/uibutton-class 3/32

contentRectForBounds:   19

imageForState:   19

imageRectForContentRect:  20

setAttributedTitle:forState:  20

setBackgroundImage:forState:  21

setImage:forState:   22

setTitle:forState:   22

setTitleColor:forState:  23

setTitleShadowColor:forState:  24

titleColorForState:  24

titleForState:   25

titleRectForContentRect:  26

titleShadowColorForState:   26

Constants   27UIButtonType   27

Deprecated UIButton Methods   29

Deprecated in iOS 3.0   29

font   29

lineBreakMode  29

titleShadowOffset  30

Document Revision History   31

2013-09-18 | Copyright © 2013 Apple Inc. All Rights Reserved.

3

Contents

Page 4: UIButton Class

7/22/2019 UIButton Class

http://slidepdf.com/reader/full/uibutton-class 4/32

Inherits from   UIControl : UIView : UIResponder : NSObject

Conforms to   NSCoding

NSCoding (UIView)

UIAppearance (UIView)

UIAppearanceContainer (UIView)

UIDynamicItem (UIView)

NSObject (NSObject)

Framework    /System/Library/Frameworks/UIKit.framework 

Availability   Available in iOS 2.0 and later.

Companion guide   “Buttons”

Declared in   UIButton.h

Related sample code   AddMusic

avTouch

Sampler Unit Presets (LoadPresetDemo)

SimpleNetworkStreams

UICatalog

OverviewAn instance of the UIButton class implements a buttonon the touch screen. A button intercepts touch events

and sends an action message to a target object when tapped. Methods for setting the target and action are

inherited from UIControl. This class provides methods for setting the title, image, and other appearance

properties of a button. By using these accessors, you can specify a different appearance for each button state.

For information about basic view behaviors, see View Programming Guide for iOS .

For more information about appearance and behavior configuration, see “Buttons”.

2013-09-18 | Copyright © 2013 Apple Inc. All Rights Reserved.

4

UIButton Class Reference

Page 5: UIButton Class

7/22/2019 UIButton Class

http://slidepdf.com/reader/full/uibutton-class 5/32

Tasks

Creating Buttons

+ buttonWithType: (page 16)Creates and returns a new button of the specified type.

Configuring the Button Title

  titleLabel (page 15)  property 

A view that displays the value of the currentTitle property for a button. (read-only)

– titleForState: (page 25)

Returns the title associated with the specified state.– setTitle:forState: (page 22)

Sets the title to use for the specified state.

– attributedTitleForState: (page 17)

Returns the styled title associated with the specified state.

– setAttributedTitle:forState: (page 20)

Sets the styled title to use for the specified state.

– titleColorForState: (page 24)

Returns the title color used for a state.

– setTitleColor:forState: (page 23)

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

– titleShadowColorForState: (page 26)

Returns the shadow color of the title used for a state.

– setTitleShadowColor:forState: (page 24)

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

  reversesTitleShadowWhenHighlighted (page 13)  property 

A Boolean value that determines whether the title shadow changes when the button is highlighted.

Configuring Button Presentation

  adjustsImageWhenHighlighted (page 8)  property 

A Boolean value that determines whether the image changes when the button is highlighted.

UIButton Class Reference

Tasks

2013-09-18 | Copyright © 2013 Apple Inc. All Rights Reserved.

5

Page 6: UIButton Class

7/22/2019 UIButton Class

http://slidepdf.com/reader/full/uibutton-class 6/32

  adjustsImageWhenDisabled (page 8)  property 

A Boolean value that determines whether the image changes when the button is disabled.

  showsTouchWhenHighlighted (page 14)  property 

A Boolean value that determines whether tapping the button causes it to glow.

– backgroundImageForState: (page 17)

Returns the background image used for a button state.

– imageForState: (page 19)

Returns the image used for a button state.

– setBackgroundImage:forState: (page 21)

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

– setImage:forState: (page 22)

Sets the image to use for the specified state.  tintColor (page 14)  property 

The tint color to apply to the button title and image.

Configuring Edge Insets

  contentEdgeInsets (page 9)  property 

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

  titleEdgeInsets (page 15)  property The inset or outset margins for the rectangle around the button’s title text.

  imageEdgeInsets (page 12)  property 

The inset or outset margins for the rectangle around the button’s image.

Getting the Current State

  buttonType (page 8)  property 

The button type. (read-only)

  currentTitle (page 11)  property 

The current title that is displayed on the button. (read-only)

  currentAttributedTitle (page 9)  property 

The current styled title that is displayed on the button. (read-only)

UIButton Class Reference

Tasks

2013-09-18 | Copyright © 2013 Apple Inc. All Rights Reserved.

6

Page 7: UIButton Class

7/22/2019 UIButton Class

http://slidepdf.com/reader/full/uibutton-class 7/32

  currentTitleColor (page 11)  property 

The color used to display the title. (read-only)

  currentTitleShadowColor (page 12)  property 

The color of the title’s shadow. (read-only)

  currentImage (page 10)  property 

The current image displayed on the button. (read-only)

  currentBackgroundImage (page 10)  property 

The current background image displayed on the button. (read-only)

  imageView (page 13)  property 

The button’s image view. (read-only)

Getting Dimensions

– backgroundRectForBounds: (page 18)

Returns the rectangle in which the receiver draws its background.

– contentRectForBounds: (page 19)

Returns the rectangle in which the receiver draws its entire content.

– titleRectForContentRect: (page 26)

Returns the rectangle in which the receiver draws its title.

– imageRectForContentRect: (page 20)

Returns the rectangle in which the receiver draws its image.

Deprecated Properties

  font (page 29)  property  Deprecated in iOS 3.0

The fontused todisplay text on the button. (Deprecated.  Use the font property of the titleLabel (page

15) instead.)

  lineBreakMode (page 29)  property  Deprecated in iOS 3.0

The line break mode to use when drawing text. (Deprecated.  Use the lineBreakMode property of thetitleLabel (page 15) instead.)

  titleShadowOffset (page 30)  property  Deprecated in iOS 3.0

The offset of the shadow used to display the receiver’s title. (Deprecated.  Use the shadowOffset

property of the titleLabel (page 15) instead.)

UIButton Class Reference

Tasks

2013-09-18 | Copyright © 2013 Apple Inc. All Rights Reserved.

7

Page 8: UIButton Class

7/22/2019 UIButton Class

http://slidepdf.com/reader/full/uibutton-class 8/32

Properties

adjustsImageWhenDisabled

 A Boolean value that determines whether the image changes when the button is disabled.

@property(nonatomic) BOOL adjustsImageWhenDisabled

Discussion

If  YES, the image is drawn darker when the button is disabled. The default value is YES.

Availability

Available in iOS 2.0 and later.

See Also

  @property adjustsImageWhenHighlighted  (page 8)

Declared in

UIButton.h

adjustsImageWhenHighlighted

 A Boolean value that determines whether the image changes when the button is highlighted.

@property(nonatomic) BOOL adjustsImageWhenHighlighted

Discussion

If  YES, the image is drawn lighter when the button is highlighted. The default value is  YES.

Availability

Available in iOS 2.0 and later.

See Also

  @property adjustsImageWhenDisabled  (page 8)

Declared in

UIButton.h

buttonType

The button type. (read-only)

UIButton Class Reference

Properties

2013-09-18 | Copyright © 2013 Apple Inc. All Rights Reserved.

8

Page 9: UIButton Class

7/22/2019 UIButton Class

http://slidepdf.com/reader/full/uibutton-class 9/32

@property(nonatomic, readonly) UIButtonType buttonType

Discussion

See UIButtonType (page 27) for the possible values.

AvailabilityAvailable in iOS 2.0 and later.

Declared in

UIButton.h

contentEdgeInsets

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

@property(nonatomic) UIEdgeInsets contentEdgeInsets

Discussion

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.

The button uses this property to determine  intrinsicContentSize and sizeThatFits:.

Availability

Available in iOS 2.0 and later.

See Also

  @property imageEdgeInsets  (page 12)

  @property titleEdgeInsets  (page 15)

Declared in

UIButton.h

currentAttributedTitle

The current styled title that is displayed on the button. (read-only)

UIButton Class Reference

Properties

2013-09-18 | Copyright © 2013 Apple Inc. All Rights Reserved.

9

Page 10: UIButton Class

7/22/2019 UIButton Class

http://slidepdf.com/reader/full/uibutton-class 10/32

@property(nonatomic, readonly, retain) NSAttributedString *currentAttributedTitle

Discussion

The value for this property reflects the title associated with the control’s current state. For states that do not

have a custom title string associated with them, this method returns the attributed title that is currentlydisplayed, which is typically the one associated with the  UIControlStateNormal state.

Availability

Available in iOS 6.0 and later.

Declared in

UIButton.h

currentBackgroundImage

The current background image displayed on the button. (read-only)

@property(nonatomic, readonly, retain) UIImage *currentBackgroundImage

Discussion

This value can be nil.

Availability

Available in iOS 2.0 and later.

See Also  @property currentImage  (page 10)

Declared in

UIButton.h

currentImage

The current image displayed on the button. (read-only)

@property(nonatomic, readonly, retain) UIImage *currentImage

Discussion

This value can be nil.

Availability

Available in iOS 2.0 and later.

UIButton Class Reference

Properties

2013-09-18 | Copyright © 2013 Apple Inc. All Rights Reserved.

10

Page 11: UIButton Class

7/22/2019 UIButton Class

http://slidepdf.com/reader/full/uibutton-class 11/32

See Also

  @property currentBackgroundImage  (page 10)

Declared in

UIButton.h

currentTitle

The current title that is displayed on the button. (read-only)

@property(nonatomic, readonly, retain) NSString *currentTitle

Discussion

The value for this property is set automatically whenever the button state changes. For states that do not have

a custom title string associated with them, this method returns the title that is currently displayed, which is

typically the one associated with the   UIControlStateNormal state. The value may be nil.

Availability

Available in iOS 2.0 and later.

See Also

– setTitle:forState: (page 22)

  @property currentTitleColor  (page 11)

  @property currentTitleShadowColor  (page 12)

  @property titleLabel  (page 15)

Declared in

UIButton.h

currentTitleColor

The color used to display the title. (read-only)

@property(nonatomic, readonly, retain) UIColor *currentTitleColor

Discussion

This value is guaranteed not to be nil. The default value is white.

Availability

Available in iOS 2.0 and later.

See Also

  @property currentTitle  (page 11)

UIButton Class Reference

Properties

2013-09-18 | Copyright © 2013 Apple Inc. All Rights Reserved.

11

Page 12: UIButton Class

7/22/2019 UIButton Class

http://slidepdf.com/reader/full/uibutton-class 12/32

  @property currentTitleShadowColor  (page 12)

Related Sample CodeGKTapper

Declared inUIButton.h

currentTitleShadowColor

The color of the title’s shadow. (read-only)

@property(nonatomic, readonly, retain) UIColor *currentTitleShadowColor

Discussion

The default value is white.

Availability

Available in iOS 2.0 and later.

See Also

  @property currentTitle  (page 11)

  @property currentTitleColor  (page 11)

Declared in

UIButton.h

imageEdgeInsets

The inset or outset margins for the rectangle around the button’s image.

@property(nonatomic) UIEdgeInsets imageEdgeInsets

Discussion

Use this property to resize andreposition the effective drawing rectangle for thebutton image.You canspecify

a different value for each of the four insets (top, left, bottom, right). A positive value shrinks, or insets, thatedge—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 .

This property is used only for positioning the image during layout. The button does not use this property to

determine  intrinsicContentSize and sizeThatFits:.

UIButton Class Reference

Properties

2013-09-18 | Copyright © 2013 Apple Inc. All Rights Reserved.

12

Page 13: UIButton Class

7/22/2019 UIButton Class

http://slidepdf.com/reader/full/uibutton-class 13/32

Page 14: UIButton Class

7/22/2019 UIButton Class

http://slidepdf.com/reader/full/uibutton-class 14/32

Availability

Available in iOS 2.0 and later.

Declared in

UIButton.h

showsTouchWhenHighlighted

 A Boolean value that determines whether tapping the button causes it to glow.

@property(nonatomic) BOOL showsTouchWhenHighlighted

Discussion

If  YES, the button glows when tapped; otherwise, it does not. The image and button behavior is not changed

by the glow. The default value is  NO.

Availability

Available in iOS 2.0 and later.

See Also

  @property adjustsImageWhenHighlighted  (page 8)

Declared in

UIButton.h

tintColor

The tint color to apply to the button title and image.

@property(nonatomic, retain) UIColor *tintColor

Discussion

In iOSv7.0, all subclasses of UIView derive their behavior for tintColor from the base class. Seethe discussion

of  tintColor at the UIView level for more information.

This property has no default effect for buttons with type UIButtonTypeCustom (page 27). For custom buttons,you must implement any behavior related to  tintColor (page 14) yourself.

Availability

Available in iOS 5.0 and later.

Declared in

UIButton.h

UIButton Class Reference

Properties

2013-09-18 | Copyright © 2013 Apple Inc. All Rights Reserved.

14

Page 15: UIButton Class

7/22/2019 UIButton Class

http://slidepdf.com/reader/full/uibutton-class 15/32

titleEdgeInsets

The inset or outset margins for the rectangle around the button’s title text.

@property(nonatomic) UIEdgeInsets titleEdgeInsets

Discussion

Use this property to resize and reposition the effective drawing rectangle for the 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 .

The insets you specify are applied to the title rectangle after that rectangle has been sized to fit the button’s

text. Thus, positive inset values may actually clip the title text.

This property is used only for positioning the title during layout. The button does not use this property to

determine  intrinsicContentSize and sizeThatFits:.

Availability

Available in iOS 2.0 and later.

See Also

  @property contentEdgeInsets  (page 9)

  @property imageEdgeInsets  (page 12)

Declared in

UIButton.h

titleLabel

 A view that displays the value of the currentTitle property for a button. (read-only)

@property(nonatomic, readonly, retain) UILabel *titleLabel

Discussion

Although this property is read-only, its ownpropertiesare read/write. Usethese propertiesprimarily to configure

the text of the button. For example:

UIButton *button = [UIButton buttonWithType: UIButtonTypeSystem];

button.titleLabel.font = [UIFont systemFontOfSize: 12];

button.titleLabel.lineBreakMode = UILineBreakModeTailTruncation;

UIButton Class Reference

Properties

2013-09-18 | Copyright © 2013 Apple Inc. All Rights Reserved.

15

Page 16: UIButton Class

7/22/2019 UIButton Class

http://slidepdf.com/reader/full/uibutton-class 16/32

Do not use the label object to set the text color or the shadow color. Instead, use the

setTitleColor:forState: (page 23) and setTitleShadowColor:forState: (page 24) methods of this class

to make those changes.

The titleLabel property returns a value even if the button has not been displayed yet. The value of theproperty is nil for system buttons.

Availability

Available in iOS 3.0 and later.

See Also

  @property currentTitle  (page 11)

Related Sample CodeGKTapper

Declared in

UIButton.h

Class Methods

buttonWithType:

Creates and returns a new button of the specified type.

+ (id)buttonWithType:(UIButtonType)buttonType

Parameters

buttonType

The button type. See  UIButtonType (page 27) for the possible values.

Return Value

A newly created button.

Discussion

This method is a convenience constructor for creating buttonobjectswith specific configurations. If yousubclass

UIButton, this method does not return an instance of your subclass. If you want to create an instance of a

specific subclass, you must alloc/init the button directly.

When creating a custom button—that is a button with the type  UIButtonTypeCustom (page 27)—the frame

of the button is set to (0, 0, 0, 0) initially. Before adding the button to your interface, you should update the

frame to a more appropriate value.

UIButton Class Reference

Class Methods

2013-09-18 | Copyright © 2013 Apple Inc. All Rights Reserved.

16

Page 17: UIButton Class

7/22/2019 UIButton Class

http://slidepdf.com/reader/full/uibutton-class 17/32

Availability

Available in iOS 2.0 and later.

Related Sample CodeAccessory

iPhoneMixerEQGraphTest

iPhoneMultichannelMixerTest

UICatalog

XMLPerformance

Declared in

UIButton.h

Instance Methods

attributedTitleForState:

Returns the styled title associated with the specified state.

- (NSAttributedString *)attributedTitleForState: (UIControlState)state

Parameters

state

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

Return Value

The title for the specified state. If no attributed title has been set for the specific state, this method returns the

attributed title associated with the   UIControlStateNormal state. If no attributed title has been set for

UIControlStateNormal , returns nil.

Availability

Available in iOS 6.0 and later.

Declared in

UIButton.h

backgroundImageForState:

Returns the background image used for a button state.

- (UIImage *)backgroundImageForState: (UIControlState)state

UIButton Class Reference

Instance Methods

2013-09-18 | Copyright © 2013 Apple Inc. All Rights Reserved.

17

Page 18: UIButton Class

7/22/2019 UIButton Class

http://slidepdf.com/reader/full/uibutton-class 18/32

Parameters

state

The state that uses the background image. Possible values are described in   UIControlState.

Return ValueThe background image used for the specified state.

Availability

Available in iOS 2.0 and later.

See Also

– setBackgroundImage:forState: (page 21)

Declared in

UIButton.h

backgroundRectForBounds:

Returns the rectangle in which the receiver draws its background.

- (CGRect)backgroundRectForBounds: (CGRect)bounds

Parameters

bounds

The bounding rectangle of the receiver.

Return Value

The bounds rectangle in which to draw any standard button content.

Discussion

The default implementation of this methodreturns thevalue in the bounds parameter. This rectangle represents

the area in which thebuttondraws itsstandard background content. Subclasses that provide custombackground

adornments can override this method and return a modified bounds rectangle to prevent the button from

drawing over any custom content.

AvailabilityAvailable in iOS 2.0 and later.

See Also

– contentRectForBounds: (page 19)

Declared in

UIButton.h

UIButton Class Reference

Instance Methods

2013-09-18 | Copyright © 2013 Apple Inc. All Rights Reserved.

18

Page 19: UIButton Class

7/22/2019 UIButton Class

http://slidepdf.com/reader/full/uibutton-class 19/32

contentRectForBounds:

Returns the rectangle in which the receiver draws its entire content.

- (CGRect)contentRectForBounds:(CGRect)bounds

Parameters

bounds

The bounding rectangle for the receiver.

Return Value

The rectangle in which the receiver draws its entire content.

Discussion

The content rectangle is thearea neededto display the image and title includingany padding andadjustments

for alignment and other settings.

Availability

Available in iOS 2.0 and later.

See Also

  @property contentEdgeInsets  (page 9)

– backgroundRectForBounds: (page 18)

Declared in

UIButton.h

imageForState:

Returns the image used for a button state.

- (UIImage *)imageForState:(UIControlState)state

Parameters

state

The state that uses the image. Possible values are described in  UIControlState.

Return Value

The image used for the specified state.

Availability

Available in iOS 2.0 and later.

UIButton Class Reference

Instance Methods

2013-09-18 | Copyright © 2013 Apple Inc. All Rights Reserved.

19

Page 20: UIButton Class

7/22/2019 UIButton Class

http://slidepdf.com/reader/full/uibutton-class 20/32

See Also

– setImage:forState: (page 22)

Declared in

UIButton.h

imageRectForContentRect:

Returns the rectangle in which the receiver draws its image.

- (CGRect)imageRectForContentRect: (CGRect)contentRect

Parameters

contentRect

The content rectangle for the receiver.

Return Value

The rectangle in which the receiver draws its image.

Availability

Available in iOS 2.0 and later.

See Also

– contentRectForBounds: (page 19)

– titleRectForContentRect: (page 26)

Declared in

UIButton.h

setAttributedTitle:forState:

Sets the styled title to use for the specified state.

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

Parameterstitle

The styled text string so use for the title.

state

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

UIButton Class Reference

Instance Methods

2013-09-18 | Copyright © 2013 Apple Inc. All Rights Reserved.

20

Page 21: UIButton Class

7/22/2019 UIButton Class

http://slidepdf.com/reader/full/uibutton-class 21/32

Discussion

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 forUIControlStateNormal  is not set, then the property defaults to a system value.

Availability

Available in iOS 6.0 and later.

Declared in

UIButton.h

setBackgroundImage:forState:

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

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.

Availability

Available in iOS 2.0 and later.

See Also

– backgroundImageForState: (page 17)

Related Sample CodeAccessory

AddMusic

UICatalog

UIButton Class Reference

Instance Methods

2013-09-18 | Copyright © 2013 Apple Inc. All Rights Reserved.

21

Page 22: UIButton Class

7/22/2019 UIButton Class

http://slidepdf.com/reader/full/uibutton-class 22/32

Declared in

UIButton.h

setImage:forState:

Sets the image to use for the specified state.

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

Parameters

image

The image to use for the specified state.

state

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

Discussion

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.

Availability

Available in iOS 2.0 and later.

See Also

– imageForState: (page 19)

Related Sample CodeRegions

WiTap

Declared in

UIButton.h

setTitle:forState:

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.

UIButton Class Reference

Instance Methods

2013-09-18 | Copyright © 2013 Apple Inc. All Rights Reserved.

22

Page 23: UIButton Class

7/22/2019 UIButton Class

http://slidepdf.com/reader/full/uibutton-class 23/32

state

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

Discussion

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.

Availability

Available in iOS 2.0 and later.

See Also– titleForState: (page 25)

Related Sample CodeUICatalog

XMLPerformance

Declared in

UIButton.h

setTitleColor:forState:

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

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 Class Reference

Instance Methods

2013-09-18 | Copyright © 2013 Apple Inc. All Rights Reserved.

23

Page 24: UIButton Class

7/22/2019 UIButton Class

http://slidepdf.com/reader/full/uibutton-class 24/32

Page 25: UIButton Class

7/22/2019 UIButton Class

http://slidepdf.com/reader/full/uibutton-class 25/32

- (UIColor *)titleColorForState:(UIControlState)state

Parameters

state

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

Return Value

The color of the title for the specified state.

Availability

Available in iOS 2.0 and later.

See Also

– setTitleColor:forState: (page 23)

Declared in

UIButton.h

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

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.

Availability

Available in iOS 2.0 and later.

See Also

– setTitle:forState: (page 22)

Declared in

UIButton.h

UIButton Class Reference

Instance Methods

2013-09-18 | Copyright © 2013 Apple Inc. All Rights Reserved.

25

Page 26: UIButton Class

7/22/2019 UIButton Class

http://slidepdf.com/reader/full/uibutton-class 26/32

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

The rectangle in which the receiver draws its title.

Availability

Available in iOS 2.0 and later.

See Also

– contentRectForBounds: (page 19)

– imageRectForContentRect: (page 20)

Declared in

UIButton.h

titleShadowColorForState:

Returns the shadow color of the title used for a state.

- (UIColor *)titleShadowColorForState: (UIControlState)state

Parameters

state

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

Return Value

The color of the title’s shadow for the specified state.

Availability

Available in iOS 2.0 and later.

See Also

– setTitleShadowColor:forState: (page 24)

Declared in

UIButton.h

UIButton Class Reference

Instance Methods

2013-09-18 | Copyright © 2013 Apple Inc. All Rights Reserved.

26

Page 27: UIButton Class

7/22/2019 UIButton Class

http://slidepdf.com/reader/full/uibutton-class 27/32

Constants

UIButtonType

Specifies the style of a button.

typedef enum {

UIButtonTypeCustom = 0,

UIButtonTypeSystem,

UIButtonTypeDetailDisclosure,

UIButtonTypeInfoLight,

UIButtonTypeInfoDark,

UIButtonTypeContactAdd,

UIButtonTypeRoundedRect,

} UIButtonType;

Constants

UIButtonTypeCustom

No button style.

Available in iOS 2.0 and later.

Declared in UIButton.h.

UIButtonTypeSystem

A system style button, such as those shown in navigation bars and toolbars.

Available in iOS 7.0 and later.

Declared in UIButton.h.

UIButtonTypeDetailDisclosure

A detail disclosure button.

Available in iOS 2.0 and later.

Declared in UIButton.h.

UIButtonTypeInfoLight

An information button that has a light background.

Available in iOS 2.0 and later.

Declared in UIButton.h.

UIButtonTypeInfoDarkAn information button that has a dark background.

Available in iOS 2.0 and later.

Declared in UIButton.h.

UIButton Class Reference

Constants

2013-09-18 | Copyright © 2013 Apple Inc. All Rights Reserved.

27

Page 28: UIButton Class

7/22/2019 UIButton Class

http://slidepdf.com/reader/full/uibutton-class 28/32

Page 29: UIButton Class

7/22/2019 UIButton Class

http://slidepdf.com/reader/full/uibutton-class 29/32

A method identified as deprecated has been superseded and may become unsupported in the future.

Deprecated in iOS 3.0

font

Thefontusedtodisplaytextonthebutton.( DeprecatediniOS3.0. Usethefont

 property of thetitleLabel

 (page15) instead.)

@property(nonatomic, retain) UIFont *font

Discussion

If  nil, a system font is used. The default value is nil.

Availability

Available in iOS 2.0 and later.

Deprecated in iOS 3.0.

See Also

  @property titleLabel  (page 15)

Declared in

UIButton.h

lineBreakMode

The line break mode to use when drawing text. ( Deprecated in iOS 3.0. Use the lineBreakMode property of thetitleLabel  (page 15) instead.)

@property(nonatomic) NSLineBreakMode lineBreakMode

Discussion

This property is one of the constants described in the  UILineBreakMode enumeration in NSString UIKit 

 Additions Reference . The default value is  UILineBreakModeMiddleTruncation.

2013-09-18 | Copyright © 2013 Apple Inc. All Rights Reserved.

29

Deprecated UIButton Methods

Page 30: UIButton Class

7/22/2019 UIButton Class

http://slidepdf.com/reader/full/uibutton-class 30/32

Availability

Available in iOS 2.0 and later.

Deprecated in iOS 3.0.

See Also

  @property titleLabel  (page 15)

Declared in

UIButton.h

titleShadowOffset

The offset of the shadow used to display the receiver’s title. ( Deprecated in iOS 3.0. Use the  shadowOffset

 property of the  titleLabel  (page 15) instead.)

@property(nonatomic) CGSize titleShadowOffset

Discussion

The horizontal and vertical offset values, specified using the  width and height fields of the CGSize data

type. Positive values always extend up and to the right from the user's perspective. The default value is

CGSizeZero.

Availability

Available in iOS 2.0 and later.

Deprecated in iOS 3.0.

See Also

  @property titleLabel  (page 15)

Declared in

UIButton.h

Deprecated UIButton Methods

Deprecated in iOS 3.0

2013-09-18 | Copyright © 2013 Apple Inc. All Rights Reserved.

30

Page 31: UIButton Class

7/22/2019 UIButton Class

http://slidepdf.com/reader/full/uibutton-class 31/32

This table describes the changes to UIButton Class Reference .

NotesDate

Added description for the UIButtonTypeSystem property, new in iOS 7;

clarified the return value description for the attributedTitleForState:

method.

2013-09-18

Clarifiedthereturnvaluedescription in attributedTitleForState: (page17).

Changed the code snippets in the descriptions of the imageView (page

13) and titleLabel (page 15) properties to employ the

UIButtonTypeSystem (page 27) constant, new in iOS 7.

Added a new interfaces related to styled-text support in iOS 6.2012-09-19

Updated for iOS 5.0.2011-10-12

Clarified descriptions of the contentEdgeInsets (page 9),

titleEdgeInsets (page 15), and imageEdgeInsets (page 12) properties.

2009-09-09

Other minor improvements.

Updated for iOS 3.0.2009-04-23

Added descriptions for the titleLabel (page 15) and imageView (page

13) properties. Minor corrections.

New document that describes the class used to implement buttons on

the touch screen.

2008-05-23

2013-09-18 | Copyright © 2013 Apple Inc. All Rights Reserved.

31

Document Revision History

Page 32: UIButton Class

7/22/2019 UIButton Class

http://slidepdf.com/reader/full/uibutton-class 32/32

Apple Inc.

Copyright © 2013 Apple Inc.

All rights reserved.

No part of this publication may be reproduced,

storedin a retrievalsystem, or transmitted, in any

form or by any means, mechanical, electronic,photocopying, recording, or otherwise, without

prior written permission of Apple Inc., with the

following exceptions: Any person is herebyauthorized to store documentation on a single

computer for personal use only and to print

copies of documentation for personal useprovided that the documentation contains

Apple’s copyright notice.

No licenses, express or implied, are granted with

respect to anyof thetechnology describedin this

document. Apple retains all intellectual propertyrights associated with the technology described

in this document. This document is intended to

assist application developers to develop

applications only for Apple-labeled computers.

Apple Inc.1 Infinite Loop

Cupertino, CA 95014

408-996-1010

Apple, the Apple logo, and iPhone aretrademarks

of Apple Inc., registered in the U.S. and other

countries.

iOS is a trademark or registered trademark of Cisco in the U.S. and other countries and is used

under license.

Even though Apple has reviewed this document,APPLE MAKES NO WARRANTY OR REPRESENTATION,EITHER EXPRESS OR IMPLIED,WITH RESPECT TO THIS

DOCUMENT, ITS QUALITY, ACCURACY,MERCHANTABILITY, OR FITNESS FOR A PARTICULARPURPOSE. AS A RESULT,THIS DOCUMENT IS PROVIDED“AS IS,” AND YOU, THE READER, ARE ASSUMING THEENTIRE RISK AS TO ITS QUALITY AND ACCURACY.

IN NO EVENT WILL APPLE BE LIABLE FOR DIRECT,INDIRECT, SPECIAL,INCIDENTAL,OR CONSEQUENTIALDAMAGES RESULTING FROM ANY DEFECT ORINACCURACY IN THIS DOCUMENT, even if advised of the possibility of such damages.

THE WARRANTY AND REMEDIES SET FORTH ABOVEARE EXCLUSIVE AND IN LIEU OF ALL OTHERS, ORALOR WRITTEN, EXPRESS OR IMPLIED. No Apple dealer,agent, or employee is authorized to make anymodification, extension,or addition to this warranty.

Some states do not allow the exclusion or limitationof implied warranties or liability for incidental orconsequential damages, so the above limitation orexclusion may not apply to you. This warranty givesyou specific legal rights, andyou mayalsohaveother

rights which vary from state to state.