设值按钮文字居中(alignment属性)
<span style="color: rgb(102, 102, 102); font-family: Verdana, Arial, Helvetica, sans-serif;">问题: 设置按钮的后</span><code style="color: rgb(102, 102, 102);">titleEdgeInset</code><span style="color: rgb(102, 102, 102); font-family: Verdana, Arial, Helvetica, sans-serif;">和</span><code style="color: rgb(102, 102, 102);">UITextAlignmentLeft</code><span style="color: rgb(102, 102, 102); font-family: Verdana, Arial, Helvetica, sans-serif;">按钮的标题仍然居中,不左对齐。 详细信息: 我有一个按钮为200px自定义背景与左边的图像。因为我不希望按钮的标题涵盖的形象,我的插页</span><code style="color: rgb(102, 102, 102);">titleEdgeInset=UIEdgeInsetsMake(0.0, 45.0, 0.0, 0.0);</code><span style="color: rgb(102, 102, 102); font-family: Verdana, Arial, Helvetica, sans-serif;">。这应该使标题标签的x位置开始在45px和结束于200像素。我也想将按钮的标题是左对齐的形象,所以我还设置</span><code style="color: rgb(102, 102, 102);">textAlignment = UITextAlignmentLeft</code><span style="color: rgb(102, 102, 102); font-family: Verdana, Arial, Helvetica, sans-serif;">。然而,标题文本仍然45px和200像素之间的中心,而不是左对齐。 为什么呢?</span>
<span style="font-family:Verdana, Arial, Helvetica, sans-serif;color:#666666;">正确代码如下:</span>
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];