我使用以下代码制作了一个 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];
}
我的输出是这样的:
我无法将 +
放在中间。
最佳答案
设置 UIButton contentEdgeInsets 将使您的按钮标题调整为顶部、左侧、底部、右侧。
floatingButton.contentEdgeInsets = UIEdgeInsetsMake(top, left, bottom, right);
在您的情况下,它将使标题居中
floatingButton.contentEdgeInsets = UIEdgeInsetsMake(0, 2, 3, 0);
注意:根据您给定的代码,未提及 kFloatingButtonSize,我检查了 kFloatingButtonSize = 50
关于ios - 圆形 UIButton 中的文本未居中对齐 - Objective C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40256092/