Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
392 views
in Technique[技术] by (71.8m points)

ios - Slow load time for custom UIView with UITextView property in Swift

I originally asked this question. I had assumed that the reason for the slow load time of my custom view was because of layering multiple views on top of each other or perhaps because of some recursion problem. However, after cutting out more and more code to see what would make a difference, it came down to whether I had a UITextView present or not. Because the apparent source of my problem is so different than what I was expecting in my first question, I decided to start a new question rather than adding a lengthy update to the old one.

I set up my test project with two view controllers. A button on the first view controller calls a show segue to the second view controller. The second view controller has my custom view on it. (Using the second view controller let me get a sense of how long it took to load the custom view.)

Custom view code:

import UIKit

@IBDesignable class UIMongolTextView: UIView {

    var textView = UITextView() // key line

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    override init(frame: CGRect){
        super.init(frame: frame)
    }
}

As you can see, the only difference from a UIView is that I added a UITextView property. And it is this custom view that loads very slowly. Running the Allocations tool in Instruments I get the following results (a count of 997):

enter image description here

However, if I comment out the line

    //var textView = UITextView()

then I the custom view loads very quickly and only has a count of 7.

enter image description here

What is going on here? Is it possible to use a UITextView property in a custom view and avoid this slow load time?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The bottleneck is the selectable property of your UITextView. There is an inexplicable performance issue in the creation of a UITextView with selectable set to its default value of true.

The easiest way to work around the problem is to add your text view using the storyboard, making sure that you untick the selectable property. There appears to be no documented way to create an unselectable text view in code (as setting selectable to false after creation does not avoid the performance issue during creation). If you need a selectable text view, first create an unselectable text view, and then set selectable to true in viewDidAppear.

If you can't use the storyboard, you might want to consider using a third party class such as TTTAttributedLabel.

It looks like Apple uses a private API to avoid this problem. Other enterprising developers have discovered that, in ChatKit, text views appear to be created using a private method called initReadonlyAndUnselectableWithFrame:textContainer:.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...