iOS   发布时间:2022-05-04  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – 如何计算textView中的单词和字符总数?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

参见英文答案 > Objective-C: -[NSString wordCount]                                    7个 我使用以下代码来计算单词总数 -(NSInteger) get@R_875_10586@lWords{ NSLog(@"@R_875_10586@l Word %lu",[[_editor.attributedText String]length]); i
参见英文答案 > Objective-C: -[NSString wordCount]                                    7个
我使用以下代码来计算单词总数

-(NSInteger) get@R_875_10586@lWords{
    NSLog(@"@R_875_10586@l Word %lu",[[_editor.attributedText String]length]);
    if ([[_editor.attributedText String]length]==0) {
        return 0;
    }

    NSString *str  =[_editor texTinRange:[_editor textRangeWithRange:[self visibleRangeOfTextView:_editor]]];

    NSInteger sepWord = [[[str StringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]componentsSeparatedByString:@" "] count];
    sepWord += [[[str StringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]componentsSeparatedByString:@"\n"] count];
    sepWord=sepWord-2;
    return sepWord;
}

这是整个角色的代码

-(NSInteger) get@R_875_10586@lChars{

        NSString *str  =[_editor texTinRange:[_editor textRangeWithRange:[self visibleRangeOfTextView:_editor]]];

        NSLog(@"%@",str);

        NSInteger charCount= [[str StringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]length];

        return charCount=charCount-1;

    }

但是,当我输入两行以上时,我得不到完美的计数.它需要换行的新词……

请帮忙..!!!

解决方法

你可以这样做:

NSString *text = @"Lorem ...";

NSArray *words = [text componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSInteger wordCount = [words count];

NSInteger characterCount = 0;
for (NSString *word in words) {
   characterCount += [word length];
}

大佬总结

以上是大佬教程为你收集整理的ios – 如何计算textView中的单词和字符总数?全部内容,希望文章能够帮你解决ios – 如何计算textView中的单词和字符总数?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。