【问题标题】:Creating NSAttributedString without a hardcoded range from user inputted UITextField从用户输入的 UITextField 创建没有硬编码范围的 NSAttributedString
【发布时间】:2014-06-25 18:12:16
【问题描述】:

我需要UITextField 在用户键入时将某些单词替换为粗体字。在指定textViewDidChange 的方法中,我有:

-(void)textViewDidChange:(UITextView *)textView {
    self.notepadTextView.font = [UIFont fontWithName:@"ProximaNova-Regular" size:20];
    NSString *textEntryContents = [[self notepadTextView ]text];
    [gCore processSpeechText:textEntryContents]; //program specific function enabling speech 
    ...
    textEntryContents = [textEntryContents stringByReplacingOccurrencesOfString:@" performance" withString:@"\n\nPerformance"];

    UIFont *fontBold = [UIFont fontWithName:@"ProximaNova-Bold" size:20];

    NSMutableAttributedString *string = [[NSMutableAttributedString alloc]initWithString:textEntryContents];

    [string addAttribute:NSFontAttributeName value:fontBold range:NSMakeRange(0,textEntryContents.length)];

    self.notepadTextView.attributedText = string;
}

这会格式化用户输入的文本,以便每次键入“性能”一词时,都会开始一个新行。这很好用。我试着让它每次输入“性能”时,它也会加粗这个词。加粗方面有效;然而,它加粗了一切,而不仅仅是性能这个词。

如果用户输入:

“性能测试良好,但需要更多更新。”

应该发生什么:

"性能

测试良好,但需要更多更新。”

会发生什么:

"性能

测试良好,但需要更多更新。"

self.notepadTextView.attributedText = string 似乎覆盖了之前的 UIFont 对每个单词的更改,我不知道如何使 UITextField 接受两个“self.textView.text”而不覆盖另一个。此外,我不知道我可以将属性字符串范围更改为什么,因为对于键入“性能”一词的任何索引,范围都会不断变化。

如果您有任何问题,请告诉我!但请帮忙!

【问题讨论】:

  • 找到子字符串的范围,然后只用粗体显示。
  • 也许正则表达式可以帮助您识别关键字。

标签: ios objective-c uitextfield nsattributedstring


【解决方案1】:

使用NSRegularExpression(又名正则表达式),您可以:

UIFont *normalFont = [UIFont fontWithName:@"HelveticaNeue" size:20];
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"hey performance tested well, but needs more updating"
                                                                                     attributes:@{NSFontAttributeName: normalFont}];

NSError *regexError;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"Performance"
                                                                       options:NSRegularExpressionCaseInsensitive error:&regexError];

if (!regexError)
{
    NSArray *allMatches = [regex matchesInString:[attributedString string]
                                         options:0
                                           range:NSMakeRange(0, [[attributedString string] length])];
    UIFont *boldFont = [UIFont fontWithName:@"HelveticaNeue-Bold" size:20];
    for (NSTextCheckingResult *aMatch in allMatches)
    {
        NSRange matchRange = [aMatch range];
        [attributedString setAttributes:@{NSFontAttributeName:boldFont}
                                  range:matchRange];
    }
}

注意:我使用了Helvetica Neue,以及它的粗体版本,因为我没有你的字体。 这是我曾经尝试过的一个例子,所以你可以用textEntryContents替换attributeString原文。

有关 Regex 的更多信息,我喜欢 this tutorial,这似乎对它们很清楚。这是谷歌搜索NSRegularExpression 的第一个条目。他们的“备忘单”可能非常方便,如果您需要更复杂的东西(我没有检查在搜索字符串后是否需要空格等),并更改模式Performance,类似于@ 987654329@ 在matchesInString:options:range:

【讨论】:

  • 这太棒了,非常感谢您的帮助!你是救生员!!
猜你喜欢
  • 2018-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-16
  • 2015-01-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多