Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make UITextView from scratch?

As I understand, Apple does not provide the source code for UIKit. In order to answer another question, though, I am trying to understand how UITextView works (or could be made to work) under the hood.

How would I set up a minimal UITextView myself?

I see from the documentation that it inherits from UIScrollView so I assume that I would start there.

import UIKit
class MyUITextView: UIScrollView {

    // ???
}

Again looking at the text view docs, it looks like I would need to at a minimum implement the init method and the text property. (I can ignore all the editing aspects and formatting attributes for now.)

The init method takes the following form:

init(frame frame: CGRect, textContainer textContainer: NSTextContainer?)

So I would also need a property for an NSTextContainer. This TextKit component works together with NSTextStorage and NSLayoutManager so I need to work those in somewhere, too. I could set the NSTextStorage with the text property but I really don't know how NSLayoutManager would interact here.

Has anyone (outside of Apple) done this before? Is this a simple enough question to answer here or would the answer be to long?

Update:

This question shows my latest attempt: How to Initialize NSTextStorage with a String in Swift

like image 852
Suragch Avatar asked Mar 06 '26 14:03

Suragch


1 Answers

This is definitely complicated. I've had to reimplement a subset of UILabel before, and that was tricky. I think the first thing you should think about is what level you're interested in working in. At it's most basic, UITextView is responsible for manipulating a bitmap graphics context and turning a string into pixels on your screen. That in itself is a pretty big challenge, and if you want to reimplement that functionality from scratch you're going to be busy for a while.

At a higher level, UITextView does things like breaking text up into lines, displaying different fonts; higher still and you have things like the UITextInput Protocol, which handles letting the user enter and manipulate the text view's contents.

In terms of implementation details, those obviously aren't available. The closest we can get is a header dump, which is interesting but might not tell us much.

Until iOS7 and TextKit, text rendering was actually handled by WebKit, which means the implementation is potentially more of a mess for having undergone that transition.

Anyway, some things to point you in the right (or at least a) direction:

  • NSAttributedString Class Reference, especially the UIKit Additions, which let you actually blit text into a graphics context;
  • Introducing TextKit, some sample code from WWDC13 (there's an accompanying video as well)
  • Intro to Text Kit from AppCoda.

I apologize that this answer probably isn't as useful as I'd intended. Basically: this is a really big question; it's probably multiple really big questions.

like image 168
cmyr Avatar answered Mar 08 '26 04:03

cmyr