【问题标题】:Shadow on UIButton text only (not background)仅 UIButton 文本上的阴影(不是背景)
【发布时间】:2015-07-12 22:50:03
【问题描述】:

我有一组带有背景颜色的UIButtons。当用户点击一个时,我希望 only 按钮的文本周围有一个阴影(以表明它已被选中)。但是,当我添加阴影时,阴影会出现在整个按钮(背景和全部)上,而不仅仅是文本。有没有比在空白按钮上添加UILabel 更简单的解决方法?

UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
...
[button setBackgroundColor:[UIColor blueColor]];

[button.layer setShadowRadius:8.0];
[button.layer setShadowColor:[[UIColor orangeColor] CGColor]];
[button.layer setShadowOpacity:0];
...

【问题讨论】:

  • 你真正想要什么,当你点击时,按钮文本应该看起来有阴影,背景应该保持不变?
  • 要给文字添加阴影,请看NSAttributedString
  • @Viral 文字应该有阴影,背景应该没有。
  • @luk2302 怎么样?

标签: ios objective-c cocoa-touch uibutton shadow


【解决方案1】:

以下是使用 Objective-C 中可用的阴影半径属性为按钮标题添加阴影的简单方法:

#import <QuartzCore/QuartzCore.h>    

button.titleLabel.layer.shadowOffset = CGSizeMake(2.0, 2.0);
button.titleLabel.layer.shadowColor = [UIColor colorWithWhite:0.1 alpha:0.7].CGColor;
button.titleLabel.layer.shadowRadius = 2.0;
button.titleLabel.layer.shadowOpacity = 1.0;
button.titleLabel.layer.masksToBounds = NO;

斯威夫特..

假设你覆盖了UIButton 类..

    titleLabel!.layer.shadowColor = UIColor.black.cgColor
    titleLabel!.layer.shadowOffset = CGSize(width: -0.5, height: 0.5)
    titleLabel!.layer.shadowOpacity = 1.0
    titleLabel!.layer.shadowRadius = 0
    titleLabel!.layer.masksToBounds = false

【讨论】:

  • 非常感谢,@Userich!碰巧你在我需要的时候发布了这个。
【解决方案2】:

您需要使用 setTitleShadowColor: forState:UIButtonshadowOffset 属性。 每当用户点击按钮时,下面的代码只会给按钮标签添加阴影。

UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];

[button setBackgroundColor:[UIColor blueColor]];

button.frame = CGRectMake(50, 70, 200, 100);

[button setTitle:@"Test Button" forState:UIControlStateNormal];

[button.titleLabel setFont:[UIFont systemFontOfSize:45]];

[button setTitleShadowColor:[UIColor redColor] forState:UIControlStateHighlighted];

[button.titleLabel setShadowOffset:CGSizeMake(2.0, 2.0)];

希望这会有所帮助。

【讨论】:

    【解决方案3】:

    只需在UIButtontitleLabel 属性上设置阴影,而不是您现在正在执行的操作。

    例如

    button.titleLabel.shadowColor = ((selectionState) ?[UIColor orangeColor] : [UIColor clearColor] );
    button.titleLabel.shadowOffset = CGSizeMake (1.5,1.5);
    

    2018 年

    这不起作用。使用@Userich的答案

    【讨论】:

    • 这不再有效。现在将添加与文本无关的一般区域阴影。现在需要用到:setTitleShadowColor等
    【解决方案4】:

    一种方法是使用属性字符串作为正常和突出显示的标题。您可以创建一个NSShadow 对象,并将其指定为NSShadowAttributeName 的值。这使您可以控制阴影的属性。为了防止标题变暗,您应该将按钮类型设置为Custom 而不是System

    - (void)viewDidLoad {
        [super viewDidLoad];
    
        NSShadow *shadow = [[NSShadow alloc] init];
        shadow.shadowColor = [UIColor darkGrayColor];
        shadow.shadowOffset = CGSizeMake(2, 2);
        shadow.shadowBlurRadius = 2;
    
        NSString *titleString = @"Title";
        NSAttributedString *normalTitle = [[NSAttributedString alloc] initWithString:titleString];
        NSAttributedString *highlightedTitle = [[NSAttributedString alloc] initWithString:titleString attributes:@{NSShadowAttributeName:shadow}];
    
        [self.button setAttributedTitle:normalTitle forState:UIControlStateNormal];
        [self.button setAttributedTitle:highlightedTitle forState:UIControlStateHighlighted];
    }
    

    【讨论】:

      猜你喜欢
      • 2012-06-25
      • 2020-06-10
      • 1970-01-01
      • 2020-10-25
      • 1970-01-01
      • 1970-01-01
      • 2015-06-18
      • 2016-06-25
      • 1970-01-01
      相关资源
      最近更新 更多