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

  • XCode Cant' Edit CoreData Model
  • Incorrect Keyboard Extension height after rotation
  • Swift Stable Sort?
  • Drawing Smooth Curves - Methods Needed
  • Swift - Dynamic cast class unconditional?
  • iOS 5.0 crash when reading data from an AVAssetReaderOutput
  • 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?

    4 Solutions Collect From Internet About “Slow load time for custom UIView with UITextView property in Swift”

    Tried something similar where a label and a text field were added as a subviews to a uiview subclass. The way I did was the following:

    @interface CustomTextField : UIView
    
    @property (weak, nonatomic) IBOutlet UITextField *valueField;
    
    @end
    

    So, we had a xib file on which we actually add the label and the text field.
    On the xib file, the file owner is “CustomTextField” and outlets are linked with the header file from there.

    The constructor method looks like this:

    - (id)initWithValue:(NSString *)value
    {
        self = [super initWithFrame:CGRectZero];
        if (self) {
            NSArray *nibs = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:self options:nil];
            UIView *view = nibs[0];
            self.valueField.frame = view.bounds;
            [self setFrame:view.bounds];
            [self addSubview:view];
            [self.valueField setText:value];
        }
    
        return self;
    }
    

    Works fine for me.

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

    downloaded your code from github, why don’t you just write a subclass of UITextView instead of the UIView?

    //
    //  UIMongolTextView-A.swift
    //  Mongol App Componants
    //
    //  Created by Allen Zhang on 12/13/15.
    //  Copyright © 2015 MongolSuragch. All rights reserved.
    //
    
    import UIKit
    
    class UIMongolTextView_A: UITextView {
    
        override func awakeFromNib() {
            super.awakeFromNib()
            self.setup()
        }
    
        func setup() {
            // 1-10: ᠨᠢᠭᠡ ᠬᠤᠶᠠᠷ ᠭᠤᠷᠪᠠ ᠳᠦᠷᠪᠡ ᠲᠠᠪᠤ ᠵᠢᠷᠭᠤᠭ᠎ᠠ ᠳᠤᠯᠤᠭ᠎ᠠ ᠨᠠᠢ᠌ᠮᠠ ᠶᠢᠰᠦ ᠠᠷᠪᠠ
    
            self.transform = translateRotateFlip()
    
    
        }
    
        /*
        // Only override drawRect: if you perform custom drawing.
        // An empty implementation adversely affects performance during animation.
        override func drawRect(rect: CGRect) {
            // Drawing code
        }
        */
        func translateRotateFlip() -> CGAffineTransform {
    
            var transform = CGAffineTransformIdentity
    
            // translate to new center
            transform = CGAffineTransformTranslate(transform, (self.bounds.width / 2)-(self.bounds.height / 2), (self.bounds.height / 2)-(self.bounds.width / 2))
            // rotate counterclockwise around center
            transform = CGAffineTransformRotate(transform, CGFloat(-M_PI_2))
            // flip vertically
            transform = CGAffineTransformScale(transform, -1, 1)
    
            return transform
        }
    }
    

    I had a very similar performance issue with a UITextView but I think the root cause and solution were different for me.

    I had a UITextView with static text on a storyboard. It was not editable or selectable. On a slow iPod Touch the ViewController would take 5 seconds to load.

    Turns out that I had an emoji in the static text. When I removed the emoji the problem went away! I used instruments to dig into what was happening and about 25 levels deep in the trace it was getting into a bunch of …font… methods. I was using the system font with no attributes so the first idea that came to mind was the emoji character in my text.