如何在Swift3中改变UITextView里面的文字颜色
我在这里看过一些现有的问题post,我试图实现所有代码,但最终无法成功实现任何代码。 实际上,我想我已经理解了如何在UITextView
里面改变一些文本的颜色,但是我不明白的是当我的文本tview开始进入编辑时,它似乎根本不能正常工作。 在下面,这是我的企图代码,密切履行我所期望的行为;
func textViewDidBeginEditing(_ textView: UITextView) { let main_string = "" let getData: [String] = userDefaults.object(forKey: "myData") as! [String] print("\(getData)") let searchWords = "world" let range = (main_string as NSString).range(of: searchWords) let attribute = NSMutableAttributedString.init(string: main_string) attribute.addAttribute(NSForegroundColorAttributeName, value: UIColor.red , range: range) mytextView.attributedText = attribute }
我通过命名是MyData
保存了input数据。 我试图使用for循环从getData
取出所有元素,并将其作为单独的值使用。 但是,在控制台中,有太多的解释。 我认为这是一个正确的写法,但我的Mac说nooooooo。 但是,如果我在main_string
和searchWords
设置了一个String
,例如, “这是我的问题,我希望人们在main_string中帮助我 ” ,在代码中写入searchWords中的“world” 。 然后,应用程序加载后,在我的文本查看单词,“世界”是完美的红色,但在世界的话,所有的文字是红色的。 我不明白为什么我被这个未知的bug所折磨。
所以我想要完成的是
- 从
myData
保存的date开始,我只希望在用户input时及时突出显示存储的单词。例如,如果用户键入“hello”和“world”。 然后他们被myData
保存,只有打字时才会被高亮显示。 当没有存储的单词被input时,切换回正常的颜色。
我想我缺less一些解释。 如果你需要知道一些事情,请为我指出。 非常感谢!!
在这个我以string“世界”为例。 当你在textview中input时,这个方法正在工作
func textViewDidChange(_ textView: UITextView) { let attrStr = NSMutableAttributedString(string:txtview.text) let inputLength = attrStr.string.characters.count let searchString = "world" let searchLength = searchString.characters.count var range = NSRange(location: 0, length: attrStr.length) while (range.location != NSNotFound) { range = (attrStr.string as NSString).range(of: searchString, options: [], range: range) if (range.location != NSNotFound) { attrStr.addAttribute(NSForegroundColorAttributeName, value: UIColor.red, range: NSRange(location: range.location, length: searchLength)) range = NSRange(location: range.location + range.length, length: inputLength - (range.location + range.length)) textView.attributedText = attrStr } } }
但是当你已经设置了textview的初始文本,然后使用这个方法
func textViewDidBeginEditing(_ textView: UITextView) { //Just set your text as you set in textview let attrStr = NSMutableAttributedString(string: "hello world I ma jeckjklwefljlwjflkjwfkljelwfjklfgjwklfjlkwgjwlgkjwklsgjklsjgklsdjgkljdslkgjsdlkgjlksdjgldjsgldjskl world nsfhjklshfklhsllsd fgiw world") let inputLength = attrStr.string.characters.count let searchString = "world" let searchLength = searchString.characters.count var range = NSRange(location: 0, length: attrStr.length) while (range.location != NSNotFound) { range = (attrStr.string as NSString).range(of: searchString, options: [], range: range) if (range.location != NSNotFound) { attrStr.addAttribute(NSForegroundColorAttributeName, value: UIColor.red, range: NSRange(location: range.location, length: searchLength)) range = NSRange(location: range.location + range.length, length: inputLength - (range.location + range.length)) textView.attributedText = attrStr } } }
对于多个string,您可以像SWIFT 3一样进行
func textViewDidChange(_ textView: UITextView) { let attrStr = NSMutableAttributedString(string: txtview.text) let inputLength = attrStr.string.characters.count let searchString : NSArray = NSArray.init(objects: "hello","world") for i in 0...searchString.count-1 { let string : String = searchString.object(at: i) as! String let searchLength = string.characters.count var range = NSRange(location: 0, length: attrStr.length) while (range.location != NSNotFound) { range = (attrStr.string as NSString).range(of: string, options: [], range: range) if (range.location != NSNotFound) { attrStr.addAttribute(NSForegroundColorAttributeName, value: UIColor.red, range: NSRange(location: range.location, length: searchLength)) range = NSRange(location: range.location + range.length, length: inputLength - (range.location + range.length)) textView.attributedText = attrStr } } } }
目标C
对于多个string,你可以这样做
-(void)textViewDidChange:(UITextView *)textView { NSMutableAttributedString *attstr = [[NSMutableAttributedString alloc]initWithString:textView.text]; NSUInteger characterCount = [attstr length]; NSArray *arr = [[NSArray alloc]initWithObjects:@"football",@"player",nil]; for (int i=0; i<arr.count; i++) { NSUInteger searchlength = [[NSString stringWithFormat:@"%@",[arr objectAtIndex:i]] length]; NSRange range1 = NSMakeRange(0, attstr.length); while (range1.location != NSNotFound) { range1 =[attstr.string rangeOfString:[NSString stringWithFormat:@"%@",[arr objectAtIndex:i]] options:0 range:range1]; if (range1.location !=NSNotFound) { [attstr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(range1.location, searchlength)]; [attstr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20.0] range:range1]; range1 = NSMakeRange(range1.location + range1.length, characterCount -(range1.location + range1.length)); textView.attributedText = attstr; } } }