在按钮底部设置带标题和阴影的按钮

时间:2014-03-18 09:58:33

标签: ios objective-c uibutton shadow

我有一个看起来像这样的按钮:

enter image description here

按钮在按钮底部始终具有相同的大小,标题和阴影,以便更好地阅读。

在片刻,我使用此代码:

 self.button = [GTButton buttonWithType:UIButtonTypeCustom];
 self.button = [[GTButton alloc]initWithFrame:CGRectMake(0, 0, 156, 156)];

 self.button.exclusiveTouch = YES;

 //Create Shadow
 UILabel *shadowLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 120.0f, 156.0f, 36.0f)];

 shadowLabel.backgroundColor = GTDefaultShadowColorTiles;

 self.button.titleLabel.numberOfLines = 0;
 self.button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
 self.button.contentVerticalAlignment = UIControlContentVerticalAlignmentBottom;
[self.button setTitle:@"SHOPPING" forState:UIControlStateNormal];

 [self.button addSubview:shadowLabel];
 [self addSubview:self.button];

你可以看到我在Button上添加了黑色和50%透明度的标签(GTDefaultShadowColorTiles)!

现在我遇到以下问题:

第一个问题:

正如您所看到的,Label位于Button的顶部,因此Button Title位于阴影后面并且不易读取。是否有更好的方法在底部设置阴影?

第二个问题:

标题应始终在阴影中水平和垂直居中!怎么能实现这个目标?在瞬间,标题对齐不仅在阴影上定位整个按钮框架。

1 个答案:

答案 0 :(得分:0)

好的,我终于解决了我的问题,我不再使用按钮标题,但我将文本放入标签中(不能相信错过了):

    self.shadowLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 120.0f, 156.0f, 36.0f)];

    self.shadowLabel.backgroundColor = GTDefaultShadowColorTiles;

    self.shadowLabel.textAlignment = UIControlContentVerticalAlignmentCenter;
    self.shadowLabel.textAlignment = UIControlContentHorizontalAlignmentCenter;
    self.shadowLabel.textAlignment = NSTextAlignmentCenter;
    self.shadowLabel.numberOfLines = 3;


    self.shadowLabel.text = @"SHOPPING";

    [self.button addSubview:self.shadowLabel];
    [self addSubview:self.button];