gpt4 book ai didi

ios - 当我设置内容插入将其移至底部时,为什么 UIButton 会将标题标签一直移至右侧?

转载 作者:行者123 更新时间:2023-11-29 03:50:07 25 4
gpt4 key购买 nike

我正在尝试制作一个在文本上方带有图像的 UIButton。为此,我尝试将按钮的边缘插入设置为:

[button setImageEdgeInsets:UIEdgeInsetsMake(0.0f, 0.0f, 10, 0.0f)];
[button setTitleEdgeInsets:UIEdgeInsetsMake(10, 0.0, 0.0, 0.0)];

我得到的是一个按钮,其中图像位于同一位置,而文本一直被压到一侧,大约一个字符宽的空间。

如何简单地设置插图,以便将图像放在文本上方?真的有那么多要求吗?

最佳答案

你只是错误地计算了edgeInSets。它们并不像你想象的那样存在。这是我的测试代码。将图像和标题放在正确的位置确实花了我一段时间。

UIButton *tmp_testBtn = [[UIButton alloc] initWithFrame:CGRectMake(50, 200, 60, 40)];
tmp_testBtn.backgroundColor = [UIColor grayColor];
[tmp_testBtn setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 20, 0)];
[tmp_testBtn setImage:[UIImage imageNamed:@"France"] forState:UIControlStateNormal];
[tmp_testBtn setTitleEdgeInsets:UIEdgeInsetsMake(20, -258, 0, 0)];
[tmp_testBtn setTitle:@"testbutton" forState:UIControlStateNormal];

CGRect contentRect = [tmp_testBtn contentRectForBounds:tmp_testBtn.bounds];
NSLog(@"contenRect:%f,%f,%f,%f",contentRect.origin.x,contentRect.origin.y,contentRect.size.width,contentRect.size.height);
CGRect titleRect = [tmp_testBtn titleRectForContentRect:contentRect];
NSLog(@"titleRect:%f,%f,%f,%f",titleRect.origin.x,titleRect.origin.y,titleRect.size.width,titleRect.size.height);
CGRect imageRect = [tmp_testBtn imageRectForContentRect:contentRect];
NSLog(@"imageRect:%f,%f,%f,%f",imageRect.origin.x,imageRect.origin.y,imageRect.size.width,imageRect.size.height);

[self.view addSubview:tmp_testBtn];
[tmp_testBtn release];

顺便说一下,我不会这样做。我更喜欢自定义一个带有 UIImageView 和 UILabel 的按钮。

关于ios - 当我设置内容插入将其移至底部时,为什么 UIButton 会将标题标签一直移至右侧?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17158293/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com