簡體   English   中英

從用戶輸入的UITextField創建沒有硬編碼范圍的NSAttributedString

[英]Creating NSAttributedString without a hardcoded range from user inputted UITextField

用戶輸入時,我需要讓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”,而不用一個覆蓋另一個。 而且,我不知道可能將屬性字符串范圍更改為什么,因為對於鍵入“性能”一詞的任何索引,范圍都會不斷變化。

如果您有任何疑問,請告訴我! 但是請幫忙!

使用NSRegularExpression (aka Regex),您可以:

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替換為attributedString原始文本。

有關Regex的更多信息,我喜歡本教程 ,它們看起來很清晰。 這是Google搜索NSRegularExpression的第一個條目。 他們的“小抄”可能是很方便,如果你需要更復雜的東西(我沒有檢查是否需要字符串搜索等之后有一個空格),並改變格局Performance ,喜歡的東西[NSString stringWithFormat:@"StartingRegexWithSpecialCharactersPerformanceEndingRegexWithSpecialCharacters]matchesInString:options:range:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM