【问题标题】:iOS UIButton with transparent title on white background带有白色背景透明标题的 iOS UIButton
【发布时间】:2014-06-24 06:30:18
【问题描述】:

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

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

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

【问题讨论】:

  • 设置标题颜色为button.superView.backgroundColor
  • 但我希望标题是真正透明的 - 不是类似的超级视图背景颜色。
  • 据我所知,您可能不得不使用 CGMask... 所以将标题设为黑色,将按钮设为白色,然后将其遮盖到背景中,然后在下方放置第二个按钮首先那只是纯白色。

标签: ios objective-c uibutton calayer alpha


【解决方案1】:

您可以使用 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];

【讨论】:

  • 我是这么认为的......你认为这会比拍摄按钮内容快照和反转 alpha 更简单吗?
  • 是的,这可能更简单。如您所见,图像创建只是几行代码。
  • 顺便说一句,如果您发现我的回答很有用,如果您接受它,那就太好了。
  • 仅添加UIGraphicsBeginImageContextWithOptions(buttonSize, NO, [[UIScreen mainScreen] scale]);
  • 我已将 sn-p 重构为 category/subclass 方法。
【解决方案2】:

你可以设置

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

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

记得用事件改回来:UIControlEventTouchUpInsideUIControlEventTouchOutSide :)

【讨论】:

  • 谢谢,但是如果您为标题设置 clearColor 它将与背景混合而不是遮盖它。我说的对吗?
  • 是的,它会的。但是为什么不保存按钮的 backgroundColor 并在用户突出显示时将其设置为按钮呢?
  • 背景不是问题。我需要透明的标题才能看穿它(只有标题)按钮下的所有内容(像钥匙孔效果)。
猜你喜欢
  • 1970-01-01
  • 2013-11-27
  • 2014-08-21
  • 2013-04-03
  • 1970-01-01
  • 2021-03-28
  • 2018-02-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多