[在底部更新了解决方案和工作代码]
在 -viewDidLoad 我分配,initWithFrame:
将 myTextView 添加到子视图
设置一些基本属性(对齐方式、背景颜色、文本颜色等)
设置默认文本
.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];
}
[更新:解决方案]
当使用 NSTextContainer 分配/初始化 UITextView 时,您还需要分别初始化 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];