gpt4 book ai didi

ios - 带有 attributedText 的 UITextField shouldChangeTextInRange 导致滚动奇怪的行为

转载 作者:可可西里 更新时间:2023-11-01 06:16:07 25 4
gpt4 key购买 nike

我需要在渲染之前捕获键入的文本,以便对其应用一些属性。

为此,我使用了 UITextView 的委托(delegate)方法 shouldChangeTextInRange 以及 NSAttributedString。到目前为止一切顺利,而且有效。

问题是,当 UITextView 获得一定数量的文本并开始滚动时,更改 attributedText 使其滚动到顶部,而下一个字符将使它滚动回到末尾。

它在打字时继续上下移动..

设置textview.text,而不是属性文本,n

有什么建议吗?

提前致谢。

- (BOOL) textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{

NSAttributedString * newString = [[NSAttributedString alloc] initWithString:text attributes:nil];
NSMutableAttributedString * string = [[NSMutableAttributedString alloc] initWithAttributedString:_textView.attributedText];

[string insertAttributedString:newString atIndex:range.location];

textView.attributedText = string;

range.location += text.length;

textView.selectedRange = range;

return NO;
}

最佳答案

我不确定你的 shouldChangeTextInRange 的意义,但要修复不需要的自动滚动,请尝试使用 包围 textView.attributedText 的设置[textView setScrollEnabled:NO].

即:

[textView setScrollEnabled:NO];
textView.attributedText = string;
range.location += text.length;
[textView setScrollEnabled:YES];

关于ios - 带有 attributedText 的 UITextField shouldChangeTextInRange 导致滚动奇怪的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14593125/

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