4

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

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

3 回答 3

4

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

于 2013-12-06T00:23:55.957 回答
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;
于 2013-12-05T23:50:21.883 回答
0

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

-(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;
}

免责声明:文本未格式化

于 2018-02-07T23:19:48.593 回答