Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITextView can't be selectable with textContainer

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]);
}
like image 895
Yasser Farrag Avatar asked Jan 18 '14 06:01

Yasser Farrag


1 Answers

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.

like image 156
Yasser Farrag Avatar answered Nov 15 '22 22:11

Yasser Farrag