Slow load time for custom UIView with UITextView property in Swift Slow load time for custom UIView with UITextView property in Swift swift swift

Slow load time for custom UIView with UITextView property in Swift


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


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.


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 UIKitclass 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    }}