【问题标题】:How to add content to html file in iOS如何在 iOS 中向 html 文件中添加内容
【发布时间】:2014-03-22 17:37:56
【问题描述】:

我想将内容添加到项目资源中可用的 html 文件中。 例如:- 下面是 html,里面有一些标签

<body >
<div id="abc">
    <div>{feed}<br /></div>
    <div >{date}</div>
</div>
</body>

我想用其中的一些字符串替换 {feed} ,{date} 并在 UIwebview 中显示该 html 请给我一些指示来实现它 提前致谢

【问题讨论】:

  • 你可以使用@代替提要和日期,然后将提要和数据传递给 [NSString stringWithForm@"html%@%@",feed,date]
  • 谢谢大家的回答:)

标签: ios iphone ios7 uiwebview


【解决方案1】:
//Get your HTML file path.
NSString * htmlFilePath = [[NSBundle mainBundle] pathForResource:@"YOUR_FILE_NAME" ofType:@"html"];

//get the file content to a NSString.
NSString *htmlContentString = [NSString stringWithContentsOfFile:htmlFilePath encoding:NSUTF8StringEncoding error:nil];

// Then you need to replace a string (say 'stringA') with new string (say 'stringB')
    htmlContentString = [htmlContentString stringByReplacingOccurrencesOfString:stringA withString:stringB];

//Then load the new HTML string to your webView

[yourWebView loadHTMLString:htmlContentString baseURL:nil];

【讨论】:

    【解决方案2】:
    1. 将 html 字符串加载到 NSMutableString
    2. 搜索并替换所需的字符串
    3. 使用UIWebView方法- (void)loadHTMLString:(NSString *)string baseURL:(NSURL *)baseURL;加载它。

    【讨论】:

      【解决方案3】:
      NSString *str = @"<body >
      <div id="abc">
          <div>{feed}<br /></div>
          <div >{date}</div>
      </div>
      </body>";
      
      str = [str stringByReplacingOccurrencesOfString:@"{feed}"
                                           withString:@"YourString"];
      str = [str stringByReplacingOccurrencesOfString:@"{date}"
                                           withString:@"YourString"];
      

      如果您需要在 html 中添加其他内容,请使用 NSMutableString 而不是 NSString

      【讨论】:

        【解决方案4】:

        使用这个:

        NSString *feedString = @"Your feed string";
        NSString *dateString = @"Your date string";
        NSString* descHtmlString = [NSString stringWithFormat:@"<body ><div id="abc"><div>%@<br /></div><div >%@</div></div></body>", feedString, dateString];
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-12-08
          • 2017-03-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-09-06
          • 1970-01-01
          • 2020-11-28
          相关资源
          最近更新 更多