圆形 UI 按钮中的文本未居中对齐 - 目标 C

Text is not center Aligned in Rounded UI Button - ObjectiveC

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

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

我的输出是这样的:

我不能把 + 放在中间。

设置UIButton contentEdgeInsets 将使您的按钮标题调整为上、左、下、右。

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

在你的情况下,它会使标题居中

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

注意: 根据你给出的代码 kFloatingButtonSize 没有被提及,我检查了 kFloatingButtonSize = 50