如何将 UILabel 绘制到上下文中

发布于 2024-11-05 19:10:10 字数 259 浏览 2 评论 0原文

我正在尝试将 UIlabel 绘制到新上下文中的特定位置。我知道您可以轻松地将 NSString 绘制到上下文中,但我想保留 UILabel 的文本大小、颜色和自动换行样式。感谢您的帮助!

 UIGraphicsBeginImageContext(targetSize);   

 // here is where I want to draw UILabel to context

 UIGraphicsEndImageContext();   

I'm trying to draw a UIlabel into a new Context, at a specific location. I know you can draw just the NSString into a context easily, but I want to retain the text size, color, and word wrap style of the UILabel. Thanks for your help!

 UIGraphicsBeginImageContext(targetSize);   

 // here is where I want to draw UILabel to context

 UIGraphicsEndImageContext();   

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁吢 2024-11-12 19:10:10

您可以使用 -renderInContext 绘制任何视图,但如果您只需要保留文本大小、颜色和自动换行样式,您可以简单地使用-drawInRect:withFont:lineBreakMode:alignment: 自定义所有选项。

[label.textColor set];
[label.text drawInRect:label.bounds
              withFont:label.font
         lineBreakMode:label.lineBreakMode
             alignment:label.textAlignment];

还有 UILabel 中的 -drawTextInRect: 方法,但 Apple 表示“您不应该调用此方法”直接方法。”

You could draw any view using -renderInContext:, but if all you need is just retain the text size, color and word wrap style, you could simply use -drawInRect:withFont:lineBreakMode:alignment: to customize all options.

[label.textColor set];
[label.text drawInRect:label.bounds
              withFont:label.font
         lineBreakMode:label.lineBreakMode
             alignment:label.textAlignment];

There is also a -drawTextInRect: method in UILabel, but Apple says "You should not call this method directly."

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文