如何根据 iphone 中的文本大小动态增加按钮宽度?
Posted
技术标签:
【中文标题】如何根据 iphone 中的文本大小动态增加按钮宽度?【英文标题】:How can i increase the button width dynamically depends on the text size in iphone? 【发布时间】:2011-03-19 04:36:57 【问题描述】:我以编程方式创建了 10 个按钮并在按钮中设置了标题。现在我想动态增加按钮框架的大小,它取决于文本。
我给出了一些条件并设置了帧大小。但是我如何设置确切的帧大小取决于文本(动态获取文本)。
这是我的示例代码,
float x=0, y=0, w, h=20;
for(int i = 100; i < 110; i++)
btn = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
UIImage * img = [UIImage imageNamed:@"Round_Rect4.png"];
[btn setBackgroundImage:img forState:UIControlStateSelected];
titleString = [titleArray objectAtIndex:i-100]; // get button title
if([titleString length] <= 5)
w = 50;
btn.frame = CGRectMake(x,y,w,h);
x = x + 70;
if (([titleString length] >= 6) && ([titleString length] <=10))
w = 70;
btn.frame = CGRectMake(x,y,w,h);
x = x + 90;
if(([titleString length] >= 11) && ([titleString length] <=15))
w = 105;
btn.frame = CGRectMake(x,y,w,h);
x = x + 120;
if([titleString length] >= 16)
w = 120;
btn.frame = CGRectMake(x,y,w,h);
x = x + 140;
[btn setTitle:titleString forState: UIControlStateNormal];
[btn setTag:i];
[self.view addSubview:btn];
查看示例图片,
image-2 http://www.freeimagehosting.net/uploads/b6e0f234dc.png image-1 http://www.freeimagehosting.net/uploads/6b3daab12f.png
那么这是否可以设置取决于文本的确切按钮框架大小?请指导我。
谢谢
【问题讨论】:
【参考方案1】:非常简单的解决方案:
TestButton.titleEdgeInsets = UIEdgeInsetsMake(0, 8, 0, 8);
[TestButton sizeToFit];
float width = TestButton.frame.size.width + 20;
TestButton.frame = CGRectMake(TestButton.frame.origin.x, TestButton.frame.origin.y, width, 40);
【讨论】:
这让我意识到我需要重置整个框架(而不是尝试更新现有框架的宽度值,它是只读的)。谢谢。 感谢您的解决方案。节省了我的时间。答对了! (Y)【参考方案2】:我得到了答案,我的工作代码是,
float x=0;
for(int i = 100; i < 110; i++)
btn = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
UIImage * img = [UIImage imageNamed:@"Round_Rect4.png"];
[btn setBackgroundImage:img forState:UIControlStateSelected];
titleString = [titleArray objectAtIndex:i-100]; // get button title
CGSize fontSize = [titleString sizeWithFont:[UIFont systemFontOfSize:12.0]];
CGRect currentFrame = btn.frame;
CGRect buttonFrame = CGRectMake(x, currentFrame.origin.y, fontSize.width + 22.0, fontSize.height + 12.0);
[btn setFrame:buttonFrame];
x = x + fontSize.width + 35.0;
[btn setTitle:titleString forState: UIControlStateNormal];
[btn setTag:i];
[self.view addSubview:btn];
【讨论】:
为什么不[titleString sizeWithFont:btn.font]
?
如果我使用了 [titleString sizeWithFont:btn.font] 此代码并显示警告。(不推荐使用字体)。
@Pugal 使用 [titleString sizeWithFont:[btn titleLabel].font]
代替,以避免警告。【参考方案3】:
从 Apples Bubble Level 源代码复制粘贴。
这是你想要的线
UIImage *newImage = [image stretchableImageWithLeftCapWidth:10 topCapHeight:10];
完整代码在这里
- (UIButton *)buttonWithTitle:(NSString *)title target:(id)target selector:(SEL)inSelector frame:(CGRect)frame image:(UIImage*)image
UIButton *button = [[UIButton alloc] initWithFrame:frame];
button.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
[button setTitle:title forState:UIControlStateNormal & UIControlStateHighlighted & UIControlStateSelected];
[button setTitleColor:[UIColor blackColor] forState:UIControlEventTouchDown];
UIImage *newImage = [image stretchableImageWithLeftCapWidth:10 topCapHeight:10];
[button setBackgroundImage:newImage forState:UIControlStateNormal];
[button addTarget:target action:inSelector forControlEvents:UIControlEventTouchUpInside];
button.adjustsImageWhenDisabled = YES;
button.adjustsImageWhenHighlighted = YES;
[button setBackgroundColor:[UIColor clearColor]]; // in case the parent view draws with a custom color or gradient, use a transparent color
[button autorelease];
return button;
【讨论】:
【参考方案4】:[btn sizeToFit]
【讨论】:
嘻嘻。但这并没有真正考虑到背景图片有圆角。 否;你必须自己做——比如btn.contentEdgeInsets = (UIEdgeInsets).left=8,.right=8
以上是关于如何根据 iphone 中的文本大小动态增加按钮宽度?的主要内容,如果未能解决你的问题,请参考以下文章