【问题标题】:How can I set the title of a UIButton as left-aligned?如何将 UIButton 的标题设置为左对齐?
【发布时间】:2011-02-15 10:33:50
【问题描述】:

我需要在UIButton 的左侧显示一个电子邮件地址,但它被定位到了中心。

有没有办法将对齐设置为UIButton 的左侧?

这是我当前的代码:

UIButton* emailBtn = [[UIButton alloc] initWithFrame:CGRectMake(5,30,250,height+15)];
emailBtn.backgroundColor = [UIColor clearColor];
[emailBtn setTitle:obj2.customerEmail forState:UIControlStateNormal];
emailBtn.titleLabel.font = [UIFont systemFontOfSize:12.5];
[emailBtn setTitleColor:[[[UIColor alloc]initWithRed:0.121 green:0.472 blue:0.823 alpha:1]autorelease] forState:UIControlStateNormal];
[emailBtn addTarget:self action:@selector(emailAction:) forControlEvents:UIControlEventTouchUpInside];
[elementView addSubview:emailBtn];
[emailBtn release];

【问题讨论】:

标签: ios objective-c xcode cocoa-touch uibutton


【解决方案1】:

设置contentHorizo​​ntalAlignment:

emailBtn.contentHorizontalAlignment = .left;

您可能还想调整左插入的内容,否则文本将触及左边框:

emailBtn.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);

// Swift 3 and up:
emailBtn.contentEdgeInsets = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 0);

【讨论】:

  • 请记住,您也可以从 Interface Builder 中设置这两个属性。
  • 所以它是我的第 9000 号,thanx,有用的答案
  • 我们懒得去搜索文档了。如果没有 SO,我们该怎么办!
  • 显示 SO 的文档比 Apple 文档更好。
  • UIControlContentHorizontalAlignmentLeadingUIControlContentHorizontalAlignmentTrailing 在 iOS 11 中添加
【解决方案2】:

@DyingCactus 的代码有一个小错误。 这是将 UILabel 添加到 UIButton 以对齐按钮文本以更好地控制按钮“标题”的正确解决方案:

NSString *myLabelText = @"Hello World";
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];

// position in the parent view and set the size of the button
myButton.frame = CGRectMake(myX, myY, myWidth, myHeight); 

CGRect myButtonRect = myButton.bounds;
UILabel *myLabel = [[UILabel alloc] initWithFrame: myButtonRect];   
myLabel.text = myLabelText;
myLabel.backgroundColor = [UIColor clearColor];
myLabel.textColor = [UIColor redColor]; 
myLabel.font = [UIFont fontWithName:@"Helvetica Neue" size:14.0];   
myLabel.textAlignment = UITextAlignmentLeft;

[myButton addSubview:myLabel];
[myLabel release];

希望这会有所帮助....

阿尔

【讨论】:

    【解决方案3】:

    这里解释了如何做到这一点以及为什么会这样: http://cocoathings.blogspot.com/2013/03/how-to-make-uibutton-text-left-or-right.html

    【讨论】:

      【解决方案4】:

      使用emailBtn.titleEdgeInsets 比使用contentEdgeInsets 更好,以防您不想更改按钮内的整个内容位置。

      【讨论】:

        【解决方案5】:

        如果您不想在代码中进行调整,也可以使用界面生成器。 这里我左对齐文本并缩进一些:

        别忘了你也可以在按钮中对齐图片。:

        【讨论】:

        • 这帮助了我很多,不敢相信我没有意识到这些控件的用途。
        • 谢谢,它帮助了我。如果没有示例图片中箭头指示的帮助,我无法找到按钮标题的边缘插图。
        • 在 xcode 8.2.1 中它移到了其他地方,请参阅下面的答案
        • 如何以编程方式快速做到这一点?我想在右边对齐图像,在左边对齐标题???
        【解决方案6】:
        UIButton *btn;
        btn.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;
        btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        

        【讨论】:

          【解决方案7】:

          在 Swift 3+ 中:

          button.contentHorizontalAlignment = .left
          

          【讨论】:

          • contentHorizo​​ntalAlignment的.leading和.left值有什么区别?
          【解决方案8】:

          对于 Swift 2.0:

          emailBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignment.Left  
          

          如果有人需要,这可以提供帮助。

          【讨论】:

            【解决方案9】:

            在 xcode 8.2.1 的界面生成器中,它移动到:

            【讨论】:

              【解决方案10】:

              Swift 4+

              button.contentHorizontalAlignment = .left
              button.contentVerticalAlignment = .top
              button.contentEdgeInsets = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
              

              【讨论】:

                【解决方案11】:

                Swift 5.0Xcode 10.2

                你有两种方法接近

                1) 直接方法

                btn.contentHorizontalAlignment = .left
                

                2) SharedClass 示例(编写一次,使用每个软件)

                这是你的共享类(像这样你访问所有组件属性)

                import UIKit
                
                class SharedClass: NSObject {
                
                    static let sharedInstance = SharedClass()
                
                    private override init() {
                
                    }
                }
                
                //UIButton extension
                extension UIButton {
                    func btnProperties() {
                        contentHorizontalAlignment = .left
                    }
                }
                

                在您的 ViewController 调用中像这样

                button.btnProperties()//This is your button
                

                【讨论】:

                  【解决方案12】:

                  试试

                  button.semanticContentAttribute = UISemanticContentAttributeForceRightToLeft;
                  

                  【讨论】:

                  • 这对我有用,contentHorizo​​ntalAlignment 没有,谢谢。
                  【解决方案13】:

                  SwiftUI

                  您应该更改应用于文本的.frame 修饰符的对齐属性。此外,将多行文本对齐方式设置为.leading

                  Button {
                      // handler for tapping on the button
                  } label: {
                      Text("Label")
                          .frame(width: 200, alignment: .leading)
                          .multilineTextAlignment(.leading)
                  }
                  

                  【讨论】:

                    猜你喜欢
                    • 1970-01-01
                    • 2017-10-05
                    • 2016-07-27
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 2012-03-10
                    • 2017-12-08
                    相关资源
                    最近更新 更多