在我以编程方式实例化的UITextView(使用NSTextContainer初始化)中.text属性始终为nil

时间:2013-12-05 22:59:09

标签: ios uitextview nslayoutmanager nstextstorage nstextcontainer

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

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

}

[更新:解决方案]

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

3 个答案:

答案 0 :(得分:3)

这是NSTextContainer ......

我评论了它并且只使用了一个框架,文字现在出现了,它会出现.text不再是零。这引出了一个问题......人们如何将NSTextContainers与UITextViews一起使用?

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

答案 1 :(得分:3)

NSTextContainer是iOS 7.0的新功能,它定义了文本布局的区域。根据Apple的Documentations,它说“必须先将新文本容器添加到NSLayoutManager对象中才能使用它。”我认为这就是为什么.text总是nil

答案 2 :(得分:0)

这个问题有点老了,但它帮助我修复了一个类似的问题,试图在scrollView中格式化文本并引导我Figure 1. Creating and configuring the non-view text objects。为了它的价值...

-(id)makeHelpPage1:(CGRect)rect
{
    page1 = [[UIView alloc] initWithFrame:rect];

//    NSString *help = [scrollHelp objectAtIndex:1];
    NSString *help = @"SOME AWESOME TEXT";

    HelpTextView *showHelp = [[HelpTextView alloc] formatHelpTextNew:(NSString*)help];
    [page1 addSubview:showHelp];
    return page1;
}


#import "HelpTextView.h"

@implementation HelpTextView

-(id)formatText:(NSString*)messageID
{

// TextKit - non-view

    NSTextStorage *textStorage;
    NSLayoutManager *layoutManager;
    NSTextContainer *textContainer;

    textStorage = [[NSTextStorage alloc] initWithString:(NSString*)messageID];

    layoutManager = [[NSLayoutManager alloc] init];
    textContainer = [[NSTextContainer alloc] init];
    [layoutManager addTextContainer:textContainer];
    [textStorage addLayoutManager:layoutManager];

// TextKit - as viewed

    UITextView *textView = [[UITextView alloc] initWithFrame: textFrame textContainer: textContainer];
    return textView;
}

免责声明:文字未格式化