1

我一直在尝试在我的文本视图中格式化一个字符串,但我无法解决。我对 xcode 很陌生。

我在这个文件中遗漏了什么吗?我一直在查看堆栈,这就是您的操作方式..但它不起作用。

在此处输入图像描述

- (NSString *)stripTags:(NSString *)str
{
    NSMutableString *html = [NSMutableString stringWithCapacity:[str length]];

    NSScanner *scanner = [NSScanner scannerWithString:str];
    scanner.charactersToBeSkipped = NULL;
    NSString *tempText = nil;

    while (![scanner isAtEnd])
    {
        [scanner scanUpToString:@"<" intoString:&tempText];

        if (tempText != nil)
            [html appendString:tempText];

        [scanner scanUpToString:@">" intoString:NULL];

        if (![scanner isAtEnd])
            [scanner setScanLocation:[scanner scanLocation] + 1];

        tempText = nil;
    }

    return html;
}


- (void)viewDidLoad
{
    [super viewDidLoad];

    NSString *str = newsArticle;

    descTextView.text = [NSString stringWithString:str];


    // Do any additional setup after loading the view from its nib.
}
4

3 回答 3

3

此代码是此处发布的类似问题的答案的修改版本https://stackoverflow.com/a/4886998/283412。这将获取您的 HTML 字符串并去除格式。

-(void)myMethod
{
   NSString* htmlStr = @"<some>html</string>";
   NSString* strWithoutFormatting = [self stringByStrippingHTML:htmlStr];
}

-(NSString *)stringByStrippingHTML:(NSString*)str
{
  NSRange r;
  while ((r = [str rangeOfString:@"<[^>]+>" options:NSRegularExpressionSearch]).location != NSNotFound)
  {
     str = [str stringByReplacingCharactersInRange:r withString:@""];
  }
  return str;
}
于 2013-02-07T19:51:43.180 回答
1

您正在尝试将 HTML 放入标签中。你想使用 UIWebView。

于 2013-02-07T16:50:41.930 回答
0

@试试这个

-(NSString *) stringByStrippingHTML:(NSString *)HTMLString {

    NSRange r;
    while ((r = [HTMLString rangeOfString:@"<[^>]+>" options:NSRegularExpressionSearch]).location != NSNotFound)
        HTMLString = [HTMLString stringByReplacingCharactersInRange:r withString:@""];
    return HTMLString;
}
于 2015-01-21T06:33:26.597 回答