iOS 按字节数截取及(普通length截取)字符串-NSString

时间:2023-01-12 08:25:45

显示的效果是:我们是一个快乐的...(超过一定(例子是8个)数量时,后面的用...代替)

1.第一种情况字符串的截取。

//1.字符串设置长度,长度限制----字符串(目的:超过length时,后面显示...代码如下)
+ (NSString *)stringWithHtml:(NSString *)str length:(CGFloat)length{
    if (!str) return nil;
    if (str  && [str isKindOfClass:[NSString class]]) {
        CGFloat  character = 0;
        const char *chars = [str cStringUsingEncoding:NSUTF8StringEncoding];
        NSUInteger charLength = length;
        if (strlen(chars) <= charLength*3 +1) {//1个汉字或中文字符3个字节,1个英文1个字节(NSUTF8StringEncoding只适用于这个方法)
            return str;
        }
//如果有宽度限制的话,建议用 if(str.length<=length){return str}; 
//缺点是字母或数字或中文与数字字母混合时计算有误,还会执行下面方法,但比较保险
        //获取字数(字节数乘以2即可)
        for(int i=0; i< [str length];i++){
            int a = [str characterAtIndex:i];
            if( a >= 0x4e00 && a <= 0x9fa5){ //判断是否为中文
                character +=1;
            }else{
                if ((isalpha(a))||(isalnum(a))||isascii(a)) {
                    character+=0.5;
                }else{
                    character +=1;
                }
            }
            if (character > length) {//按字数截取
                if (character == length) {
                      str = [str substringToIndex:i+1];
                }else{
                     str = [str substringToIndex:i];
                }
                str = [str stringByAppendingString:@"..."];
                break;
            }
        }
    }
    return str;
}

2.第二种情况简单富文本的截取。

//2.NSAttributedString 按照字符数(中文1个字占1位)量截取
+ (NSAttributedString*)subString:(NSAttributedString*)str len:(NSInteger)len{
    if (!str) return nil;
    if (str.length <=len) return str;
    NSUInteger count = 0;
    NSMutableAttributedString *sb = [[NSMutableAttributedString alloc]init];
    for (int i = 0; i< str.length; i++) {
        NSRange range = NSMakeRange(i, 1) ;
        NSAttributedString *aStr = [str attributedSubstringFromRange:range];
        count += [aStr.string lengthOfBytesUsingEncoding:NSUTF8StringEncoding]>1?2:1;
        [sb appendAttributedString:aStr];
        if(count >= len*2) {//因为上面已经返回1或2了所以这里乘以2
            if (i == str.length -1) {
                return sb;
            }else{
                if (sb) {
                    NSMutableAttributedString *temp1 = [[NSMutableAttributedString alloc]initWithAttributedString:sb];
                    [temp1 appendAttributedString:[[NSAttributedString alloc] initWithString:@"..."]];
                    return temp1;
                }
            }
        }
    }
    return str;
}

3.第三种情况不管是富文本还是字符串规定用length来判断长度并截取。

NSString *subTitle = @"服务器返回的富文本或字符串"
    if (subTitle) {
        NSAttributedString *attribute = [CommonUtil attributedStringWithHtml:subTitle];
        if (attribute && [attribute length] > 8) {
            NSMutableAttributedString *muAttribute = [[NSMutableAttributedString alloc] initWithAttributedString:attribute];
            [muAttribute replaceCharactersInRange:NSMakeRange(8,[attribute length]-8) withString:@"..."];
            _subLabel.attributedText = muAttribute;
        }else{
            _subLabel.attributedText = attribute;
        }
    }

相关文章