z在項(xiàng)目中,要求用戶評(píng)論超過(guò)10行或者超過(guò)400字節(jié)顯示“...read more”類似微信的展開(kāi)全文,這個(gè)功能已經(jīng)做好怒允,突然QA告訴我說(shuō)柑潦,有條數(shù)據(jù)沒(méi)顯示享言,但是進(jìn)入用戶評(píng)論的詳情頁(yè),數(shù)據(jù)是有的渗鬼。查了一下接口览露,service返回的數(shù)據(jù)沒(méi)有問(wèn)題。
經(jīng)過(guò)仔細(xì)觀察發(fā)現(xiàn)地四百個(gè)字符正好是表情符號(hào)譬胎,查了一些資料發(fā)現(xiàn)iOS中表情符號(hào)占位是兩位差牛,這樣就正好截取了表情符號(hào)的一半,所以不會(huì)顯示堰乔,用了兩個(gè)方法來(lái)解決這個(gè)問(wèn)題偏化,一個(gè)是獲取到字符串的真正長(zhǎng)度
- (NSInteger)getStringLengthWithString:(NSString *)string
{
__block NSInteger stringLength = 0;
[string enumerateSubstringsInRange:NSMakeRange(0, [string length])
options:NSStringEnumerationByComposedCharacterSequences
usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop)
{
const unichar hs = [substring characterAtIndex:0];
if (0xd800 <= hs && hs <= 0xdbff)
{
if (substring.length > 1)
{
const unichar ls = [substring characterAtIndex:1];
const int uc = ((hs - 0xd800) * 0x400) + (ls - 0xdc00) + 0x10000;
if (0x1d000 <= uc && uc <= 0x1f77f)
{
stringLength += 1;
}
else
{
stringLength += 1;
}
}
else
{
stringLength += 1;
}
} else if (substring.length > 1)
{
const unichar ls = [substring characterAtIndex:1];
if (ls == 0x20e3)
{
stringLength += 1;
}
else
{
stringLength += 1;
}
} else {
if (0x2100 <= hs && hs <= 0x27ff)
{
stringLength += 1;
}
else if (0x2B05 <= hs && hs <= 0x2b07)
{
stringLength += 1;
}
else if (0x2934 <= hs && hs <= 0x2935)
{
stringLength += 1;
}
else if (0x3297 <= hs && hs <= 0x3299)
{
stringLength += 1;
}
else if (hs == 0xa9 || hs == 0xae || hs == 0x303d || hs == 0x3030 || hs == 0x2b55 || hs == 0x2b1c || hs == 0x2b1b || hs == 0x2b50)
{
stringLength += 1;
}
else
{
stringLength += 1;
}
}
}];
return stringLength;
}
還有一個(gè)方法是判斷字符串中是否有表情符號(hào)
+ (BOOL)stringContainsEmoji:(NSString *)string
{
__block BOOL returnValue = NO;
[string enumerateSubstringsInRange:NSMakeRange(0, [string length])
options:NSStringEnumerationByComposedCharacterSequences
usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
const unichar hs = [substring characterAtIndex:0];
if (0xd800 <= hs && hs <= 0xdbff) {
if (substring.length > 1) {
const unichar ls = [substring characterAtIndex:1];
const int uc = ((hs - 0xd800) * 0x400) + (ls - 0xdc00) + 0x10000;
if (0x1d000 <= uc && uc <= 0x1f77f) {
returnValue = YES;
}
}
} else if (substring.length > 1) {
const unichar ls = [substring characterAtIndex:1];
if (ls == 0x20e3) {
returnValue = YES;
}
} else {
if (0x2100 <= hs && hs <= 0x27ff) {
returnValue = YES;
} else if (0x2B05 <= hs && hs <= 0x2b07) {
returnValue = YES;
} else if (0x2934 <= hs && hs <= 0x2935) {
returnValue = YES;
} else if (0x3297 <= hs && hs <= 0x3299) {
returnValue = YES;
} else if (hs == 0xa9 || hs == 0xae || hs == 0x303d || hs == 0x3030 || hs == 0x2b55 || hs == 0x2b1c || hs == 0x2b1b || hs == 0x2b50) {
returnValue = YES;
}
}
}];
return returnValue;
}
進(jìn)行判斷的核心代碼如下
if(!readMore && [self getStringLengthWithString:summarize]>300){
if ([HomeReviewTableViewCell stringContainsEmoji:[summarize substringWithRange:NSMakeRange(299,2)]]){
summarize=[NSString stringWithFormat:@"%@%@",[summarize substringToIndex:299] ,LINK_TEXT];
}else if ([HomeReviewTableViewCell stringContainsEmoji:[summarize substringWithRange:NSMakeRange(300,2)]]) {
summarize=[NSString stringWithFormat:@"%@%@",[summarize substringToIndex:300] ,LINK_TEXT];
}else{
summarize=[NSString stringWithFormat:@"%@%@",[summarize substringToIndex:300],LINK_TEXT];
}