在我编程实例化的UITextView(用NSTextContainer初始化).text属性总是零

[更新解决scheme和工作代码在底部]

在-viewDidLoad我分配,initWithFrame:

将myTextView添加到子视图

设置一些基本属性(alignment,背景颜色,文本颜色等)

设置默认文本

.text不会出现。 出现myTextView(如背景颜色所示),设置断点,它有一个框架,内存等。一切看起来都正确。 myTextView看起来不错,但是.text是零。 我改变它,设置它,更新它。 不pipe什么文字仍然是零。

我一遍又一遍地阅读文档。 没有提到我没有做的事情。 我不知所措 帮帮我。

在@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]; } 

[更新:解决scheme]

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

NSTextContainer是iOS 7.0的新function,它定义了文本布局的区域。 根据苹果的文档 ,它说:“新的文本容器必须添加到NSLayoutManager对象才可以使用。” 我想这就是为什么.text总是nil

这是NSTextContainer …

我评论出来,只用了一个框架和文字现在出现,它会出现.text不再是零。 这引发了问题…如何使用UITextViews使用NSTextContainers?

  // NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize: size]; // UITextView *aTextView = [[UITextView alloc] initWithFrame: frame textContainer: textContainer]; UITextView *aTextView = [[UITextView alloc] initWithFrame: frame]; self.contentTextView = aTextView;