【问题标题】:UITextView can't be selectable with textContainerUITextView 不能用 textContainer 选择
【发布时间】:2014-02-07 15:58:42
【问题描述】:

我正在使用带有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]);
}

【问题讨论】:

  • 你试过这个吗? textView.selectable = YES;
  • 我没有使用 text 属性,我使用的是 textContainer...
  • 我将 scrollEnabled 设置为 NO,但仍然无法选择

标签: ios uitextview nsattributedstring


【解决方案1】:

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

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

那么选择就起作用了。

【讨论】:

  • 很抱歉,这对我不起作用。它显示一个空白的文本视图。能否请您添加更多代码
  • 我发现用第一个 NSTextContainer 初始化的第一个 UITextView 是可选择的,但是用第二个 NSTextContainer 初始化的第二个 UITextView 不能选择文本。我打印了两个UITextViews的gestureRecognizer计数,发现第一个UITextView有13个gestureRecognizers,但是第二个UITextView只有2个,我想这可能是它不可选的原因。但是为什么第二个文本视图只有 2 个手势识别器,对我来说仍然是个谜。
  • @LiangZhao 这可能在 Xcode 8 和 iOS 10 中,对吧?
  • @LiangZhao 这是真的,我认为它与布局管理器有关。我有 N 个文本容器附加到 1 个布局管理器,但只能选择附加容器 0 的视图 0。 Textview 1 到 N 是不可选择的 - 无论您是否从层次结构中删除以前的视图,或者我们为第一个视图设置 selectable = false。这是一个限制,而且非常令人困惑。
  • textcontainer 属性在 uitextview 上是只读的这一事实使得无法一遍又一遍地重用相同的可选视图。这是设计的错误....
【解决方案2】:

试试这个

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

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

【讨论】:

    猜你喜欢
    • 2014-05-09
    • 2014-03-19
    • 1970-01-01
    • 2011-03-24
    • 2014-10-16
    • 1970-01-01
    • 2010-12-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多