为iphone应用程序设置UILabel的透明背景
问题描述:
我在UITableView中有一个UILabel。这里是我写在cellForRowAtIndexPath的代码为iphone应用程序设置UILabel的透明背景
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectMake(30.0, 5.0, 250.0, 35.0) reuseIdentifier:CellIdentifier] autorelease];
}
CGRect rect = CGRectMake(30.0, 5.0, 250.0, 35.0);
UILabel *label = [[UILabel alloc] initWithFrame:rect];
label.opaque = NO;
label.font = [UIFont systemFontOfSize:14];
label.numberOfLines = 2;
label.textAlignment = UITextAlignmentCenter;
label.textColor = [UIColor darkGrayColor];
label.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0];
[label setText:@"Test Message"];
[cell addSubview:label];
[label release];
cell.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
我的意图是设置单元格和UILabel的背景为透明。
但它不工作。任何一个可以告诉我什么,我在这里失去了
感谢
桑迪普
答
更改以下行:
label.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0];
这样:
label.backgroundColor = [UIColor clearColor];
答
对于使用Xamarin那些.iOS很明显:
UILabel myLabel = new UILabel();
myLabel.Text = "Hello world!";
myLabel.BackgroundColor = UIColor.Clear;
答
[label setBackGroundColor:[UIColor clearColor]];
要为标签
设置clearBackGround颜色现在,你已经指出我们在正确的方向(谢谢!)我要补充的是'label.backgroundColor =零; '更容易记住,做同样的事情。 (BG颜色是超类UIView的一个属性,每个类ref,“默认值是nil,这会导致透明的背景颜色。”UILabel子类可能最初将它设置为白色,这就是为什么您必须显式更改它回到无/清除。) – Wienke 2012-09-27 15:13:06
然而,经过测试,事实证明你应该坚持奥登的原始答案。首先,忽略backgroundColor可以正常工作,但如果随后更改标签的文本,则新文本将叠加*在旧的文本上。很奇怪。 – Wienke 2012-09-27 19:50:21
将值设置为零也是我的第一个猜测,但它导致整个标签(文本和背景)完全变黑。 [UIColor clearColor]完美运作。 – 2013-05-16 00:45:53