gpt4 book ai didi

ios - 无法使用 textContainer 选择 UITextView

转载 作者:可可西里 更新时间:2023-11-01 04:44:05 29 4
gpt4 key购买 nike

我正在使用带有 textContainer 属性和属性字符串的 UITextView...

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

但是这个 textview 变得不可选择了。

我尝试子类化 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]);
}

最佳答案

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

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

然后选择工作。

关于ios - 无法使用 textContainer 选择 UITextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21200846/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com