4

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

Community
  • 1
  • 1
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
  • Why are you doing this? Does a category not do what you want? – Allison Jun 04 '15 at 03:43
  • TextMate is an open-source app that implements its own text view, if I'm not mistaken. It's on GitHub. – jscs Jun 04 '15 at 03:44
  • @Sirens I'm doing this in order to eventually answer this question: http://stackoverflow.com/questions/30620291/how-to-make-a-vertical-uitextview-by-subclassing-it-from-uiscrollview-in-swift – Suragch Jun 04 '15 at 04:08
  • @JoshCaswell You're right. [TextMate source](https://github.com/textmate/textmate) in Objective C is on GitHub. I couldn't find a custom UITextView, though. – Suragch Jun 04 '15 at 04:18
  • Extra user defined methods can be added using categories. – Mithun Ravindran Jun 15 '15 at 13:08

2 Answers2

1

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:

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.

cmyr
  • 2,235
  • 21
  • 30
  • All I want it to do is render text on a view with the appropriate line breaks and scrolling. It seems to me like TextKit should be able to handle the rendering and line wrapping and subclassing UIScrollView should be able to handle the scrolling. I keep hoping someone will tell me that a basic solution isn't that complicated, but you are probably right. Anyway, +1 for your input. Thanks. – Suragch Jun 15 '15 at 12:43
0

You can use the below sources to understand what happens under the hood. Apple'e implementation need not be the same though.

https://github.com/BigZaphod/Chameleon/blob/master/UIKit/Classes/UITextView.h https://github.com/BigZaphod/Chameleon/blob/master/UIKit/Classes/UITextView.m

RK-
  • 12,099
  • 23
  • 89
  • 155
  • Some thoughts after reading through the code: These didn't use any TextKit components that I could see. I wonder if they are implementing a pre iOS 7 version of UITextView that didn't use TextKit. I got a little lost in the details (like why `interface UIScrollView` is used rather than subclassing UIScrollView) but I will continue to study this. – Suragch Jun 08 '15 at 13:59
  • Yes, It is pretty ios7 – RK- Jun 08 '15 at 14:48
  • Chameleon is an older project, dating back I think to iOS 4/5? So it doesn't use any of the new TextKit stuff. – cmyr Jun 15 '15 at 11:59