Showing posts with label setTitleColor. Show all posts
Showing posts with label setTitleColor. Show all posts

Thursday, May 30, 2013

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.

Wednesday, April 17, 2013

UIButton setTitleColor forState example objc


UIButton 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 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.


Example for (UIButton setTitleColor forState )
{
    NSString            *title = @"My button title";
    UIColor              *titleColor = [UIColor colorWithWhite:0.5f alpha:1.0f];
    UIImage             *bgImage = [UIImage imageNamed:@"yourImage.png"];

    UIImage             *highlightedBgImage = [UIImage imageNamed:@"yourImage.png"];
    UIButton            *button = [UIButton buttonWithType:UIButtonTypeCustom];

    
    // Create button.
    //
    button.bounds = CGRectMake0.0f0.0f, bgImage.size.width, bgImage.size.height );
    button.titleLabel.font = [UIFont fontWithName:@"HelveticaNeue-Medium" size:14.0f];
    button.titleLabel.adjustsFontSizeToFitWidth = YES;
    button.titleLabel.minimumFontSize = 10.0f;
    [button setTitleColor:titleColor forState:UIControlStateNormal];
    [button setTitleColor:titleColor forState:UIControlStateHighlighted];
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitle:title forState:UIControlStateHighlighted];
    [button setBackgroundImage:bgImage forState:UIControlStateNormal];
    [button setBackgroundImage:highlightedBgImage forState:UIControlStateHighlighted];

    // You must specify your target object and action selector here.
    // 
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];

}