gpt4 book ai didi

ios - 如何在 Swift3 中更改 UITextView 中文本字符串的颜色

转载 作者:行者123 更新时间:2023-12-01 17:48:22 24 4
gpt4 key购买 nike

我在这里查看了一些现有的问题帖子,并尝试实现所有代码,但最终无法成功实现任何代码。实际上,我想我理解了如何更改 UITextView 中某些文本的颜色。如果最初设置了文本,但我不明白的是,当我的 UITextView开始进入编辑,它似乎根本无法正常工作。在下文中,我尝试的代码完全满足了我想要的行为。

 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 中取出所有元素并将其用作个人值(value)。但是,在控制台中,解释的行数太多了。我认为这是一种正确的写作方式,但我的 mac 说 nooooooo。但是,如果我设置 String里面 main_stringsearchWords ,例如,main_string 中的“这是我的问题。我希望人们从世界上帮助我”和 searchWords 中的“world”,如代码中所写。然后在加载应用程序后,在我的 TextView 中,“世界”这个词完美地以红色突出显示,但在世界词之后,所有文本都是红色的。我不明白为什么我会被这个未知的错误折磨。

所以我想要完成的是
  • 从我的 myData 中保存的日期开始,我只希望在用户输入时及时突出显示存储的单词。例如,如果用户输入“hello”和“world”。然后他们被 myData 保存。并且它们只会在键入时突出显示。并在键入未存储的单词时切换回正常颜色。

  • 我想我缺乏一些解释。如果你需要知道什么,请指出我。非常感谢!!

    最佳答案

    在此,我以字符串“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
    }
    }
    }
    }

    objective-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;
    }
    }
    }

    关于ios - 如何在 Swift3 中更改 UITextView 中文本字符串的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40149122/

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