首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在以编程方式实例化的UITextView (使用NSTextContainer初始化)中,.text属性始终为零

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

Stack Overflow用户
提问于 2013-12-05 22:59:08
回答 3查看 5.8K关注 0票数 4

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

在-viewDidLoad I alloc中,initWithFrame:

将myTextView添加到subView

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

设置默认文本

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

我一遍又一遍地阅读这些文件。没有提到我没有做的任何事。我不知所措。帮助。

在@interface MyController ()中..。

以前一切都很(弱),但为了以防万一,我把它变成了(强者)。没有骰子。

代码语言:javascript
复制
@property (strong, nonatomic) UIScrollView *scrollView;

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

在viewDidLoad..。

代码语言:javascript
复制
- (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保持不变。

以下是更新的工作代码

代码语言:javascript
复制
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];
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-12-06 00:23:55

NSTextContainer是iOS 7.0的新特性,它定义了文本布局的区域。苹果的文献资料称,“新的文本容器必须添加到NSLayoutManager对象中,才能使用它。”我认为这就是为什么.text总是nil的原因。

票数 4
EN

Stack Overflow用户

发布于 2013-12-05 23:50:21

是NSTextContainer..。

我把它注释掉了,只使用了一个框架,文本现在出现了,.text不再是零了。哪个引发的question...How应该与UITextViews一起使用NSTextContainers?

代码语言:javascript
复制
  // NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize: size];
  // UITextView *aTextView = [[UITextView alloc] initWithFrame: frame textContainer: textContainer];
  UITextView *aTextView = [[UITextView alloc] initWithFrame: frame];
  self.contentTextView = aTextView;
票数 3
EN

Stack Overflow用户

发布于 2018-02-07 23:19:48

这个问题有点老了,但它帮助我解决了一个类似的问题,试图在scrollView中格式化文本,并引导我使用图1.创建和配置非视图文本对象。不管值多少钱..。

代码语言:javascript
复制
-(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;
}

免责声明:文本未格式化

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20412563

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档