【问题标题】:How to Render a rotated UIlabel如何渲染旋转的 UIlabel
【发布时间】:2012-10-02 07:41:14
【问题描述】:

我目前正在为 iPad 开发像 photoshop 这样的应用程序,当我想要展平我的图层数组时,DrawTextInRect 不会保留我的UILabel 的变换 (CAAffineTransformMakeRotation)。有人知道吗?

【问题讨论】:

    标签: iphone ios uilabel transform render


    【解决方案1】:

    将您的文本绘制到另一个上下文中并获取它的 CGImage,然后查看在旋转的上下文中绘制图像是否有效(应该)。

    编辑:我自己没有这样做,我相信它会起作用。执行此操作时,标签可能不应该插入到视图中,但无论如何它可能会以这种方式工作。您可能需要进行实验:

    UIGraphicsBeginImageContextWithOptions( myLabel.bounds.size, NO, 0); // 0 == [UIScreen mainScreen].scale
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    [myLabel.layer renderInContext:context];
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    

    【讨论】:

    • 我尝试在自定义 UIlabel 类中创建一个函数,该函数返回我的标签图像。当我将函数的代码放在可以工作的主上下文中但在上下文中创建我的函数时不是。我不明白为什么...如何在当前上下文中创建新上下文?
    【解决方案2】:

    将上下文转换为标签的原点,然后将标签的变换应用到上下文。

    下面的代码是用 Swift 编写的。

    let context = UIGraphicsGetCurrentContext()!
    
    context.saveGState()
    
    let t = myLabel.transform
    let translationPt = CGPoint.init(x: myLabel.frame.origin.x, y: myLabel.frame.origin.y)
    context.translateBy(x: translationPt.x, y: translationPt.y)
    
    context.concatenate(t)
    
    myLabel.layer.render(in: context)
    
    context.restoreGState()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-27
      • 2023-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-13
      • 1970-01-01
      相关资源
      最近更新 更多