如何以编程方式设置 UIButton 的垂直对齐方式 - iOS
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8503943/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I programmatically set the vertical alignment of a UIButton - iOS
提问by lindon fox
I know how to set the vertical alignment of a button with IB; see here. But I can not do it programmatically... This is one of the things I have tried:
我知道如何使用 IB 设置按钮的垂直对齐方式;看到这里。但我不能以编程方式做到这一点......这是我尝试过的事情之一:
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(10, 10, 110, 130)];
[button setTitle:@"Align" forState:UIControlStateNormal];
button.backgroundColor = [UIColor whiteColor];
button.titleLabel.textColor = [UIColor grayColor];
[button setContentVerticalAlignment:UIControlContentVerticalAlignmentBottom];
[self.view addSubview:button];
[button release];
I have also tried insets...
我也试过插入...
But the alignment won't change.
但对齐不会改变。
Please note that I want to keep the CustomStyle of the button (I don't want the default look and feel).
请注意,我想保留按钮的 CustomStyle(我不想要默认的外观和感觉)。
采纳答案by lindon fox
The problem was the using
问题是使用
button.titleLabel.textColor = [UIColor grayColor];
instead of
代替
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
Elsewhere in my code I am still having troubles vertically aligning the text, but I have got the basic example going, so I should be able to get the more complicated version going if I look at it more. Perhaps there are other methods I am using like button.titleLabel.textColor which make the vertical alignment not work...
在我的代码的其他地方,我仍然在垂直对齐文本时遇到问题,但我已经有了基本的例子,所以如果我多看它,我应该能够得到更复杂的版本。也许我正在使用其他方法,例如 button.titleLabel.textColor 使垂直对齐不起作用......
UpdateThe original problem was actually because my button was too short (button height).
更新原来的问题实际上是因为我的按钮太短(按钮高度)。
The word husband
is in the button, which is currently vertically aligned to the bottom. But since the height of the button is not enough, it is not obvious. Even though the default of the title insets and content insets are zero, it does not seem like the title can be flush against the bottom. It looks like about 5 pixels to me. I tested with a smaller font to make sure this is correct.
这个词husband
在按钮中,它当前垂直对齐到底部。但是由于按钮的高度不够,所以不明显。尽管标题插入和内容插入的默认值为零,但标题似乎不能与底部齐平。对我来说,它看起来大约是 5 像素。我用较小的字体进行了测试,以确保这是正确的。
Unfortunately vertically aligning the text to the top does not seem to change anything...
不幸的是,将文本垂直对齐到顶部似乎并没有改变任何东西......
回答by Alessandro Pirovano
You can alignment (horizontal and vertical) the text of button in this way:
您可以通过这种方式对齐(水平和垂直)按钮的文本:
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 200, 70)];
button.backgroundColor = [UIColor whiteColor];
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
button.contentVerticalAlignment = UIControlContentVerticalAlignmentBottom;
[button setTitle:@"Align" forState:UIControlStateNormal];
[self.container addSubview:button];
回答by XJones
contentVerticalAlignment
is the way to go. Could be how you're creating the button. Try this:
contentVerticalAlignment
是要走的路。可能是您创建按钮的方式。尝试这个:
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(10, 10, 110, 130);
// rest of your code
// don't release button explicitly
回答by Dmitriy Lezhnev
You can easily manipulate inner text label of UIButton. Here how you can do it:
您可以轻松操作 UIButton 的内部文本标签。在这里你可以怎么做:
- set buttons's text to @"" (empty string)
- create new UILabel and adjust it's frame
- insert created UILabel inside buttons's view as subview
- It's done. Now you can programmatically set position of inner UILabel with changing it's frame property.
- 将按钮的文本设置为@""(空字符串)
- 创建新的 UILabel 并调整它的框架
- 在按钮的视图中插入创建的 UILabel 作为子视图
- 完成。现在,您可以通过更改其 frame 属性以编程方式设置内部 UILabel 的位置。
回答by cynistersix
I found a better way that works for me. I'm using a custom font so the alignment went a bit off by a few like in lindon fox'sanswer above.
我找到了一种更适合我的方法。我正在使用自定义字体,因此对齐方式有些偏离,就像上面lindon fox 的回答一样。
Inside of you UIButton subclass:
在你的 UIButton 子类中:
- (void)layoutSubviews {
[super layoutSubviews];
if (UIEdgeInsetsEqualToEdgeInsets(UIEdgeInsetsZero, self.titleEdgeInsets)) {
// adjust top, left bottom, and right to fit your needs
self.titleEdgeInsets = UIEdgeInsetsMake(top, left, bottom, right);
}
}
I agree with the other answers that using the contentVerticalAlignment
set to UIControlContentVerticalAlignmentCenter
is also a good idea.
我同意其他答案,即使用contentVerticalAlignment
set toUIControlContentVerticalAlignmentCenter
也是一个好主意。