iOS 使用webView实现图文混排

1,第一种 添加加载webview的视图

-(void) loadMyWebView{
  NSString *title=@"韩寒《后会无期》奇葩的吸金3秘籍";
  
  NSString *linkStr=[NSString stringWithFormat:@"<a href='%@'>我的博客</a> <a href='%@'>原文</a>",@"http://blog.csdn.net/wildcatlele",@"http://jincuodao.baijia.baidu.com/article/26059"];
  
  NSString *p1=@"韩寒《后会无期》的吸金能力很让我惊讶!8月12日影片票房已成功冲破6亿大关。而且排片量仍保持10 以上,以日收千万的速度稳步向七亿进军。";
  
  NSString *p2=@"要知道,《后会无期》不是主流类型片,是一个文艺片。不像《小时代》,是一个商业主流的偶像电影。";
  NSString *image1=[NSString stringWithFormat:@"<img src='%@'  height='280' width='300' />",@"http://nvren.so/uploads/allimg/c140801/140DR4554L40-YB9.jpg"];
  NSString *image2=[NSString stringWithFormat:@"<img src='%@'  height='280' width='300' />",@"http://f.hiphotos.baidu.com/news/w%3D638/sign=78315beeb1fb43161a1f797918a44642/2934349b033b5bb58cb61bdb35d3d539b600bcb5.jpg"];
  
  NSString *p3=@"太奇葩了!有人说,这是中国电影市场的红利,是粉丝电影的成功。但是,有一部投资3000万的粉丝电影《我就是我》,有明星,制作也不错,基本上是惨败。";
  
  NSString *p4=@"《后会无期》卖的不是好故事,是优越感。特别是针对80、90后的人群,你有没有发现,看《后会无期》比看《小时代3》有明显的优越感。故事虽然一般,但是很多人看完后,会在微博、微信上晒照片。所以说,对一个族群靠的不是广度,而是深度。<br>\
  \
  很凶残,值得大家借鉴。韩寒《后会无期》还有什么秘密武器,欢迎《后会无期》团队或相关方爆料,直接留言即可,有料的可以送黎万强亲笔签名的《参与感》一书。";
  
  //初始化和html字符串
  NSString *htmlURlStr=[NSString stringWithFormat:@"<body style='background-color:#EBEBF3'><h2>%@</h2><p>%@</p> <p>%@ </p>%@ <br><p> %@</p> <p>%@</p>%@<p>%@</p></body>",title,linkStr,p1,image1,p2,p3,image2,p4];
  
  [self.myWebView loadHTMLString:htmlURlStr baseURL:nil];

}

这个方法是可以传入字符串的

精髓在于:- (nullable WKNavigation *)loadHTMLString:(NSString *)string baseURL:(nullable NSURL *)baseURL;

2,第二种

接口返回字符串,显示在UIlabel富文本属性上

    activityMsgTempStr=[self htmlEntityDecode:responseData[@"activityHTMLMsg"]];

/解析html

//将 &lt 等类似的字符转化为HTML中的“<”等

- (NSString *)htmlEntityDecode:(NSString *)string

{

    string = [string stringByReplacingOccurrencesOfString:@"&quot;" withString:@"\""];

    string = [string stringByReplacingOccurrencesOfString:@"&apos;" withString:@"'"];

    string = [string stringByReplacingOccurrencesOfString:@"&lt;" withString:@"<"];

    string = [string stringByReplacingOccurrencesOfString:@"&gt;" withString:@">"];

    string = [string stringByReplacingOccurrencesOfString:@"&amp;" withString:@"&"]; // Do this last so that, e.g. @"&amp;lt;" goes to @"&lt;" not @"<"

    

    return string;

}

//富文本赋值

  _activityMsgLab.attributedText=[self attributedStringWithHTMLString:activityMsgTempStr];

   CGSize size = [_activityMsgLab.attributedText boundingRectWithSize:CGSizeMake(kYFScreenWidth-15, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:nil].size;

 _textOfHeight.constant=size.height+10;

//将HTML字符串转化为NSAttributedString富文本字符串

- (NSAttributedString *)attributedStringWithHTMLString:(NSString *)htmlString

{

    NSDictionary *options = @{ NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType,

                               NSCharacterEncodingDocumentAttribute :@(NSUTF8StringEncoding) };

    

    NSData *data = [htmlString dataUsingEncoding:NSUTF8StringEncoding];

    

    return [[NSAttributedString alloc] initWithData:data options:options documentAttributes:nil error:nil];

}

猜你喜欢

转载自blog.csdn.net/ios_xumin/article/details/118304906