ios - 将 UIButton 标题置于其框架的中心

标签 ios ios7 uibutton uilabel cgrect

我想在我的 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);

最佳答案

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

关于ios - 将 UIButton 标题置于其框架的中心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24356966/

相关文章:

ios - 在背景中以低精度更新地理围栏,以高精度更新 map 上的位置,无法按预期工作

ios - UIView 相对于另一个 UIView 的位置

iOS7脏矩形绘图怪异

ios - 如何将平移手势的 x 坐标映射到立方体的旋转?

ios - SVProgressHUD 在模态视图的背景中显示

IOS 7 损坏的数据库

ios - 根据状态以编程方式更改 UIButton 字体颜色

ios - swift |计时器不会在无效调用时停止,而是加速?

ios - 如何在 iOS 上的按钮中显示带下划线的文本?

ios - 在主线程上调用方法?