W moim programowo utworzonym UITextView (zainicjowanym za pomocą NSTextContainer) właściwość .text ma zawsze wartość zero

[AKTUALIZOWANE W / ROZWIĄZANIE i KOD ROBOCZY w dolnej części]

W -viewDidLoad I alloc, initWithFrame:

Dodaj myTextView do subView

Ustaw niektóre podstawowe właściwości (wyrównanie, kolor tła, kolor tekstu itp.)

Ustaw domyślny tekst

Tekst nie pojawia się. Pojawia się myTextView (jak wskazuje kolor tła), ustawia punkt przerwania, ma ramkę, pamięć itp. Wszystko wygląda prawidłowo. myTextView wygląda dobrze, ale tekst jest zerowy. Zmieniam go, ustawiam, aktualizuję. Bez względu na to, jaki tekst pozostaje.

Przeczytałem dokumentację w kółko. Brak wzmianki o czymś, czego nie robię. Jestem ze stratą. Wsparcie.

w @interface MyController () ...

Wszystko było kiedyś (słabe), ale na wszelki wypadek zmieniłem je na (mocne). Nie ma kości.

@property (strong, nonatomic) UIScrollView *scrollView;

@property (strong, nonatomic) UIImageView *imageView;
@property (strong, nonatomic) UILabel *titleLabel;
@property (strong, nonatomic) UITextView *contentTextView;

w viewDidLoad ...

- (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];

}

[Aktualizacja: rozwiązanie]

Gdy przydzielasz / inicjujesz UITextView za pomocą NSTextContainer, musisz również osobno zainicjować NSLayoutManager i NSTextStorage, aby tekst mógł się trzymać.

Oto zaktualizowany działający kod

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];

questionAnswers(3)

yourAnswerToTheQuestion