6

I am using UITextView with textContainer property and attributed string...

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

But this textview became unSelectable.

I tried to subClass UITextView and return YES to canBecomeFirstResponder method but it still unSelectable..

how to fix this problem ?


This is Part of my code

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]);
}
Yasser Farrag
  • 307
  • 2
  • 12

2 Answers2

3

It seems that UITextView can't be selectable with using textContainer. So, as workaround we can retrieve attributedString from range of textContainer and then set it to the attributedText property of our UITextView.

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

then the selection is working.

halfer
  • 19,824
  • 17
  • 99
  • 186
Yasser Farrag
  • 307
  • 2
  • 12
  • Sorry to say that , it didn't work for me. It shows a blank textview. Can you please add some more code – Karan Alangat Mar 29 '14 at 15:07
  • 1
    I found that the first UITextView that's inited with the first NSTextContainer is selectable, but the second UITextView that's inited with the second NSTextContainer is not able to select text. I printed the gestureRecognizer count of the two UITextViews, found out that the first UITextView has 13 gestureRecognizers, but the second UITextView has only 2, I think this might be the reason why it's not selectable. But why the second text view only got 2 gesture recognizers, is still a mystery to me. – Liang Zhao Sep 26 '16 at 01:18
  • @LiangZhao this may be in Xcode 8 with iOS 10, right ? – Yasser Farrag Sep 27 '16 at 09:09
  • @LiangZhao this is true, I think its connected with layout manager. I have N textcontainers attached to 1 layoutmanager but only view 0 attached with container 0 can be selectable. Textview 1 to N are non-selectable - no matter if you remove previous views from hierarchy OR we set selectable = false for the first view. It's a limitation, and quite baffling at that. – Nirav Bhatt Aug 06 '19 at 17:37
  • The fact that textcontainer property is readonly on uitextview makes it impossible to reuse that same selectable view over and over. It's bug by design.... – Nirav Bhatt Aug 06 '19 at 17:39
0

Try this

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

// create the UITextView for this column        
UITextView *textView = [[UITextView alloc] initWithFrame:textViewFrame
                                           textContainer:textContainer];
Retro
  • 3,985
  • 2
  • 17
  • 41