Thursday, May 30, 2013

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.