当我启用模拟器调试选项"颜色混合图层"时然后显示不同的结果如下:
对不起,我不能发布图片:
https://i.stack.imgur.com/NLRqh.jpg https://i.stack.imgur.com/cOQso.jpg看起来带有中文字符的标签会有一个额外的子层(不是子视图)
当我在一个视图中添加两个UILabel
时,一个带有英文字符,一个带有中文字符,它们的层次结构略有不同。
0x7fd3b972c020
是英文字符
UILabel
。(lldb) po [[0x7fd3b972c020 layer] sublayers]
nil
(lldb) po [0x7fd3b972c020 layer]
<_UILabelLayer:0x7fd3b972c2b0; position = CGPoint (187.5 333.75); bounds = CGRect (0 0; 133 20.5); delegate = <UILabel: 0x7fd3b972c020; frame = (121 323.5; 133 20.5); text = 'This is just a test.'; autoresize = RM+BM; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7fd3b972c2b0>>; contents = <CABackingStore 0x7fd3bb0e1530 (buffer [266 41] BGRX8888)>; opaque = YES; allowsGroupOpacity = YES; rasterizationScale = 2; contentsScale = 2>
0x7fd3b95208f0
为UILabel
,有中文字符:
(lldb) po [[0x7fd3b95208f0 layer] sublayers]
<CALayerArray 0x7fd3bb0e0c90>(
<_UILabelContentLayer:0x7fd3bb0e0a60; position = CGPoint (68.25 11); bounds = CGRect (0 0; 140.5 29); contents = <CABackingStore 0x7fd3bb0e1180 (buffer [281 58] BGRA8888)>; allowsGroupOpacity = YES; rasterizationScale = 2; contentsScale = 2>
)
(lldb) po [0x7fd3b95208f0 layer]
<_UILabelLayer:0x7fd3b950ba70; position = CGPoint (187.5 361.75); bounds = CGRect (0 0; 136 20.5); delegate = <UILabel: 0x7fd3b95208f0; frame = (119.5 351.5; 136 20.5); text = '这只是一个测试。'; autoresize = RM+BM; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7fd3b950ba70>>; sublayers = (<_UILabelContentLayer: 0x7fd3bb0e0a60>); opaque = YES; allowsGroupOpacity = YES; rasterizationScale = 2; contentsScale = 2>
每个视图都启用了opaque
,背景颜色为白色。
红色的中文字符解决方案:
label.layer.maskToBounds = true