xcode 对齐 UIButton 的文本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15269971/
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
Aligning a UIButton's text?
提问by himan
I am generating some buttons. In the button I am showing date and a reference id given by the user. I want these data to be left aligned in the button.
我正在生成一些按钮。在按钮中,我显示了用户提供的日期和参考 ID。我希望这些数据在按钮中左对齐。
What I have tried:
我尝试过的:
[button.titleLabel setTextAlignment:UITextAlignmentLeft];
button.titleLabel.textAlignment = UITextAlignmentLeft;
But this doesn't work.
但这不起作用。
- (void)viewDidLoad
{
[super viewDidLoad];
[scrollView setScrollEnabled:YES];
[scrollView setContentSize:CGSizeMake(320, 500)];
int i = 0;
for (CalculatorData *data in calcDatas) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(1 , 20 + i*40, 295, 30);
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"MM/dd/yyyy HH:MM:SS"];
NSString *dateString = [dateFormat stringFromDate:data.updateDate];
NSString *title = [NSString stringWithFormat:@"%@ : %@",dateString,[data dataKey]];
[button setTitle:title forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonPressed:)
forControlEvents:UIControlEventTouchUpInside];
button.tag = data.calcId;
[scrollView addSubview:button];
i++;
}
回答by dsgriffin
You can use the contentHorizontalAlignment:
您可以使用contentHorizontalAlignment:
"The horizontal alignment of content (text or image) within the receiver."
“接收器内内容(文本或图像)的水平对齐方式。”
and the contentVerticalAlignmentproperties for this:
以及用于此的contentVerticalAlignment属性:
"The vertical alignment of content (text or image) within the receiver."
“接收器内内容(文本或图像)的垂直对齐方式。”
Example usage:
用法示例:
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
button.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;
回答by Vineet Singh
Use this :
用这个 :
button.contentVerticalAlignment = UIControlContentVerticalAlignmentFill;
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
button.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
回答by Rushabh
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
button.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
回答by cheznead
For Swift 3, the following worked for me:
对于 Swift 3,以下对我有用:
button.contentHorizontalAlignment = .left
button.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0)
回答by nyxee
Swift 3.x:
斯威夫特 3.x:
button.titleLabel?.textAlignment = .center
回答by Abha
button.titleLabel.textAlignment = NSTextAlignmentLeft;
button.titleLabel.textAlignment = NSTextAlignmentLeft;
回答by Pablo Segura
In Swift you can use this:
在 Swift 中你可以使用这个:
uibutton/uilabel.titleLabel?.textAlignment = .Center
uibutton/uilabel.titleLabel?.textAlignment = .Center
NSTextAlignment is a enum there for you can assign it by placing a .VALUE
NSTextAlignment 是一个枚举,你可以通过放置一个 .VALUE 来分配它
Here are the possible alignment enum's case Left // Visually left aligned
这是可能的对齐枚举的情况 Left // 视觉上左对齐
case Center // Visually centered
case Right // Visually right aligned
/* !TARGET_OS_IPHONE */
// Visually right aligned
// Visually centered
case Justified // Fully-justified. The last line in a paragraph is natural-aligned.
case Natural // Indicates the default alignment for script