【问题标题】:How can I programmatically set the vertical alignment of a UIButton - iOS如何以编程方式设置 UIButton 的垂直对齐方式 - iOS
【发布时间】:2012-01-20 04:41:57
【问题描述】:

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

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];

我也尝试过插入...

但对齐方式不会改变。

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

【问题讨论】:

    标签: ios cocoa-touch uibutton


    【解决方案1】:

    您可以通过这种方式对齐(水平和垂直)按钮的文本:

            UIButton *button  = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 200, 70)];
            button.backgroundColor = [UIColor whiteColor];
            button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
            button.contentVerticalAlignment = UIControlContentVerticalAlignmentBottom;
            [button setTitle:@"Align" forState:UIControlStateNormal];
            [self.container addSubview:button];
    

    【讨论】:

    • 好,我们也可以对齐按钮的图像。
    【解决方案2】:

    contentVerticalAlignment 是要走的路。可能是您创建按钮的方式。试试这个:

     UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
     button.frame = CGRectMake(10, 10, 110, 130);
     // rest of your code
     // don't release button explicitly
    

    【讨论】:

    • +1 100% 回答我的问题。但是我不会给你打勾,因为你没有读懂我的想法......对不起,我应该更好地指定这个问题。我也想保留自定义类型。我将修改我的问题以反映这一点。如果没有更好的答案出现,我会给你打勾。干杯。
    • 有趣的是,当您(错误地)通过访问 titleLabel(而不是发送 setTitleColor 消息)更改字体颜色时,这将停止工作
    • 这可能与问题无关,但您仍应使用buttonWithType 创建按钮。任何类型都可以,包括buttonWithType:UIButtonTypeCustom
    【解决方案3】:

    问题在于使用

    button.titleLabel.textColor = [UIColor grayColor];
    

    而不是

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

    在我的代码的其他地方,我仍然在垂直对齐文本时遇到问题,但我已经完成了基本示例,所以如果我更多地查看它,我应该能够获得更复杂的版本。也许我正在使用其他方法,例如 button.titleLabel.textColor 使垂直对齐不起作用...

    更新 原来的问题其实是因为我的按钮太短(按钮高度)。

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

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

    【讨论】:

      【解决方案4】:

      您可以轻松操作 UIButton 的内部文本标签。 在这里你可以怎么做:

      1. 将按钮的文本设置为@""(空字符串)
      2. 创建新的 UILabel 并调整它的框架
      3. 在按钮视图中插入创建的 UILabel 作为子视图
      4. 完成了。现在您可以以编程方式设置内部的位置 UILabel 改变它的 frame 属性。

      【讨论】:

        【解决方案5】:

        我找到了一种更适合我的方法。我使用的是自定义字体,所以对齐有点偏离上面的lindon fox's 答案。

        在你内部的 UIButton 子类:

        - (void)layoutSubviews {
        
              [super layoutSubviews];
        
              if (UIEdgeInsetsEqualToEdgeInsets(UIEdgeInsetsZero, self.titleEdgeInsets)) {
        
                    // adjust top, left bottom, and right to fit your needs
                    self.titleEdgeInsets = UIEdgeInsetsMake(top, left, bottom, right);
              }
        }
        

        我同意其他答案,即使用 contentVerticalAlignment 设置为 UIControlContentVerticalAlignmentCenter 也是一个好主意。

        【讨论】:

          【解决方案6】:

          我已经尝试了上面描述的 UIEdgeInset 更改,但它们最终破坏了我的视图的 LayoutConstraints。所以我想出了另一个解决方案,它是 UIButton 的子类,并在它的底部添加一个 UILabel 并附加约束。如果有人想走这条路,这是我的解决方案:

          class VerticalButton: UIButton {
          
          lazy var verticalTitleLabel: UILabel = {
              let label = UILabel().autolayout()
              label.font = ScaledFont.shared.font(forTextStyle: .body)
              label.numberOfLines = 0
              label.textColor = .white
              return label
          }()
          
          required init?(coder: NSCoder) {
              super.init(coder: coder)
              commonInit()
          }
          
          override init(frame: CGRect) {
              super.init(frame: frame)
              commonInit()
          }
          
          convenience init(title: String) {
              self.init(frame: .zero)
              verticalTitleLabel.text = title
          }
          
          private func commonInit() {
              setTitle(nil, for: .normal)  // Set the original title to nil so it doesn't show
              addSubview(verticalTitleLabel)
              NSLayoutConstraint.activate([
                  verticalTitleLabel.bottomAnchor.constraint(equalTo: bottomAnchor),
                  verticalTitleLabel.centerXAnchor.constraint(equalTo: centerXAnchor),
                  verticalTitleLabel.heightAnchor.constraint(greaterThanOrEqualToConstant: 25.0)
              ])
          }
          
          override func setTitle(_ title: String?, for state: UIControl.State) {
              super.setTitle("", for: state)
              verticalTitleLabel.text = title
          }
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2014-08-12
            • 2016-11-02
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-08-12
            • 2011-06-24
            相关资源
            最近更新 更多