简体   繁体   English

如何使用NSTextContainer和NSTextStorage在UITextView中显示属性文本

[英]How to display attributed text in a UITextView with NSTextContainer and NSTextStorage

I am trying to display some attributed text in a UITextView by using NSTextContainer and NSTextStorage but I don't see anything drawn in the textview (textview itself is drawn with white background as desired but no attributed text in it) 我试图通过使用NSTextContainer和NSTextStorage在UITextView中显示一些属性文本,但是我没有看到在textview中绘制的任何内容(textview本身根据需要使用白色背景绘制,但其中没有属性文本)

I however, can do it by just setting the .attributedText property of UITextView to my attributed text string but I want to know what I am doing wrong here or what concepts I misunderstood. 但是,我可以通过将UITextView的.attributedText属性设置为我的属性文本字符串来做到这一点,但是我想知道我在这里做错了什么或我误解了哪些概念。 Hopefully someone can explain. 希望有人可以解释。

- (void) test
{
    UIView *mainView = [[UIView alloc] initWithFrame:self.window.frame];
    mainView.translatesAutoresizingMaskIntoConstraints = NO;

    UITextView* textView = [[UITextView alloc] init];
    textView.translatesAutoresizingMaskIntoConstraints = NO;
    textView.backgroundColor = [UIColor whiteColor];

    [mainView addSubview:textView];
    [mainView removeConstraints:mainView.constraints];

    // layout constraints
    NSArray *constraintHorizontal = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[textView]-|" options:0 metrics:nil views:@{@"textView":mainView.subviews[0]}];
    NSArray *constraintVertical = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[textView(>=100)]" options:0 metrics:nil views:@{@"textView":mainView.subviews[0]}];

    [mainView addConstraints:constraintHorizontal];
    [mainView addConstraints:constraintVertical];

    NSString *data = @"This is some text where few words are colored and funky.  Some more garbage text. ";
    NSDictionary *attr = @{NSForegroundColorAttributeName:[UIColor redColor]};
    NSMutableAttributedString *textToShow = [[NSMutableAttributedString alloc] initWithString:data attributes:attr];

    NSRange r = {.location = 8, .length = 9};
    [textToShow addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:r];

    NSRange r2 = {.location = 23, .length = 4};
    [textToShow addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:r2];

    NSTextStorage* textStorage = [[NSTextStorage alloc]  initWithAttributedString:textToShow];

    CGSize cgs = textView.bounds.size;
    NSTextContainer *textcont = [[NSTextContainer alloc] initWithSize:cgs];

    // if i comment out these three lines and uncomment the
    //   following line (textView.attributedText .. ), then it works,
    NSLayoutManager *layoutManager = textView.layoutManager;
    [layoutManager addTextContainer:textcont];
    [textStorage addLayoutManager:layoutManager];

    //textView.attributedText = textToShow;

    textView.scrollEnabled = NO;

    UIViewController *vc = [[UIViewController alloc] init];
    vc.view = mainView;
    [vc.view layoutIfNeeded];
    self.window.rootViewController = vc;
}

Update There was no problem with the code above. 更新上面的代码没有问题。 I forgot to add the following lines to application:didFinishLaunchingWithOptions.. method (after looking at the code of the user with accepted answer below) 我忘了在application:didFinishLaunchingWithOptions ..方法中添加以下行(在查看了下面具有可接受答案的用户代码之后)

//self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
[self.window makeKeyAndVisible]; 

Your code works just fine for me (Xcode 5.0.2). 您的代码对我来说很好(Xcode 5.0.2)。 Sample project is available here: 示例项目在这里可用:

https://dl.dropboxusercontent.com/u/1365846/Test.zip https://dl.dropboxusercontent.com/u/1365846/Screen%20Shot%202014-02-09%20at%2023.11.22.png https://dl.dropboxusercontent.com/u/1365846/Test.zip https://dl.dropboxusercontent.com/u/1365846/Screen%20Shot%202014-02-09%20at%2023.11.22.png

According to the OP he was missing following lines: 根据《任择议定书》的规定,他没有参加以下活动:

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.window makeKeyAndVisible];

See comments. 看评论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM