使 UILabel 看起来像 CSS 显示:内联

发布于 11-30 14:53 字数 527 浏览 1 评论 0原文

好吧,这是我的问题。我有一组相当大的 UIButtons,其中有一个 UIImage 设置为背景。现在我想在这些按钮内添加标签。但我目前正在实现的设计使用“花哨”标签,其中标签的背景颜色是半透明的,并在文本周围折叠。

我的第一个想法是使用 UIWebview 来实现这一点并且它有效,但是添加大量 UIWebview 需要很长时间才能显示标签。

下一个解决方案是使用 UILabel,性能会好很多倍。但对于我的一生,我无法弄清楚如何让视觉风格与设计相匹配。

所以基本上我希望使 UILabel 的行为像具有 CSS 规则 display: inline; 的 HTML 元素一样。风格定义。

使用 UILabel 的内置参数是否可以实现这一点?如果没有,我可能会使用 UIWebView 解决方案,但使用 UILabel 当然会更好。

这是一个屏幕截图,应该可以更好地解释它

顶部是默认 UILabel 的行为方式,底部是我希望它的行为方式。

Ok, here’s my problem. I have a rather large set of UIButtons that have a UIImage set as a background. Now I want to add labels inside those buttons. But the design I’m currently implementing uses “fancy” labels where the background color of the label is semi transparent and collapses around the text.

My first idea was to use a UIWebview to make that happen and it works, but adding a lot of UIWebview’s takes a really long time for the labels to show up.

Next solution would be to use an UILabel and the performance is times better. But for the life of me, I cannot figure out, how to get the visual style to match the design.

So basically I’m looking to make an UILabel behave like an HTML element that has as CSS rule display: inline; style defined.

Is this even possible with the built-in parameters of an UILabel? If not, I’m probably going to use the UIWebView solution, but it would be of course much niftier to use the UILabel’s.

Here’s a screenshot that should explain it somewhat better

Top is how the default UILabel acts and bottom is how I want it to act.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

鲜血染红嫁衣2024-12-07 14:53:50

您还没有完全描述您的文本的风格,但在我看来您想要一种“发光”效果。这可以使用 QuartzCore 在标签上产生阴影效果来完成。请注意,单个阴影通常无法做到这一点,因此这里是一个具有四个标签和四个阴影的示例:

for (int i = 0; i < 4; i++) {
    UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 280, 30)];
    [label setBackgroundColor:[UIColor clearColor]];
    [label setTextColor:[UIColor colorWithRed:1 green:1 blue:1 alpha:1]];
    [label setText:@"This is some glowing text"];
    [[label layer] setShadowColor:[[UIColor colorWithRed:1 green:1 blue:1 alpha:1] CGColor]];
    CGSize shadowOff = CGSizeMake(0, 0);
    if (i == 0) shadowOff = CGSizeMake(3, 3);
    else if (i == 1) shadowOff = CGSizeMake(3, -3);
    else if (i == 2) shadowOff = CGSizeMake(-3, -3);
    else if (i == 3) shadowOff = CGSizeMake(-3, 3);
    [[label layer] setShadowOffset:shadowOff];
    [[label layer] setShadowOpacity:0.5];
    [[label layer] setShadowRadius:3];
    [[label layer] setMasksToBounds:NO];
    [self.view addSubview:[label autorelease]];
}

显然,可以修改阴影不透明度、半径等以获得不同级别的“发光”。

You haven't fully described the style that your text has, but it sounds to me that you want a sort of "glow" effect. This can be done using QuartzCore for a shadow effect on the labels. Note that a single shadow normally doesn't do it, so here is an example with four labels and four shadows:

for (int i = 0; i < 4; i++) {
    UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 280, 30)];
    [label setBackgroundColor:[UIColor clearColor]];
    [label setTextColor:[UIColor colorWithRed:1 green:1 blue:1 alpha:1]];
    [label setText:@"This is some glowing text"];
    [[label layer] setShadowColor:[[UIColor colorWithRed:1 green:1 blue:1 alpha:1] CGColor]];
    CGSize shadowOff = CGSizeMake(0, 0);
    if (i == 0) shadowOff = CGSizeMake(3, 3);
    else if (i == 1) shadowOff = CGSizeMake(3, -3);
    else if (i == 2) shadowOff = CGSizeMake(-3, -3);
    else if (i == 3) shadowOff = CGSizeMake(-3, 3);
    [[label layer] setShadowOffset:shadowOff];
    [[label layer] setShadowOpacity:0.5];
    [[label layer] setShadowRadius:3];
    [[label layer] setMasksToBounds:NO];
    [self.view addSubview:[label autorelease]];
}

Obviously the shadow opacity, radius, etc can be modified for a different level of "glow."

萝莉病2024-12-07 14:53:50

据我所知,您需要在这里弄清楚两件事:

  1. 您想要在 UILabel 中使用的文本的渲染大小。可以使用 -[NSString sizeWithFont:] 来计算。还有很多其他相关方法。请参阅 NSString UIKit 添加参考。

  2. 您需要的第二件事是 UILabel 的背景颜色应该完美地折叠在文本周围。为此,您需要设置一个可拉伸图像作为 UILabel 的背景。请参阅-[UIImagestretchableImageWithLeftCapWidth:topCapHeight:]。请参阅http://idevrecipes.com/2010/12/08/stretchable -images-and-buttons/ 有关可拉伸图像如何工作的示例。

From what I can understand, there are two things you need to figure out here:

  1. The rendered size of the text you want to use in your UILabel. That can be computed with -[NSString sizeWithFont:]. There are a bunch of other related methods. See NSString UIKit Additions Reference.

  2. The second thing you need is that your UILabel's background colour should collapse perfectly around the text. For that, you need to set a stretchable image as the background for the UILabel. See -[UIImage stretchableImageWithLeftCapWidth:topCapHeight:]. See http://idevrecipes.com/2010/12/08/stretchable-images-and-buttons/ for an example of how stretchable images work.

善良天后2024-12-07 14:53:50

我设法找到了一个几乎按预期工作的解决方案。它不会折叠所有单行,但它折叠文本周围的框,这对我来说已经足够好了。

这是对我有用的最终代码:

UILabel *buttonTitle = [[UILabel alloc] initWithFrame:CGRectMake(4, 4, 176, 120)];
buttonTitle.text = @"Button text";
buttonTitle.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.65];
buttonTitle.textColor = [UIColor whiteColor];
buttonTitle.textAlignment = UITextAlignmentLeft;
buttonTitle.numberOfLines = 0;
buttonTitle.lineBreakMode = UILineBreakModeWordWrap;
buttonTitle.adjustsFontSizeToFitWidth = NO;
[buttonTitle sizeToFit];

I’ve managed to find a solution that almost works as intended. It doesn’t collapsed around all the single lines, but it collapses the box around the text and that,s good enough for me at the moment.

Here’s the final code that does the trick for me:

UILabel *buttonTitle = [[UILabel alloc] initWithFrame:CGRectMake(4, 4, 176, 120)];
buttonTitle.text = @"Button text";
buttonTitle.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.65];
buttonTitle.textColor = [UIColor whiteColor];
buttonTitle.textAlignment = UITextAlignmentLeft;
buttonTitle.numberOfLines = 0;
buttonTitle.lineBreakMode = UILineBreakModeWordWrap;
buttonTitle.adjustsFontSizeToFitWidth = NO;
[buttonTitle sizeToFit];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文