UILabel/NSTextView 与 CATextLayer

发布于 2024-10-12 08:21:08 字数 367 浏览 4 评论 0原文

我有一个 iOS 应用程序,我正在将其转换为 iOS/Mac 应用程序。在 iOS 版本中,我使用 UILabels 作为文本。显然,我无法在 mac 上做到这一点。我可以看到两个选项:

选项 1:在 iOS 上使用 UILabels,在 Mac 上使用 NSTextView。

选项 2:在两个平台上使用 CATextLayers,作为尽可能在这两个平台上工作的总体策略的一部分。

显然,选项 2 有一个很大的优点,那就是需要维护的代码更少。我的问题是——选项 1 有什么我应该注意的优点吗?特别是,CATextLayer是否有UILabel和NSTextView没有的缺点?

编辑:这是一个游戏。用户从不输入文本——但游戏确实会输出一些文本,例如分数。

I have an iOS app that I am converting to an iOS/Mac app. In the iOS version, I used UILabels for my text. Obviously, I can't do that on the mac. I can see two options:

Option 1: Use UILabels on iOS and NSTextView on the Mac.

Option 2: Use CATextLayers on both platforms, as part of a general strategy to make as much as possible work on both of them.

Obviously, option 2 has a big advantage that there would be less code to maintain. My question is -- does option 1 have any advantages I should be aware of? In particular, does CATextLayer have drawbacks that UILabel and NSTextView don't have?

EDIT: It's a game. The user never enters text -- but the game does output some, scores for example.

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

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

发布评论

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

评论(2

贱贱哒 2024-10-19 08:21:08

这取决于您打算如何使用文本以及您正在编写的应用程序类型。如果文本主要是静态的并用作 UI 中的标签或对话框,那么 NSTextField 应该可以正常工作。如果您需要精美的动画,那么CATextLayer可能更合适。如果您正在编写游戏(例如纸牌游戏),UI 往往会更加相似,并且使用 CATextLayer 和 CoreAnimation 会更有意义。如果您正在编写更通用的应用程序(例如聊天客户端),则用户与应用程序交互的界面和方式有很大不同,并且使用 NSTextFieldsNSTextViews 可能更有意义

同样,这一切都取决于您要创建的应用程序的类型,但如果它是更传统的应用程序(例如聊天客户端示例),那么我认为使用 CATextLayer 并必须在界面中创建所有内容以编程方式,而不是使用 NSTextField 并将内容放置在 nib 文件中,将意味着数百行代码。另一方面,如果应用程序更像是纸牌游戏示例,并且层和属性的尺寸定义方式考虑了应用程序运行的平台,那么您可能可以节省大量代码。

根据记录,NSTextField 可能是与 UILabel 最接近的等效 AppKit 类。 NSTextView 是一个更重量级的类,通常只有在您想要进行完整段落和更高级的排版时才需要它。使 NSTextField 更轻的部分原因是它们倾向于共享一个不可见的单个 NSTextView (或其超类 NSText)作为字段编辑器,这有助于文本输入。由于应用程序中只有一个文本字段可以同时获得焦点,因此它可以共享一个通用字段编辑器以帮助提高性能。

It depends on how you intend to use the text and the type of application you're writing. If the text will primarily be static and used as labels or dialogs in the UI, then NSTextField ought to work okay. If you need fancy animations, then CATextLayer may be more appropriate. If you are writing a game (solitaire, for example), the UIs will tend to be more similar and using CATextLayer and CoreAnimation would make sense. If you're writing a more general application like a chat client, the interfaces and ways the user interacts with the app are vastly different, and it would probably make more sense to use NSTextFields or NSTextViews.

Again, this all depends on the type of app you're creating, but if it's a more traditional app like the chat client example, then I would think that using CATextLayer and having to create everything in the interface programmatically, rather than using NSTextField and laying stuff out in a nib file, would mean hundreds of more lines of code. On the other hand, if the app is more like the solitaire game example, and the dimensions of the layers and properties are defined in a way that takes into account which platform the app is running on, then you could probably save a lot of code.

For the record, NSTextField is probably the closest equivalent AppKit class to UILabel. NSTextView is a more heavyweight class that you generally only need if you want to do full paragraphs and more advanced typesetting. Part of what makes NSTextField lighter weight is that they tend to share an invisible, single NSTextView (or its superclass, NSText) as the field editor, that helps with text entry. Since only one text field in an app can have focus at one time, it can share a common field editor to help improve performance.

等数载,海棠开 2024-10-19 08:21:08

您还需要注意一个缺点(或者将来的人会看到这个帖子)。使用 UILabel 时,所有可访问性都可以通过 UIAccessibility 免费获得。对于 CATextLayer,您需要自己处理所有事情:触摸内部、accessibilityLabel、accessibilityElements 列表中的位置...

There's one more drawback that you need to be aware of (or future people coming to see this thread). When using UILabel, all the accessibility comes for free through UIAccessibility. For CATextLayer, you will need to handle everything yourself: touch inside, the accessibilityLabel, the position in the list of accessibilityElements...

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