[英]Programmatically adding a shadow to a UIButton label
我試圖在沒有運氣的情況下為按鈕標簽添加1px黑色陰影。 我試過這個: self.setTitleShadowOffset = CGSizeMake(0, -1);
但我得到:
在非結構或聯合的東西中請求成員'setTitleShadowOffset'
任何建議都會非常感謝!
正確的屬性是self.titleLabel.shadowOffset:
UIButton *b = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[b setTitleShadowColor:[UIColor purpleColor] forState:UIControlStateNormal];
b.titleLabel.shadowOffset = CGSizeMake(1.0, 1.0);
[b setTitle:@"Hello, I'm a Button" forState:UIControlStateNormal];
b.frame = CGRectMake(10.0, 10.0,300.0, 40.0);
其他答案沒有正確設置陰影顏色(我懷疑他們沒有注意到,因為他們試圖將陰影顏色設置為默認顏色,黑色。)
這段代碼為我添加了一個白色陰影到我的按鈕文本:
myButton.titleLabel.shadowOffset = CGSizeMake(0, 1);
[myButton setTitleShadowColor:[UIColor whiteColor] forState:UIControlStateNormal];
不推薦使用UIButton
的setTitleShadowOffset
。 使用shadowOffset
的titleLabel財產UIButton
buttonName.titleLabel.shadowOffset = CGSizeMake(0, -1);
對於Swift 3:
button.setTitleShadowColor(UIColor.red, for: .normal)
button.titleLabel?.shadowOffset = CGSize(width: 2, height: 2)
以下是如何使用radius屬性在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;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.