UILabel/NSTextView 与 CATextLayer
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这取决于您打算如何使用文本以及您正在编写的应用程序类型。如果文本主要是静态的并用作 UI 中的标签或对话框,那么
NSTextField
应该可以正常工作。如果您需要精美的动画,那么CATextLayer
可能更合适。如果您正在编写游戏(例如纸牌游戏),UI 往往会更加相似,并且使用 CATextLayer 和 CoreAnimation 会更有意义。如果您正在编写更通用的应用程序(例如聊天客户端),则用户与应用程序交互的界面和方式有很大不同,并且使用NSTextFields
或NSTextViews 可能更有意义
。同样,这一切都取决于您要创建的应用程序的类型,但如果它是更传统的应用程序(例如聊天客户端示例),那么我认为使用 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, thenCATextLayer
may be more appropriate. If you are writing a game (solitaire, for example), the UIs will tend to be more similar and usingCATextLayer
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 useNSTextFields
orNSTextViews
.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 usingNSTextField
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 toUILabel
.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 makesNSTextField
lighter weight is that they tend to share an invisible, singleNSTextView
(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.您还需要注意一个缺点(或者将来的人会看到这个帖子)。使用 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...