【问题标题】:UIButton titleLabel not vertically centered when using minimumScaleFactor使用 minimumScaleFactor 时 UIButton titleLabel 未垂直居中
【发布时间】:2014-06-30 20:49:19
【问题描述】:

我有一个 UIButton,文本通常在按钮内居中,直到文本字符串变长并开始按比例缩小文本的大小。然后文本似乎粘在 textField 的底部,而不是停留在按钮的中心。

self.titleBtn.titleLabel.adjustsFontSizeToFitWidth = YES;
self.titleBtn.titleLabel.minimumScaleFactor = .2;

[self.descriptionBtn setTitle:@"T" forState:UIControlStateNormal]; 
//works normally

但是

[self.descriptionBtn setTitle:@"This a longer title" forState:UIControlStateNormal];
//is vertically lower than center

【问题讨论】:

    标签: ios uibutton


    【解决方案1】:

    如上所述here :)
    您需要使用 UILabel 的 baselineAdjustment 属性
    label.baselineAdjustment = UIBaselineAdjustmentAlignCenters

    【讨论】:

      【解决方案2】:

      斯威夫特 3

      label.baselineAdjustment = .alignCenters
      

      对于按钮

      button.titleLabel?.baselineAdjustment = .alignCenters
      

      【讨论】:

        猜你喜欢
        • 2013-12-15
        • 1970-01-01
        • 2014-03-02
        • 1970-01-01
        • 2020-05-21
        • 1970-01-01
        • 2021-09-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多