在我以编程方式实例化的UITextView(使用NSTextContainer初始化)中.text属性始终为nil [英] In my programmatically instantiated UITextView (initialized with NSTextContainer) the .text property is always nil

查看:620
本文介绍了在我以编程方式实例化的UITextView(使用NSTextContainer初始化)中.text属性始终为nil的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[更新了w /解决方案和工作代码在底部]

在-viewDidLoad我分配,initWithFrame:

In -viewDidLoad I alloc, initWithFrame:

将myTextView添加到subView

Add myTextView to subView

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

Set some basic properties (alignment, background color, text color, etc)

设置默认文本

.text不会出现。 myTextView出现(由背景颜色表示),设置断点,它有一个框架,内存等。一切看起来都正确。 myTextView看起来不错,但.text是零。我改变它,设置它,更新它。无论.text仍为零。

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.

我一遍又一遍地阅读文档。没有提到我没有做的任何事情。我很茫然。帮助。

I have read the documentation over and over again. No mention of anything that I am not doing. I'm at a loss. Help.

在@interface MyController()...

in @interface MyController ()...

过去一切都是(弱)但是我把它变成(强)以防万一。没有骰子。

Everything used to be (weak) but I turned it up to (strong) just in case. No dice.

@property (strong, nonatomic) UIScrollView *scrollView;

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

在viewDidLoad ...

in 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坚持。

When alloc/initializing a UITextView with a NSTextContainer you also need to separately initialize NSLayoutManager and NSTextStorage for the .text to stick.

这是更新的工作代码

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的新功能,它定义了文本布局的区域。根据Apple的 Documentations ,它说必须先将新文本容器添加到NSLayoutManager对象中才能使用它。我认为这就是为什么 .text 总是 nil

NSTextContainer is the new feature of iOS 7.0, it defines the region where the text is laid out. And according to Apple's Documentations, it says "The new text container must be added to an NSLayoutManager object before it can be used.". I think thats why the .text is always nil.

这篇关于在我以编程方式实例化的UITextView(使用NSTextContainer初始化)中.text属性始终为nil的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆