UILabel / NSTextView vs CATextLayer

I have an iOS app that I convert to an iOS / Mac app. In the iOS version, I used UILabels for my text. Obviously, I cannot do this on a Mac. I see two options:

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

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

Obviously, option 2 has the big advantage of having less code to support. My question is: does option 1 have any advantages that I should know about? In particular, does CATextLayer have flaws that UILabel and NSTextView do not have?

EDIT: This is a game. The user never enters text, but the game displays some, for example, grades.

+7
source share
2 answers

It depends on how you are going to use the text and the type of application you are writing. If the text will be mostly static and used as shortcuts or dialogs in the user interface, then NSTextField should work fine. If you need some fantastic animation, then CATextLayer might be more appropriate. If you're writing a game (like solitaire), the user interfaces will tend to be more similar and use CATextLayer , and CoreAnimation will make sense. If you are writing a more general application, such as a chat client, the interfaces and methods of user interaction with the application are significantly different from each other, and it probably makes sense to use NSTextFields or NSTextViews .

Again, it all depends on the type of application that you are creating, but if it is a more traditional application, for example, an example of a chat client, then I would think that using CATextLayer and software to create everything in the interface, instead of using NSTextField and laying out the material in a nib file, would mean hundreds of other lines of code. On the other hand, if the application is more like an example of a game with solitaire, and the sizes of layers and properties are determined in such a way as to take into account which platform the application works 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 need only if you want to make full paragraphs and more complex settings. Part of what makes NSTextField easier is that they tend to share the invisible single NSTextView (or its superclass, NSText ) as a field editor that helps with text input. Since only one text field in an application can have focus at a time, it can share a common field editor to improve performance.

+10
source

There is another drawback that you need to know (or future people come to see this topic). When using UILabel, all availability is provided free of charge through UIAccessibility. For CATextLayer you will need to do everything yourself: tap inside, accessibilityLabel, position in the accessibilityElements list ...

+1
source

All Articles