Joe*_*ang 6 core-graphics uitableview uikit uilabel ios
我有一个自定义单元格的表格视图,我尝试通过Color Blended Layer
在模拟器中检查单元格内的所有子视图为绿色来优化它.
当UILabel
单元格中的背景设置为白色时:
let title = UILabel()
contentView.addSubview(title)
title.background = UIColor.whiteColor()
title.text = "Hi, how are you?"
Run Code Online (Sandbox Code Playgroud)
这个UILabel子视图在模拟器中会变成绿色,这很好.但是,如果我将文本更改为某些中文:
title.text = "??"
Run Code Online (Sandbox Code Playgroud)
UILabel子视图将变为红色.这篇SO帖子提供了有关情况的一些解释.实际上有解决方案吗?
使用CATextLayer
而不是UILabel
. 并且不要忘记将其opaque
属性设置为true
.
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {\n let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)\n\n // You should definitely put layer handling logic into \n // UITableViewCell subclass. I\'m omitting it for clarity\n let layer = cell.contentView.layer\n\n let textLayer = CATextLayer()\n textLayer.string = "\xe4\xbd\xa0\xe5\xa5\xbd"\n textLayer.bounds = CGRect(x: 0, y: 0, width: 100, height: 50)\n textLayer.position = CGPoint(x: 50, y: 25)\n textLayer.opaque = true\n\n layer.addSublayer(textLayer)\n\n return cell\n}\n
Run Code Online (Sandbox Code Playgroud)\n\nUPD:如果黑色背景不是您想要的,问题就会变得有点棘手,但仍然可以解决。
\n\n您所需要的只是将两行代码注入到绘图机制中。有两种方法可以实现此目的:
\n\n使用 a或delegate
a 的CALayer
制作 的子类CATextLayer
。
就我们而言,我认为后者更可取。因此,不要CATextLayer
在下面的示例中使用此类:
class OpaqueTextLayerWithCustomizableBackgroundColor: CATextLayer {\n override func drawInContext(ctx: CGContext) {\n if let color = backgroundColor {\n CGContextSetFillColorWithColor(ctx, color)\n CGContextFillRect(ctx, bounds);\n }\n super.drawInContext(ctx)\n }\n}\n
Run Code Online (Sandbox Code Playgroud)\n
归档时间: |
|
查看次数: |
669 次 |
最近记录: |