使用textContainer无法选择UITextView

时间:2014-01-18 06:21:42

标签: ios uitextview nsattributedstring

我正在使用带有textContainer属性的UITextView并归因于字符串...

  

UITextView * textView = [[UITextView alloc] initWithFrame:textViewFrame   textContainer:textContainer];

但是这种观点变得无法选择。

我尝试了子类UITextView并将YES返回到canBecomeFirstResponder方法,但它仍然无法选择..

如何解决这个问题?


这是我的代码的一部分

NSString * decodedHtmlString = [[contentString stringByDecodingHTMLEntities] stringWithNewLinesAsBRs];

NSString * plainText = [decodedHtmlString stringByConvertingHTMLToPlainText];

NSData * dataString = [decodedHtmlString dataUsingEncoding:NSUnicodeStringEncoding allowLossyConversion:NO];

NSDictionary * attributedOptions = [NSDictionary dictionaryWithObjectsAndKeys:
                                    NSHTMLTextDocumentType, NSDocumentTypeDocumentAttribute
                                    , nil];



NSAttributedString * attributesString = [[NSAttributedString alloc] initWithData:dataString
                                                                         options:attributedOptions
                                                              documentAttributes:nil
                                                                           error:nil];


/////direction
NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.alignment = NSTextAlignmentRight;


NSMutableAttributedString * mutableAttrString = [[NSMutableAttributedString alloc] initWithAttributedString:attributesString];
[mutableAttrString addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Arial" size:29.0] range:NSMakeRange(0, plainText.length)];
[mutableAttrString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, plainText.length)];



NSTextStorage * textStorage = [[NSTextStorage alloc] initWithAttributedString:mutableAttrString];
NSLayoutManager * layoutManager = [[NSLayoutManager alloc] init];
[textStorage addLayoutManager:layoutManager];


NSUInteger lastRenderedGlyph = 0;
CGFloat currentXOffset = 0;
while (lastRenderedGlyph < layoutManager.numberOfGlyphs) {

    CGRect textViewFrame = CGRectMake(currentXOffset, 0, 500, 700);
    CGSize columnSize = textViewFrame.size;

    NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize:columnSize];
    [layoutManager addTextContainer:textContainer];

    UITextView *textView = [[UITextView alloc] initWithFrame:textViewFrame
                                               textContainer:textContainer];
    [textView setSelectable:YES];
    textView.scrollEnabled = NO;

    currentXOffset += CGRectGetWidth(textViewFrame);

    [self.scrollView addSubView:textView];
    lastRenderedGlyph = NSMaxRange([layoutManager glyphRangeForTextContainer:textContainer]);
}

2 个答案:

答案 0 :(得分:3)

使用textContainer似乎无法选择UITextView。因此,作为解决方法,我们可以从textContainer的范围中检索attributedString,然后将其设置为UITextView的attributedText属性。

    NSAttributedString * subAttributedString = [mutableAttrString attributedSubstringFromRange:[layoutManager glyphRangeForTextContainer:textContainer]];
    [textView setAttributedText: subAttributedString];

然后选择正在运作。

答案 1 :(得分:0)

试试这个

NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize:Size];

// create the UITextView for this column        
UITextView *textView = [[UITextView alloc] initWithFrame:textViewFrame
                                           textContainer:textContainer];