gpt4 book ai didi

ios - 突出显示 UITextView 中的文本

转载 作者:行者123 更新时间:2023-11-29 13:02:20 24 4
gpt4 key购买 nike

我有一个名为 Highlight 的菜单项,它调用以下函数。该函数更改所选文本的颜色并且工作正常。但是,我不确定我做的是否正确。我担心如果 _myTextView(一个 UITextView)有大量文本,可能会出现性能问题。

你能看看它并提出更好的方法吗?

- (void)highlight {

NSRange selectedRange = _myTextView.selectedRange;

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]
initWithAttributedString:_myTextView.attributedText];

[attributedString addAttribute:NSForegroundColorAttributeName
value:[UIColor redColor]
range:selectedRange];

_myTextView.attributedText = attributedString;

}

最佳答案

这应该不会导致性能问题,因为您没有做任何太繁重的事情。即使有大量文本,UIKit 也能很好地处理。这就像添加大量文本并在设备上测试以查看其处理方式一样简单。

我建议您开始将您的对象称为 self.myObject 而不是 _myObject。这是因为如果您需要覆盖 getter,则必须返回并修复您的代码以改为使用 getter。

关于ios - 突出显示 UITextView 中的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19466209/

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