В моем программно созданном экземпляре UITextView (инициализирован с помощью NSTextContainer) свойство .text всегда равно nil
[ОБНОВЛЕНО с РЕШЕНИЕМ и РАБОЧИМ КОДОМ в нижней части]
В -viewDidLoad я размещаю, initWithFrame:
Добавить myTextView в subView
Установите некоторые основные свойства (выравнивание, цвет фона, цвет текста и т. Д.)
Установить текст по умолчанию
.Text не появляется. Появляется myTextView (как указано цветом фона), устанавливается точка останова, у него есть рамка, память и т. д. Все выглядит правильно. myTextView выглядит хорошо, но .text ноль. Я изменяю это, устанавливаю это, обновляю это. Независимо от того, что .text остается ноль.
Я прочитал документацию снова и снова. Никаких упоминаний о том, чего я не делаю. Я'м в недоумении. Помогите.
в @interface MyController () ...
Раньше все было (слабо), но я на всякий случай включил его (сильно). Нет кости.
@property (strong, nonatomic) UIScrollView *scrollView;
@property (strong, nonatomic) UIImageView *imageView;
@property (strong, nonatomic) UILabel *titleLabel;
@property (strong, nonatomic) UITextView *contentTextView;
в 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];
}
[Обновление: решение]
При выделении / инициализации UITextView с помощью NSTextContainer вам также необходимо отдельно инициализировать NSLayoutManager и NSTextStorage для прикрепления .text.
Вот'обновленный рабочий код
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];