【问题标题】:Add text to CALayer将文本添加到 CALayer
【发布时间】:2011-01-13 15:54:11
【问题描述】:

是否可以将UILabel 添加到CALayer 而无需子类化并在drawInContext: 中绘制它?

谢谢!

【问题讨论】:

    标签: iphone


    【解决方案1】:
    CATextLayer *label = [[CATextLayer alloc] init];
    [label setFont:@"Helvetica-Bold"];
    [label setFontSize:20];  
    [label setFrame:validFrame];
    [label setString:@"Hello"];
    [label setAlignmentMode:kCAAlignmentCenter];
    [label setForegroundColor:[[UIColor whiteColor] CGColor]];
    [layer addSublayer:label];
    
    [label release];
    

    【讨论】:

    • 恕我直言,这是一个比自定义 CALayer 或图层委托更简单的解决方案,只要您不需要基本文本绘图。 CATextLayer 已经知道如何绘制文本,因此无需重新发明轮子。
    • @Mshah2 为什么文本带有非抗锯齿效果?文字看起来像是被放大了
    • 您需要调整 CALayer 的内容比例,否则您会在视网膜 (@2x) 或@3x 设备上看到模糊的文本。使用:label.contentsScale = [UIScreen mainScreen].scale;
    • 像往常一样,最有用的答案在底部。感谢 StackOverflow!
    • @bicycle,加油,如果不是 Stack Overflow,我们都会失业
    【解决方案2】:

    我认为您不能将 UIView 子类添加到 CALayer 对象。但是,如果您想在 CALayer 对象上绘制文本,可以使用 NSString UIKit additions 中提供的绘制函数来完成,如下所示。虽然我的代码是在委托的 drawLayer:inContext 方法中完成的,但同样可以在子类的 drawInContext: 方法中使用。您是否想利用任何特定的 UILabel 功能?

    - (void) drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx {
      CGContextSetFillColorWithColor(ctx, [[UIColor darkTextColor] CGColor]);
    
      UIGraphicsPushContext(ctx);
      /*[word drawInRect:layer.bounds 
              withFont:[UIFont systemFontOfSize:32] 
         lineBreakMode:UILineBreakModeWordWrap 
             alignment:UITextAlignmentCenter];*/
    
      [word drawAtPoint:CGPointMake(30.0f, 30.0f) 
               forWidth:200.0f 
               withFont:[UIFont boldSystemFontOfSize:32] 
          lineBreakMode:UILineBreakModeClip];
    
      UIGraphicsPopContext();
    }
    

    【讨论】:

    • [[UIColor darkTextColor] set] 也适用于推送图形上下文时
    • @Deepak Danduprolu 这对我有用,但我想知道为什么要调用 push 和 pop 函数来使其工作
    【解决方案3】:

    只是为了记录我的方法,我在 Swift 4+ 中这样做了:

    let textlayer = CATextLayer()
    
    textlayer.frame = CGRect(x: 20, y: 20, width: 200, height: 18)
    textlayer.fontSize = 12
    textlayer.alignmentMode = .center
    textlayer.string = stringValue
    textlayer.isWrapped = true
    textlayer.truncationMode = .end
    textlayer.backgroundColor = UIColor.white.cgColor
    textlayer.foregroundColor = UIColor.black.cgColor
    
    caLayer.addSublayer(textlayer) // caLayer is and instance of parent CALayer 
    

    【讨论】:

      【解决方案4】:

      你的 UILabel 后面已经有一个 CALayer。如果您将多个 CALayer 放在一起,您只需将 UILabel 的层添加为其中一个的子层(通过使用其layer 属性)。

      如果在您想要的图层中直接绘制文本,那么 Deepak 指向的 UIKit NSString 附加项就是您要走的路。例如,Core Plot framework 有一个独立于 Mac / iPhone 平台的 CALayer 子类,它执行文本渲染,CPTextLayer

      【讨论】:

      • 嘿,布拉德,我实际上曾经使用过这种技术,(将 UILabel 的层添加到 CALayer 中)似乎它在 iOS 8 基础 SDK 中停止工作了?我已经开始重建我创建的导出视频的旧应用程序,并且父 CALayer 的子层似乎与这个旧代码保持为零。可能有其他问题,继续寻找。
      • 已修复。对于寻找如何使这项工作针对 iOS 8 Base SDK 编译的其他任何人,请在将标签层添加到另一个 CALayer 之前调用 [label.layer display]。信用:stackoverflow.com/questions/23171790/…
      【解决方案5】:

      添加一个 CATextLayer 作为子层并设置字符串属性。这将是最简单的,您可以轻松地使用布局管理器使其非常通用。

      【讨论】:

      【解决方案6】:

      下面的答案很好,只要确保你添加,否则你的文字会模糊:

      textLayer.contentsScale = UIScreen.main.scale
      

      Swift 的最终代码:

      let textLayer = CATextLayer()
      textLayer.frame = CGRect(x: 0, y: 0, width: 60, height: 15)
      textLayer.fontSize = 12
      textLayer.string = "my text"
      textLayer.foregroundColor = UIColor.red.cgColor
      textLayer.contentsScale = UIScreen.main.scale
      

      【讨论】:

        【解决方案7】:

        如果要添加另一个子层,请务必记住删除以前的子层,以防止重复视图:

        if let sublayers = layer.sublayers {
            for sublayer in sublayers {
                sublayer.removeFromSuperlayer()
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2023-04-05
          • 1970-01-01
          • 2012-07-30
          • 2014-02-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多