3
- (int)getMsgLen:(NSString *)msg{
    int unicode = 0;//is arabic or english (1=arabic 0=english)
    NSString *str = @"دجحخهعغفقثصضطكمنتالبيسشظزوةىلارؤءئإلإألأآلآ،؛؟ءآأؤإئابةتثجحخدذرزسشصضطظعغفقكلمنهوىيـًٌٍَُِّْ";

    for (int i = 0; i<[str length]; i++) {
        NSString *strResult = [str substringWithRange:NSMakeRange(i, 1)];

        for (int r = 0; r<[msg length] ;r++) {
            NSString *msgResult = [msg substringWithRange:NSMakeRange(r, 1)];
            if([strResult isEqualToString:msgResult] && strResult)
                unicode = 1;
        }
    }

    int msgLenght = 1;

    // arabic value

    float oneMsgLenth = 70;
    float twoMsgLenth = 67;

    //  english value
    if(unicode == 0){
        oneMsgLenth = 160;
        twoMsgLenth = 134;
    }

    int numChar = [msg length];
    if( numChar > oneMsgLenth){
        msgLenght = ceil(numChar / twoMsgLenth);
    }
    return msgLenght;
}

我有这个代码,这个代码检查是否有任何阿拉伯字符来改变计算消息的数量,这个代码每次你写一个 UITextView 并且它工作成功,但它太慢了,因为每次点击它都会改变值UIL标签。

有什么建议可以帮助我快速完成吗?

4

1 回答 1

3

您至少可以通过尽快退出来提高循环的速度,unicode != 0还可以使用NSCharacterSet仅初始化一次的类进行比较。

在 MyClass.h 中添加一个实例变量:

@interace MyClass : NSObject
{
    NSCharacterSet *_arabicChars;
}

...

@end

我的班级.m:

- (id)init
{
    self = [super init];
    if (self != nil)
    {
        _arabicChars = [NSCharacterSet characterSetWithCharactersInString:@"دجحخهعغفقثصضطكمنتالبيسشظزوةىلارؤءئإلإألأآلآ،؛؟ءآأؤإئابةتثجحخدذرزسشصضطظعغفقكلمنهوىيـًٌٍَُِّْ"];
        ...
    }
    return self;
}

- (int)getMsgLen:(NSString *)msg{

    BOOL isArabic = [msg rangeOfCharacterFromSet:_arabicChars].location != NSNotFound;

    int msgLenght = 1;

    // arabic value

    float oneMsgLenth = 70;
    float twoMsgLenth = 67;

    //  english value
    if (!isArabic){
        oneMsgLenth = 160;
        twoMsgLenth = 134;
    }

    int numChar = [msg length];
    if( numChar > oneMsgLenth){
        msgLenght = ceil(numChar / twoMsgLenth);
    }
    return msgLenght;
}
于 2013-05-22T09:11:53.380 回答