【问题标题】:Center a UIButton title in its frame在其框架中居中 UIButton 标题
【发布时间】:2014-08-12 23:08:54
【问题描述】:

我想在我的 UIButton CGRect 中将这个 + 符号居中(x 和 y 轴),但它一直被向下推到 y 轴。

UIButton *aButton = [UIButton buttonWithType:UIButtonTypeSystem];
[aButton setTitle:@"+" forState:UIControlStateNormal];
[aButton.titleLabel setFont:[UIFont fontWithName:@"HelveticaNeue-Bold" size:(40.0)]];
[aButton setBackgroundColor:[UIColor grayColor]];
[aButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[aButton setTitleColor:[UIColor blueColor] forState:UIControlStateHighlighted];

aButton.layer.cornerRadius = 25.0;

aButton.frame = CGRectMake(75.0, 100.0, 50.0, 50.0);

【问题讨论】:

    标签: ios ios7 uibutton uilabel cgrect


    【解决方案1】:

    如果你有上升器和下降器,这对你来说会更正确。按钮内有一个标签,它的大小可以容纳该字体所有可能字符的高度。如果您不喜欢标签的位置,可以随意移动它。有很多方法可以做到这一点;例如,您可以尝试使用按钮的titleEdgeInsets

    【讨论】:

    • 哦,你如何移动标签。添加一个 UILabel 然后选择它的中心原点?
    • 还有一个contentEdgeInsets,但如果我是你,我会先试试titleEdgeInsets
    【解决方案2】:

    设置标题和字体后添加这一行:

    aButton.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 9, 0);
    

    【讨论】:

      猜你喜欢
      • 2021-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-28
      • 2013-08-06
      相关资源
      最近更新 更多