Note that there are some explanatory texts on larger screens.

plurals
  1. POIn my programmatically instantiated UITextView (initialized with NSTextContainer) the .text property is always nil
    primarykey
    data
    text
    <p><strong>[UPDATED w/ SOLUTION and WORKING CODE in bottom section]</strong></p> <p>In -viewDidLoad I alloc, initWithFrame:</p> <p>Add myTextView to subView</p> <p>Set some basic properties (alignment, background color, text color, etc)</p> <p>Set default text</p> <p>The .text does not appear. myTextView appears (as indicated by background color), set breakpoint, it has a frame, memory, etc. Everything looks right. myTextView looks good, but the .text is nil. I change it, set it, update it. No matter what the .text remains nil.</p> <p>I have read the documentation over and over again. No mention of anything that I am not doing. I'm at a loss. Help.</p> <p>in @interface MyController ()...</p> <p>Everything used to be (weak) but I turned it up to (strong) just in case. No dice.</p> <pre><code>@property (strong, nonatomic) UIScrollView *scrollView; @property (strong, nonatomic) UIImageView *imageView; @property (strong, nonatomic) UILabel *titleLabel; @property (strong, nonatomic) UITextView *contentTextView; </code></pre> <p>in viewDidLoad...</p> <pre><code>- (void)viewDidLoad { [super viewDidLoad]; // scroll view CGSize size = CGSizeMake(703, self.view.frame.size.width); UIScrollView *aScrollView = [[UIScrollView alloc] initWithFrame: self.view.frame]; self.scrollView = aScrollView; [self.scrollView setDelegate: self]; [self.scrollView setDirectionalLockEnabled: YES]; [self.scrollView setContentSize: size]; [self.view addSubview: self.scrollView]; // image view CGRect frame = CGRectMake(0, 0, 703, 400); UIImageView *anImageView = [[UIImageView alloc] initWithFrame: frame]; self.imageView = anImageView; [self.scrollView addSubview: self.imageView]; [self.imageView setBackgroundColor: [UIColor blueColor]]; [self.imageView setContentMode: UIViewContentModeScaleAspectFit]; // text view frame = CGRectMake(0, self.imageView.frame.size.height, 703, self.view.frame.size.width - self.imageView.frame.size.height); size = CGSizeMake(self.view.frame.size.height -320, self.view.frame.size.width - self.imageView.frame.size.height); NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize: size]; UITextView *aTextView = [[UITextView alloc] initWithFrame: frame textContainer: textContainer]; self.contentTextView = aTextView; [self.scrollView addSubview: self.contentTextView]; self.contentTextView.delegate = self; self.contentTextView.editable = NO; self.contentTextView.hidden = NO; [self.body setBackgroundColor: [UIColor orangeColor]]; // text characteristics self.contentTextView.textColor = [UIColor blueColor]; self.contentTextView.textAlignment = NSTextAlignmentNatural; self.contentTextView.font = [UIFont fontWithName: @"Helvetica" size: 30]; self.contentTextView.text = @"SOME AWESOME TEXT"; self.contentTextView.autoresizingMask = UIViewAutoresizingFlexibleHeight; // text view specific self.contentTextView.contentSize = size; // controller [self setEdgesForExtendedLayout:UIRectEdgeNone]; } </code></pre> <p><strong>[Update: Solution]</strong></p> <p>When alloc/initializing a UITextView with a NSTextContainer you also need to separately initialize NSLayoutManager and NSTextStorage for the .text to stick.</p> <p>Here's the updated working code</p> <pre><code>NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize: size]; NSLayoutManager *layoutManager = [NSLayoutManager new]; self.layoutManager = layoutManager; [layoutManager addTextContainer: textContainer]; NSTextStorage *textStorage = [[NSTextStorage alloc] initWithString: kBaconIpsum]; self.textStorage = textStorage; [textStorage addLayoutManager: layoutManager]; UITextView *aTextView = [[UITextView alloc] initWithFrame: frame textContainer: textContainer]; self.contentTextView = aTextView; [self.scrollView addSubview: self.contentTextView]; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload