如何从头开始制作UITextView? [英] How to make UITextView from scratch?

查看:80
本文介绍了如何从头开始制作UITextView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我了解, Apple未提供UIKit的源代码。为了回答另一个问题但是,我试图了解 UITextView 如何工作(或可以使用)。

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.

我如何自己设置最小的 UITextView

How would I set up a minimal UITextView myself?

我从 UIScrollView 的nofollow noreferrer>文档所以我假设我会从那里开始。

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

import UIKit
class MyUITextView: UIScrollView {

    // ???
}

再次查看文本视图文档,看起来我需要在最低限度实现 init 方法和 text property。 (我现在可以忽略所有编辑方面和格式化属性。)

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.)

init 方法采用以下形式:

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

所以我还需要一个 NSTextContainer 。这个 TextKit 组件与 NSTextStorage NSLayoutManager 所以我也需要在某个地方工作。我可以用 text 属性设置 NSTextStorage ,但我真的不知道如何 NSLayoutManager 会在这里互动。

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.

有没有人(Apple以外的人)之前做过这个?这是一个简单的问题,可以在这里回答,还是答案很长?

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

更新:

这个问题显示了我最近的尝试:如何使用Swift中的字符串初始化NSTextStorage

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

推荐答案

这绝对是复杂的。我之前不得不重新实现UILabel的一个子集,这很棘手。我认为你应该考虑的第一件事就是你对工作感兴趣的程度。最基本的是, UITextView 负责操作位图图形上下文并转动字符串到屏幕上的像素。这本身就是一个相当大的挑战,如果你想从头开始重新实现这个功能,你将会忙碌一段时间。

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.

在更高的层次, UITextView 可以将文本分成行,显示不同的字体;仍然更高,你有像 UITextInput Protocol ,它处理允许用户输入和操作文本视图的内容。

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.

在iOS7和TextKit之前,文本呈现实际上是由WebKit处理的,这意味着实现可能会因为经历了这种转变而变得更加混乱。

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.

无论如何,有些事情要指向你(或至少 a )方向:

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.

这篇关于如何从头开始制作UITextView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆