如何在Swift3中的UITextView中更改文本字符串的颜色
我看过这里一些现有的问题,我尝试实现所有代码,但最终无法成功实现任何代码.实际上,我想我理解了如果最初设置了文本,则如何更改UITextView
中某些文本的颜色,但是我不明白的是,当我的UITextView
开始进入编辑状态时,它似乎无法正常工作全部.在下面,正是我尝试的代码完全满足了我想要的行为.
I have looked at some existing questions posts here and I tried to implement all code but ended up not being able to successfully implement any code. Actually, I think I comprehended how to change colors of some texts inside UITextView
if a text is initially set, but what I do not understand is that when my UITextView
began entering editing, it does not seem to be working properly at all. In the following, it is my attempted code that closely full fills my desired behaviour.
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
保存了输入数据.我试图使用一个for循环将所有元素从getData
中取出,并将其用作单个值.但是,在控制台中,有太多的解释行.我认为这是一种正确的书写方式,但是我的Mac电脑说nooooooo.但是,例如,如果我在main_string
和searchWords
内设置了String
,则这是我的问题.我希望人们从世界中帮助我" 在main_string和中代码中写在searchWords中的世界" .然后,在加载应用程序之后,在我的文本视图上,"world"一词完美地以红色突出显示,但是在world词之后,所有文本均为红色.我不明白为什么我会被这个未知的错误折磨.
I saved input data by naming MyData
. I tried to use a for-loop to take all elements out of getData
and use it as individual values. However, in the console, there are so many lines of explanation. I think this is a proper way to write, but my mac says nooooooo. However, If I set a String
inside main_string
and searchWords
, for example, "this is my question. I want people to help me from the world" in main_string and "world" in searchWords as written in the code. Then after the app is being loaded, on my text view the word, "world" is highlighted in red perfectly, but after the world word, all text is in red. I do not understand why I am being tortured by this unknown bug.
所以我要完成的是
-
从
myData
中的保存日期开始,我只希望在用户键入内容时及时突出显示所存储的单词.例如,如果用户键入"hello"和"world".然后它们由myData
保存,并且仅在键入时才突出显示.并键入未存储的单词时切换回正常颜色.
- from the saved date in my
myData
, I only want the stored words to be highlighted timely when users type in. For example, if a user types "hello" and "world". Then they are saved bymyData
and they are going to be highlighted only when typed. And switch back to the normal color when words which are not stored are typed.
我认为我缺乏解释.如果您需要了解一些信息,请为我指出.非常感谢!
I assume I lack some explanation. If you need to know something, please point out for me. Thanks very much!!
在此,我以字符串"world"为例.当您在textview中键入内容时,此方法有效
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
}
}
}
对于多个字符串,您可以执行以下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
对于多个字符串,您可以这样做
-(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;
}
}
}