Simple rich text view in cocoa-touch

2020-07-26 16:25发布

问题:

I'm working on an iOS app in which I need to implement a basic text view with these requirements:

  • Rich text
    • Font emphasis (bold, italics).
    • Inline images for hyperlink-like actions.
  • Scrolling
    • Getting and setting the scroll offset (for remembering the previous scroll position).
  • Text selection
    • Getting the selected text character range.
    • Scrolling the view when selecting text, if needed.

I believe I could achieve this with UIWebView but the problem with this is that it provides very little control and is somewhat slow and shows a blank screen while loading. I was wondering if this could be achieved with Core Text but regarding the text selection I'm not sure. I'm hoping to achieve as iOS-native behaviour as possible.

What I'd need is pretty much like Instapaper's text view.

回答1:

OmniGroup have done something like this by creating a text editor much like UITextView which draws the text using Core Text but also has all the editing features of UITextView as well as Rich text features from Core Text.

You can find it here.



回答2:

There are some answers about this in StackOverflow, for example: Core text tutorial

However the best tutorial I ever read about Core Text in iOS is not listed in any answer I've found: How to create a simple magazine app with core text

Please notice that CoreText is only available for iOS 3.2 or superior.