用 NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType 加载返回的HTML数据,需要放在异步处理,否则会导致线程拥堵

具体如下处理

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                    NSAttributedString *attrStr=  [[NSAttributedString alloc] initWithData:[shipInfo dataUsingEncoding:NSUTF8StringEncoding] options:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType,NSCharacterEncodingDocumentAttribute:@(NSUTF8StringEncoding)} documentAttributes:nil error:nil];
                    NSMutableAttributedString * mutableAttrStr = [[NSMutableAttributedString alloc] initWithAttributedString:attrStr];
                    [mutableAttrStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:11] range:NSMakeRange(0, attrStr.string.length)];
                       //处理两个文本不居中对齐的方法:(11--11):大字体 --- 小字体
                    [attStr addAttribute:NSBaselineOffsetAttributeName value:@(0.36 * (11 - 11)) range:NSMakeRange(0, shipDiscount.length)];
                    dispatch_async(dispatch_get_main_queue(), ^{
                        //主线程赋值 并更新布局
                        freeView.contenLabel.attributedText = mutableAttrStr;
                        freeView.contenLabel.textAlignment = NSTextAlignmentCenter;
                    });
                });

你可能感兴趣的:(用 NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType 加载返回的HTML数据,需要放在异步处理,否则会导致线程拥堵)