UILabel设置透明背景颜色

UILabel设置透明背景颜色

本文介绍了UILabel设置透明背景颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为我的视图添加一个透明背景的黑色标签。

  //添加标签
UILabel * label = [[UILabel alloc] init];
[label setFrame:CGRectMake(124,312,72,35)];
[label setText:@Yay!];
[label setTextAlignment:UITextAlignmentCenter];
[[self view] addSubview:label];
[label release];

当我添加标签时,它总是在白色背景上出现黑色文本。我已经查看了NIB,我唯一的方法,我可以看到,使这项工作设置背景>颜色>不透明度为0或在Xcode:

  [label setBackgroundColor:[UIColor clearColor]];这是正确的做法吗?









$ b

Cheers Gary

解决方案

我可以确认使用

  [label setBackgroundColor:[UIColor clearColor]]; 

可以工作。



此外,在Interface Builder中设置背景颜色时,您可以将背景颜色的不透明度设置为0。请确保您不将标签本身的不透明度设置为0,否则文本也将是透明的。


I am looking to add a black label with a transparent background to my view (see below)

// ADD LABEL
UILabel *label = [[UILabel alloc] init];
[label setFrame:CGRectMake(124, 312, 72, 35)];
[label setText:@"Yay!"];
[label setTextAlignment:UITextAlignmentCenter];
[[self view] addSubview:label];
[label release];

When I add the label it always comes out black text on a white background. I have looked in the NIB and the only way I can see to make this work is set the background > color > opacity to 0, or in Xcode:

[label setBackgroundColor:[UIColor clearColor]];

Is this the right way to do this?

Cheers Gary

解决方案

I can confirm that creating a label programatically with background color set using

[label setBackgroundColor:[UIColor clearColor]];

will work. I'm using this in one of my apps.

Also, when you set the background color in Interface Builder, you can set the opacity of the background color to 0. Make sure that you don't set the opacity of the label itself to 0, or the text will also be transparent.

这篇关于UILabel设置透明背景颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 03:31
查看更多