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.
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.
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...
精彩评论