如何更好地将UIView的背景颜色设置为透明?

3

我知道您可以将UIView的backgroundColor设置为透明,如下所示:

UILabel *aLabel = [[UILabel alloc] init];
aLabel.backgroundColor = [UIColor clearColor];

但是根据一些文章所说,[UIColor clearColor] 的性能并不好,那么,有什么更好的方法将一个 UIView 的背景色设置为透明呢?

3个回答

15

UIColor clearColor 之所以会慢是因为它是透明的。任何需要系统计算图层混合的操作都会比不透明的操作慢。如果你需要它是透明的,就使用 [UIColor clearColor] 。如果可以避免使用透明的颜色,那就要避免使用。


0
有时候在视图的 drawRect 方法里,可以使用 NSString 的 drawAtPoint 方法,而不是使用 UILabel。
NSString *text = @"hello world";
[text drawAtPoint:CGPointMake(10, 10) forWidth:165 withFont:[UIFont boldSystemFontOfSize:16] lineBreakMode:UILineBreakModeTailTruncation];

-2
你可以将背景设置为任何具有0.0 alpha值的颜色。

这将具有与[UIColor clearColor]相同的性能。 "Clear"的意思是“透明黑色”。 - Rob Napier

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接