Esqarrouth
Esqarrouth

Reputation: 39181

How to change alpha of UIButton while the buttons alpha also set

As seen on the picture I have 2 buttons with 0.5 alpha and 1 alpha. I want to change the alpha of the title in the first picture to 1. Is this possible?

So far I tried these which did not work:

button.titleLabel.alpha = 1;  
[button setTitleColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:1] forState:UIControlStateNormal];

enter image description here

UIButton *button = (UIButton *)view;
if(self.currentArray[index][2] != NULL) //before it was if (button == nil)
{
    NSString *name = [[NSString alloc] initWithString:self.currentArray[index][2]];
    UIImage *image = [UIImage imageNamed:self.currentArray[index][5]];
    button = [UIButton buttonWithType:UIButtonTypeCustom];

    int extraLeftInset = 0;
    if ([self.currentArray[index][6] isEqualToString:@"true"]) {
        //show it
    }else if([self.currentArray[index][7] isEqualToString:@"true"]){

    }else{
        UIImage *locked = [UIImage imageNamed:@"fruitify_locked.png"];
        button.imageEdgeInsets = UIEdgeInsetsMake(15, 15, 15, 15);
        [button setImage:locked forState:UIControlStateNormal];
        extraLeftInset = - 256; //size of locked
        button.backgroundColor = [UIColor clearColor];
        button.alpha = 0.5;
    }

    button.frame = CGRectMake(0.0, 0.0, 56, 56);
    button.tag = index;

    [button setBackgroundImage:image forState:UIControlStateNormal];

    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    UIEdgeInsets buttonInset = UIEdgeInsetsMake(30, -2 + extraLeftInset, -20, -2);
    [button setTitleEdgeInsets:buttonInset];
    [button setTitle:name forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize: 8];
    button.titleLabel.lineBreakMode   = NSLineBreakByWordWrapping;


    [button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
}

Upvotes: 0

Views: 1007

Answers (2)

rdelmar
rdelmar

Reputation: 104082

One way to do this would be to adjust the alpha value of your image. That can be done by passing your image to this method,

-(UIImage *)image:(UIImage *) image  withAdjustedAlpha:(CGFloat) newAlpha{
    UIGraphicsBeginImageContextWithOptions(image.size, NO, 0.0);
    [image drawAtPoint:CGPointZero blendMode:kCGBlendModeCopy alpha:newAlpha];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

Upvotes: 2

Duncan C
Duncan C

Reputation: 131398

The alpha setting affects a view and all it's subviews. If you set the alpha on a button, every part of the button will have that alpha value.

You have a couple of possible options.

You could create a custom subclass of UIButton that managed a UILabel that was a sibling view of the button in the view hierarchy. That would get messy in several ways however. The normal button title methods wouldn't work, and you would have to introduce your own control logic to remove the button label from the superview if the button was removed

You might also try manipulating the alpha property of the button's image view. (The buttons' imageView property is read-only, but you CAN still make changes to the attributes of the image view in that property. You would need to test to make sure that the button doesn't mess up the image view's alpha property when it swaps images for the different button states (highlighted, selected, etc.) You would probably also need to set the opaque property on the button to NO, and perhaps on the image view as well. (You'll need to experiment.)

BTW, I like you trick of offsetting the button title using setTitleEdgeInsets. I hadn't seen that before.

Upvotes: 0

Related Questions