WKWebView加载h5中有视频链接,无法加载视频首帧,看到一片空白和一个播放按钮。那如何让它加载首帧图片呢?很简单在视频链接前加上controls=\"controls\" autoplay=\"autoplay\"。很不幸,当有这量个属性时,在电脑web浏览器上它直接自动播放了。而在iOS端它会自动加载视频数据,停留在首帧图片。由于列表中可能有很多视频,都一起播放,想一想就够乱的,而iOS端不加它却是空白。自动iOS端自己拿到h5字符加上这个属性了。当然在上传后台时加上controls=\"controls\"对web端也没有影响,iOS端就以它为视频识别吧!开始想的是用字符串替换简单搞定。不过理想很丰满,现实很骨感,替换不了。因为h5字符串中有双引号等控制字符。这也告诉我们字符串替换无法对含有特殊控制符的字符串进行替换。最后以双引号为间隔分割成数组,然后在数组中查找替换才搞定。
因为是含有非标准h5字符串,可能只返回一个字符串,由于没有设置字体WKWebView直接显示默认字体可能很小,完全达不到ui图的效果,只能增加头设置字体来搞定了。
-(void)updateBody { if(isCommonUnitEmptyString(self.Body)) { self.contentHeight = 0; return; } NSDictionary *attributes3 = @{NSFontAttributeName :BGFont(15)}; //字体属性,设置字体的font CGSize maxSize3 = CGSizeMake(FULL_WIDTH - COMMON_EDGE_DISTANCE*2, MAXFLOAT); //设置字符串的宽高 CGSize size3 = [getNotNilString(self.Body) boundingRectWithSize:maxSize3 options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes3 context:nil].size; self.contentHeight = size3.height+0.1; if([self.Body hasPrefix:@"<p>"] && [self.Body hasSuffix:@"</p>"]) { self.Body = [NSString stringWithFormat:@"<head><style>p{font-size:30px}</style></head><body>%@</body>", self.Body]; } else if(([self.Body hasPrefix:@"<img src"] && [self.Body hasSuffix:@"/>"]) || ([self.Body hasPrefix:@"<head><style>"] && [self.Body hasSuffix:@"</body>"]) || ([self.Body hasPrefix:@"<p><video src=\""] && [self.Body hasSuffix:@"\" controls=\"controls\" style=\"max-width:100%%\"></video></p>"])) { } else { self.Body = [NSString stringWithFormat:@"<head><style>p{font-size:30px}</style></head><body><p>%@</p></body>", self.Body]; } self.Body = [self.Body stringByReplacingOccurrencesOfString:@"\" controls=\"controls\" autoplay=\"autoplay\" style=\"max-width:100%%\"></video></p>" withString:@"\" controls=\"controls\" style=\"max-width:100%%\"></video></p>"]; NSArray *arr1 = [self.Body componentsSeparatedByString:@"\""]; NSMutableArray *arr = [NSMutableArray array]; for(NSInteger i = 1; !isCommonUnitEmptyArray(arr1) && (i<arr1.count); i++) { NSString *frontStr = arr1[i-1]; NSString *afterStr = arr1[i]; NSString *afterStr2 = [arr1 objectAtSafeIndex:(i+1)]; [arr addObject:frontStr]; if(!isCommonUnitEmptyString(frontStr) && !isCommonUnitEmptyString(afterStr)) { if(!isCommonUnitEmptyString(afterStr2)) { if([frontStr isEqualToString:@" controls="] && [afterStr isEqualToString:@"controls"] && [afterStr2 isEqualToString:@" style="]) { [arr addObject:afterStr]; i++; [arr addObject:@" autoplay="]; [arr addObject:@"autoplay"]; } } else if(!afterStr2) { [arr addObject:afterStr]; break; } } } NSString *str = @""; if(!isCommonUnitEmptyArray(arr)) { str = [NSString stringWithFormat:@"%@", arr[0]]; } else { return; } for(NSInteger i = 1; !isCommonUnitEmptyArray(arr) && (i<arr.count); i++) { NSString *frontStr = arr[i-1]; NSString *afterStr = arr[i]; if(!isCommonUnitEmptyString(frontStr) && [frontStr hasSuffix:@"="]) { str = [NSString stringWithFormat:@"%@\"%@\"", str,afterStr]; } else { str = [NSString stringWithFormat:@"%@%@", str,afterStr]; } } self.Body = str; }