Ios 文本在圆形UI按钮-Objective中未居中对齐
我用下面的代码制作了一个浮动图标Ios 文本在圆形UI按钮-Objective中未居中对齐,ios,objective-c,ios8,uibutton,Ios,Objective C,Ios8,Uibutton,我用下面的代码制作了一个浮动图标 - (void)createFloatingButton { UIButton *floatingButton = [UIButton buttonWithType:UIButtonTypeCustom]; floatingButton.frame = CGRectMake(kScreenWidth-kFloatingButtonSize-kFloatingButtonSize/2, self.view.frame.size.height-kFloatingB
- (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进行了检查
设置UIButton contentEdgeInsets将使您的按钮标题调整为顶部、左侧、底部和右侧
floatingButton.contentEdgeInsets = UIEdgeInsetsMake(top, left, bottom, right);
在您的情况下,它将使标题居中
floatingButton.contentEdgeInsets = UIEdgeInsetsMake(0, 2, 3, 0);
注意:根据您给定的代码,未提及kFloatingButtonSize,我使用kFloatingButtonSize=50进行了检查
您可以将image与+一起使用,而不必执行所有这些操作。用+本身制作一个图像。对于图像中的+@Jecky来说,这很容易没有问题。我必须添加点击手势,或者如果我将UIImage放在UIButton后面,代码会增加,我不想使用Imgaege。“floatingButton.titleLabel.textAlignment=NSTextAlignmentCenter;”应该很有用。@alicanbatur它不起作用。如果必须使用+作为字符串,则应该更改字体大小以获得最佳外观。如果您不必通过字符串执行此操作,则使用image将解决您的解决方案。您可以使用image+来代替所有这些操作。用+本身制作一个图像。对于图像中的+@Jecky来说,这很容易没有问题。我必须添加点击手势,或者如果我将UIImage放在UIButton后面,代码会增加,我不想使用Imgaege。“floatingButton.titleLabel.textAlignment=NSTextAlignmentCenter;”应该很有用。@alicanbatur它不起作用。如果必须使用+作为字符串,则应该更改字体大小以获得最佳外观。如果您不必通过字符串执行此操作,那么使用image将解决您的解决方案。