白色背景上带有透明标题的iOS UIButton

白色背景上带有透明标题的iOS UIButton,ios,objective-c,uibutton,calayer,alpha,Ios,Objective C,Uibutton,Calayer,Alpha,我有一个自定义的ui按钮,背景透明,标题为白色。 我正在寻找一个简单的解决方案,在突出显示(白色背景和透明标题)上反转它,因为它是在systemUISegmentedControl上实现的 有没有比将快照上的alpha反转用作CALayer掩码更简单的解决方案 如果没有,您能否告诉我如何反转CALayeralpha?您可以设置 -(void)viewDidload: { [yourButton setTitleColor:(UIColor *)color

我有一个自定义的
ui按钮
,背景透明,标题为白色。 我正在寻找一个简单的解决方案,在突出显示(白色背景和透明标题)上反转它,因为它是在system
UISegmentedControl
上实现的

有没有比将快照上的alpha反转用作
CALayer
掩码更简单的解决方案

如果没有,您能否告诉我如何反转
CALayer
alpha?

您可以设置

-(void)viewDidload: {
    [yourButton setTitleColor:(UIColor *)color 
                     forState:UIControlStateHighlighted];
    [yourButton addTarget:self 
                   action:@selector(changeButtonBackGroundColor:) 
         forControlEvents:UIControlEventTouchDown];
}

-(void)changeButtonBackGroundColor:(UIButton *)button {
    [button setBackgroundColor:[UIColor yourColor]];
}

记住使用事件将其更改回:
UIControlEventTouchUpInside
uicontrolEventTouchOut
:)

您可以使用Destination Out作为混合模式在CGContext上绘制文本

此代码似乎有效:

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor greenColor];

    UIFont* font = [UIFont fontWithName:@"Helvetica-Bold" size:18];
    NSString* buttonText = @"BUTTON";
    CGSize buttonSize =  CGSizeMake(200, 50);
    NSDictionary* textAttributes = @{NSFontAttributeName:font};

    CGSize textSize = [buttonText sizeWithAttributes:textAttributes];

    UIGraphicsBeginImageContextWithOptions(buttonSize, NO, [[UIScreen mainScreen] scale]);
    CGContextRef ctx = UIGraphicsGetCurrentContext();

    CGContextSetFillColorWithColor(ctx, [UIColor redColor].CGColor);

    UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, buttonSize.width, buttonSize.height)];
    CGContextAddPath(ctx, path.CGPath);
    CGContextFillPath(ctx);

    CGContextSetBlendMode(ctx, kCGBlendModeDestinationOut);
    CGPoint center = CGPointMake(buttonSize.width/2-textSize.width/2, buttonSize.height/2-textSize.height/2);
    [@"BUTTON" drawAtPoint:center withAttributes:textAttributes];


    UIImage* viewImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    UIImageView* imgView = [[UIImageView alloc] initWithImage:viewImage];
    [self.view addSubview:imgView];
}
顺便说一下,如果要在UIButton中设置UIImage,而不是将其添加到视图中:

UIButton* boton = [[UIButton alloc] initWithFrame:CGRectMake(50, 50, viewImage.size.width, viewImage.size.height)];
[boton setBackgroundColor:[UIColor clearColor]];
[boton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
boton.titleLabel.font = font;
[boton setTitle:buttonText forState:UIControlStateNormal];
[boton setImage:viewImage forState:UIControlStateHighlighted];
[self.view addSubview:boton];

Thx,但如果您为标题设置clearColor,它将与背景混合,而不是遮罩背景。我说的对吗?是的,会的。但是为什么不保存按钮的背景色,并在用户高亮显示时将其设置为按钮?背景不是问题。我需要透明的标题才能看穿它(只有标题)按钮下的所有内容(如锁孔效果)。将标题颜色设置为button.superView.BackgroundColor,但我希望标题是真正透明的,而不是与superView背景颜色相似。根据我所知,您可能必须使用CGMasks。。。因此,将标题设为黑色,按钮设为白色,然后将其遮罩到背景上,然后在第一个按钮下方放置第二个按钮,该按钮为纯白色。我想是这样的。。。你认为这会比拍摄按钮内容快照和反转alpha更简单吗?是的,这可能更简单。正如您所看到的,图像创建只是几行代码。顺便说一句,如果您觉得我的答案有用,那么接受它就太好了。只添加了
UIGraphicsBeginImageContextWithOptions(按钮化,否,[[UIScreen mainScreen]缩放])我已经将代码片段重构为category/subclass方法。