带有 UITextAlignmentLeft 的 iOS UIButton 仍然居中文本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9334047/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 16:54:12  来源:igfitidea点击:

iOS UIButton with UITextAlignmentLeft still centering text?

objective-cioscocoa-touchuibutton

提问by memmons

Problem:
After setting a button's titleEdgeInsetand UITextAlignmentLeftthe button's title is still centered, not left-aligned.

问题:
设置一个按钮的经过titleEdgeInsetUITextAlignmentLeft按钮的标题仍然是中心,不是以左对齐。

Details:
I have a button 200px wide using a custom background with an image on the left. Since I don't want the button title to cover the image, I inset the title using titleEdgeInset=UIEdgeInsetsMake(0.0, 45.0, 0.0, 0.0);. This should make title label's x position start at 45px and end at 200px. I also want the button's title to be left-aligned to the image, so I also set textAlignment = UITextAlignmentLeft. However, the title text is still centered between 45px and 200px, not left aligned.

详细信息:
我有一个 200 像素宽的按钮,使用自定义背景,左侧有一个图像。由于我不希望按钮标题覆盖图像,因此我使用titleEdgeInset=UIEdgeInsetsMake(0.0, 45.0, 0.0, 0.0);. 这应该使标题标签的 x 位置从 45 像素开始到 200 像素结束。我还希望按钮的标题与图像左对齐,因此我还设置了textAlignment = UITextAlignmentLeft. 但是,标题文本仍然在 45 像素和 200 像素之间居中,而不是左对齐。

Why?

为什么?

Here's the relevant code:

这是相关的代码:

button.titleEdgeInsets = UIEdgeInsetsMake(0.0, 45.0, 0.0, 0.0);
button.titleLabel.textAlignment = UITextAlignmentLeft;
[button setTitle:@"X"];

回答by rob mayoff

My test code:

我的测试代码:

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(20, 20, 200, 72);
UIImage *image = [UIImage imageNamed:@"rob.png"];
[button setImage:image forState:UIControlStateNormal];
button.imageEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
[button setTitle:@"Hello" forState:UIControlStateNormal];
button.titleEdgeInsets = UIEdgeInsetsMake(0, 20, 0, 0);
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
[self.view addSubview:button];

Note that I set button.contentHorizontalAlignment, not button.titleLabel.textAlignment. My result:

请注意,我设置了button.contentHorizontalAlignment,而不是button.titleLabel.textAlignment。我的结果:

Screen shot of label

标签截图

回答by Vineesh TP

set contentHorizontalAlignment property for button, it will work.

为按钮设置 contentHorizo​​ntalAlignment 属性,它将起作用。

YourButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;

Swift 4.0

斯威夫特 4.0

YourButton.contentHorizontalAlignment = UIControlContentHorizontalAlignment.center

回答by ZaBlanc

Put a background color on the titleLabel. I'm betting it's being sized to fit, so the text alignment won't matter. It's only as long as it needs to be. What you need to fix is the position of the label itself. You might have to extend the UIButton and override layoutSubviews to get it right. I've never seen a button with anything but a centered label.

在 titleLabel 上放置背景颜色。我打赌它的大小适合,所以文本对齐无关紧要。只要它需要。您需要修复的是标签本身的位置。您可能需要扩展 UIButton 并覆盖 layoutSubviews 才能使其正确。我从来没有见过一个按钮,除了一个居中的标签。

回答by Evana

Set contentHorizontalAlignment property for a button:

为按钮设置 contentHorizo​​ntalAlignment 属性:

yourButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;

回答by Riajur Rahman

Swift 3

斯威夫特 3

Set contentHorizontalAlignment property do the work. Thanks

设置 contentHorizo​​ntalAlignment 属性来完成这项工作。谢谢

yourButton.contentHorizontalAlignment = .left

回答by Mostafa Elbutch

Just override the button's setter and try to set the contentHorizontalAlignment property in the setter:

只需覆盖按钮的 setter 并尝试在 setter 中设置 contentHorizo​​ntalAlignment 属性:

-(UIButton *)yourButton {
      [_yourButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];

    return _yourButton;
}