iNiko84
iNiko84

Reputation: 115

Border around text UIButton

I've a question about UIButton in iOS. I use Apple UIButton from library and I need that it, when is pressed become "selected" and its background is blue. I make anything and it 's working correctly, but around the "label" of title my color is more dark. I try to set label background to clearColor but it isn't a color of label and I don't know which button subview is. Any idea? I use Objective-C, but if anyone has idea about Swift I understand too.

Thanks in advance

enter image description here

Upvotes: 2

Views: 605

Answers (1)

Sakir Sherasiya
Sakir Sherasiya

Reputation: 1572

Write this code in

viewDidLoad()

    self.btnDemo.layer.borderWidth = 1.0;
    self.btnDemo.layer.borderColor = [UIColor grayColor].CGColor;
    [self.btnDemo setTitle:@"ESTERNO" forState:UIControlStateNormal];
    [self.btnDemo setTitle:@"ES" forState:UIControlStateHighlighted];
    [self.btnDemo setBackgroundImage:[self imageWithColor:[UIColor clearColor]] forState:UIControlStateNormal];
    [self.btnDemo setBackgroundImage:[self imageWithColor:[UIColor colorWithRed:0 green:0 blue:255 alpha:1.0]] forState:UIControlStateHighlighted];
    [self.btnDemo setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.btnDemo setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];

Here btnDemo is UiButton outlet

  • Below method for convert Color to image

    - (UIImage *)imageWithColor:(UIColor *)color {
        CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
        UIGraphicsBeginImageContext(rect.size);
        CGContextRef context = UIGraphicsGetCurrentContext();
    
        CGContextSetFillColorWithColor(context, [color CGColor]);
        CGContextFillRect(context, rect);
    
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    
        return image;
    }
    

Upvotes: 1

Related Questions