ios - 圆形 UIButton 中的文本未居中对齐 - Objective C

标签 ios objective-c ios8 uibutton

我使用以下代码制作了一个 float 图标。

- (void)createFloatingButton
{

UIButton *floatingButton = [UIButton buttonWithType:UIButtonTypeCustom];
floatingButton.frame = CGRectMake(kScreenWidth-kFloatingButtonSize-kFloatingButtonSize/2, self.view.frame.size.height-kFloatingButtonSize-kFloatingButtonSize/2, kFloatingButtonSize,kFloatingButtonSize);
[floatingButton setBackgroundColor:kNavColor];
floatingButton.layer.cornerRadius = kFloatingButtonSize/2;

//Configre Font and Text
[floatingButton setTitle:@"+" forState:UIControlStateNormal];
floatingButton.titleLabel.font = [UIFont systemFontOfSize:30];

[floatingButton addTarget:self action:@selector(floatingButtonTapped) forControlEvents:UIControlEventTouchUpInside];

//Mange Alignment
floatingButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
floatingButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;

//Add shadow
floatingButton.layer.shadowColor = [[UIColor blackColor] CGColor];
floatingButton.layer.shadowOffset = CGSizeMake(0, 2.0f);
floatingButton.layer.shadowOpacity = 0.6f;
floatingButton.layer.shadowRadius = 3.0f;
floatingButton.layer.masksToBounds = NO;

[self.view addSubview:floatingButton];
}

我的输出是这样的:

enter image description here

我无法将 + 放在中间。

最佳答案

设置 UIButton contentEdgeInsets 将使您的按钮标题调整为顶部、左侧、底部、右侧。

floatingButton.contentEdgeInsets = UIEdgeInsetsMake(top, left, bottom, right);

在您的情况下,它将使标题居中

floatingButton.contentEdgeInsets = UIEdgeInsetsMake(0, 2, 3, 0);

注意:根据您给定的代码,未提及 kFloatingButtonSize,我检查了 kFloatingButtonSize = 50

enter image description here

关于ios - 圆形 UIButton 中的文本未居中对齐 - Objective C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40256092/

相关文章:

ios - 在 iPhone 上继续中断的下载

iphone - UIImage imageWithData 需要一些时间才能出现

IOS CollectionView 页脚添加 Google AdMob

xcode - dyld : Library not loaded:/System/Library/Frameworks/SceneKit. 框架/SceneKit

ios - 从字符串中获取范围

ios - 使用 Xcode 8.3.3 和 Swift 3 从移动应用程序向服务器发送 NS 用户身份验证数据问题

ios - Realm LinkingObjects 第二级返回零

objective-c - 单击项目时关闭 NSTokenField 完成列表?

iphone - 无法处理自定义 URL 方案时提供故障转移 URL

keyboard - XCode 6.1 iOS 8键盘十进制垫不正确