首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在UITextView中使用NSTextContainer和NSTextStorage显示属性化文本

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

Stack Overflow用户
提问于 2014-02-09 21:55:50
回答 1查看 831关注 0票数 2

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

不过,我只需将UITextView的UITextView属性设置为属性化文本字符串就可以做到这一点,但我想知道我在这里做错了什么,或者我误解了哪些概念。希望有人能解释。

代码语言:javascript
复制
- (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。方法(在查看了用户的代码后,可以接受下面的答案)。

代码语言:javascript
复制
//self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
[self.window makeKeyAndVisible]; 
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-02-09 22:03:34

您的代码对我来说很好(Xcode 5.0.2)。这里提供了示例项目:

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

根据“任择议定书”,他漏掉了以下几行:

代码语言:javascript
复制
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.window makeKeyAndVisible];

见评论。

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

https://stackoverflow.com/questions/21665578

复制
相关文章

相似问题

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