gpt4 book ai didi

ios - 启用颜色混合图层后,是否可以将带有非英文字符的 UILabel 设置为绿色?

转载 作者:可可西里 更新时间:2023-11-01 03:53:33 28 4
gpt4 key购买 nike

我有一个带有自定义单元格的 TableView ,当在模拟器中选中 Color Blended Layer 时,我尝试通过将单元格内的所有 subview 设为绿色来优化它。

当单元格中的 UILabel 背景设置为白色时:

let title = UILabel()
contentView.addSubview(title)
title.background = UIColor.whiteColor()
title.text = "Hi, how are you?"

这个 UILabel subview 在模拟器中会变成绿色,这很好。但是,如果我将文本更改为一些中文:

title.text = "你好"

UILabel subview 将变为红色。这SO post提供有关情况的一些解释。实际上有解决方案吗?

最佳答案

使用 CATextLayer 而不是 UILabel。并且不要忘记将其 opaque 属性设置为 true

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)

// You should definitely put layer handling logic into
// UITableViewCell subclass. I'm omitting it for clarity
let layer = cell.contentView.layer

let textLayer = CATextLayer()
textLayer.string = "你好"
textLayer.bounds = CGRect(x: 0, y: 0, width: 100, height: 50)
textLayer.position = CGPoint(x: 50, y: 25)
textLayer.opaque = true

layer.addSublayer(textLayer)

return cell
}

UPD:如果黑色背景不是您想要的,问题就会变得有点棘手,但仍然可以解决。

您只需将两行代码注入(inject)绘图机制即可。有两种方法可以实现这一点:

  • 使用CALayer

  • 委托(delegate)
  • 创建CATextLayer 的子类。

在我们的例子中,我认为后者更可取。因此,在下面的示例中使用此类代替 CATextLayer:

class OpaqueTextLayerWithCustomizableBackgroundColor: CATextLayer {
override func drawInContext(ctx: CGContext) {
if let color = backgroundColor {
CGContextSetFillColorWithColor(ctx, color)
CGContextFillRect(ctx, bounds);
}
super.drawInContext(ctx)
}
}

关于ios - 启用颜色混合图层后,是否可以将带有非英文字符的 UILabel 设置为绿色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35308501/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com