【问题标题】:Text Antialiasing in CALayer with Opaque Background具有不透明背景的 CALayer 中的文本抗锯齿
【发布时间】:2012-03-08 01:38:49
【问题描述】:

正如其他人所做的那样,我正在尝试在 CALayer 支持的视图上获得看起来不错的文本。我发现的最相关的线程是Faking Subpixel Antialiasing on Text with Core Animation

在那篇文章的开头,它声明“现在,对于能够为其文本设置不透明背景的人(使用 setBackgroundColor: 调用或 Interface Builder 中的等效设置),此问题不存在问题太大了。”但是,当我在 IB 中设置文本框的背景并让它绘制背景并对其单元格执行相同操作时,我仍然遇到同样的问题(使用图层时没有抗锯齿)。这些属于也绘制其背景的 NSBox。

知道我不应该做什么吗?谢谢

【问题讨论】:

    标签: objective-c macos core-animation


    【解决方案1】:

    如果您只是想在不透明的背景上获得清晰的文本并用 CATextLayer 将头撞到墙上 - 放弃并使用 NSTextField 并禁用 inset 并将 linefragmentpadding 设置为 0。示例代码很快,但您应该能够轻松翻译...

    var testV = NSTextView()
    testV.backgroundColor = NSColor(calibratedRed: 0.73, green: 0.84, blue: 0.89, alpha: 1)
    testV.frame = CGRectMake(120.0, 100.0, 200.0, 30.0)
    testV.string = "Hello World!"
    testV.textContainerInset = NSZeroSize
    testV.textContainer!.lineFragmentPadding = 0
    self.addSubview(testV)
    

    对我来说显示文本相当于:

    var testL = CATextLayer()
    testL.backgroundColor = NSColor(calibratedRed: 0.73, green: 0.84, blue: 0.89, alpha: 1).CGColor
    testL.bounds = CGRectMake(0.0, 0.0, 200.0, 30.0)
    testL.position = CGPointMake(100.0, 100.0)
    testL.string = "Hello World!"
    testL.fontSize = 14
    testL.foregroundColor = NSColor.blackColor().CGColor
    self.layer!.addSublayer(testL)
    

    【讨论】:

      【解决方案2】:

      不确定这是否有助于解决您的问题,但是当您在图层中进行自定义绘图时,您必须使用具有不透明颜色的 CGContextFillRect(),然后在该矩形顶部绘制字符串以获得亚像素抗锯齿。

      设置 layer.backgroundColor 不够好,可能是因为 layer.backgroundColor 可以在不重新绘制图层内容的情况下更改。

      【讨论】:

      • 你有一个例子来说明如何绘制字符串以使其不会呈现不佳 - 我的尝试没有很好地呈现。
      • @Sam 确保将 layer.contentsScale 设置为 [UIScreen mainScreen].scale
      • 美丽。这非常有效。我一直将 layer.backgroundColor 设置为完全不透明的颜色,但在绘制文本之前我没有填充该颜色。我一这样做,文本就完美呈现了。谢谢!
      猜你喜欢
      • 2014-02-18
      • 1970-01-01
      • 2010-11-20
      • 1970-01-01
      • 2015-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多