如何使用NSTextContainer和NSTextStorag

如何使用NSTextContainer和NSTextStorag

本文介绍了如何使用NSTextContainer和NSTextStorage在UITextView中显示属性文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过使用NSTextContainer和NSTextStorage在UITextView中显示一些属性文本,但我没有看到textview中绘制的任何内容(textview本身是根据需要使用白色背景绘制的,但其中没有任何属性文本)

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)

但是,我可以通过将UITextView的.attributedText属性设置为我的属性文本字符串来实现,但我想知道我在这里做错了什么或者我误解了什么概念。希望有人可以解释。

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

更新
没有问题使用上面的代码。我忘了在应用程序中添加以下行:didFinishLaunchingWithOptions ..方法(在查看下面接受的答案的用户代码之后)

UpdateThere 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)

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


推荐答案

您的代码对我来说很合适(Xcode 5.0。 2)。示例项目可在此处获取:

Your code works just fine for me (Xcode 5.0.2). Sample project is available here:


根据OP,他缺少以下几行:

According to the OP he was missing following lines:

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

查看评论。

这篇关于如何使用NSTextContainer和NSTextStorage在UITextView中显示属性文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 09:51