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

。_饼干妹妹 提交于 2019-12-12 18:24:49

问题


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)

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

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)

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

回答1:


Your code works just fine for me (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

According to the OP he was missing following lines:

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

See comments.



来源:https://stackoverflow.com/questions/21665578/how-to-display-attributed-text-in-a-uitextview-with-nstextcontainer-and-nstextst

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!