2014-01-18 2 views
4

Я использую UITextView с textContainer собственности и приписываемой строки ...UITextView не может быть по выбору с textContainer

UITextView * TextView = [[UITextView Alloc] initWithFrame: textViewFrame textContainer: textContainer];

Но этот текст не был выбран.

Я попытался Подкласс UITextView и вернуться к YEScanBecomeFirstResponder методу, но он все еще недоступный ..

как исправить эту проблему?


Это часть моего кода

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]); 
} 
+0

Вы пробовали это ?. textView.selectable = YES; – sathiamoorthy

+0

Да, конечно, я сделал –

+0

Попробуйте это http://stackoverflow.com/a/20334204/2629258 – sathiamoorthy

ответ

3

Кажется, что UITextView не может быть по выбору с использованием textContainer. Таким образом, в качестве обходного пути мы можем восстановить атрибутString из диапазона textContainer, а затем установить его в свойство attributedText нашего UITextView.

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

затем выбор работает.

+0

Извините, что это не сработало для меня. Он показывает пустой текст. Не могли бы вы добавить еще один код –

+0

. Я обнаружил, что первый UITextView, который был включен в первый NSTextContainer, можно выбрать, но второй UITextView, который включен во второй NSTextContainer, не может выбрать текст. Я напечатал счетчик жестов для двух UITextViews, выяснил, что первый UITextView имеет 13 жестовых реестров, но второй UITextView имеет только 2, я думаю, что это может быть причиной того, что он не может быть выбран. Но почему второе текстовое представление получило только два распознавателя жестов, для меня все еще остается загадкой. –

+0

@ LiangZhao это может быть в Xcode 8 с iOS 10, правильно? –

0

Попробуйте

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

// create the UITextView for this column   
UITextView *textView = [[UITextView alloc] initWithFrame:textViewFrame 
              textContainer:textContainer]; 
Смежные вопросы