如何以编程方式设置UIButton的垂直对齐方式 - iOS [英] How can I programmatically set the vertical alignment of a UIButton - iOS

查看:197
本文介绍了如何以编程方式设置UIButton的垂直对齐方式 - iOS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何用IB设置按钮的垂直对齐方式;请参阅此处。但我不能以编程方式做到这一点......这是我尝试过的事情之一:

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:

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.

请注意我想保留按钮的CustomStyle(我不想要默认的外观)。

Please note that I want to keep the CustomStyle of the button (I don't want the default look and feel).

推荐答案

问题在于使用

button.titleLabel.textColor = [UIColor grayColor];

而不是

[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

在我的代码中,我仍然遇到垂直对齐文本的麻烦,但我有基本的例子如果我再看一下,我应该能够得到更复杂的版本。也许我使用的其他方法如button.titleLabel.textColor使垂直对齐不起作用...

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...

更新
最初的问题实际上是因为我的按钮太短(按钮高度)。

Update The original problem was actually because my button was too short (button height).

单词丈夫在按钮,当前垂直对齐底部。但由于按钮的高度不够,所以并不明显。尽管标题插入内容和内容插入的默认值为零,但似乎标题不能与底部齐平。它看起来像我约5个像素。我用较小的字体测试以确保这是正确的。

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.

不幸的是,将文本垂直对齐到顶部似乎没有改变任何东西......

Unfortunately vertically aligning the text to the top does not seem to change anything...

这篇关于如何以编程方式设置UIButton的垂直对齐方式 - iOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆