0

我在 webView 中显示 html 数据(大量内容)。现在我想删除所有标签并将内容修剪为仅 250 个字符并显示在我的 Web 视图中。

提前致谢....

4

1 回答 1

2

在类中包含此功能。

在.h

- (NSString *)stringByStrippingHTML:(NSString *)inputString;

- (NSString *)stringByStrippingHTML:(NSString *)inputString 
{
  NSMutableString *outString;

  if (inputString)
  {
    outString = [[NSMutableString alloc] initWithString:inputString];

    if ([inputString length] > 0)
    {
      NSRange r;

      while ((r = [outString rangeOfString:@"<[^>]+>" options:NSRegularExpressionSearch]).location != NSNotFound)
      {
        [outString deleteCharactersInRange:r];
      }      
    }
  }

  return outString; 
}

通话

  NSString *plainString = [self stringByStrippingHTML:inputHTMLString ];


    NSString *rangedString = [plainString substringToIndex:249];  //0 to 249 makes it 250 characters
于 2012-10-15T11:55:12.267 回答