【问题标题】:Check if an NSString is an html string?检查 NSString 是否是 html 字符串?
【发布时间】:2018-08-12 16:11:29
【问题描述】:

我正在创建一个应显示字符串列表的应用程序,该字符串是从服务器返回的,可以是 html 也可以不是。 我目前正在 UILabel 中设置文本。为此,我正在使用以下内容

    NSMutableAttributedString *attributedTitleString = [[NSMutableAttributedString alloc] initWithData:[title dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
cell.label.attributedText =attributedTitleString;

当文本是 html 时,一切正常,因为字体和对齐方式在 html 中返回。当文本是普通文本时会出现此问题。字体、文本对齐方式、文本大小等不再受到尊重。

那么如何检查文本是否为 html 字符串? 如果是普通文本,我将使用以下内容:

cell.label.text =title;

我尝试在论坛上搜索,但仍然没有得到任何关于我的问题的答案。

【问题讨论】:

  • 字体 pp. 不再受到尊重是什么意思?如果是纯文本,则没有这样的属性。
  • 我的意思是在创建标签时,我将字体设置为 20 用于 ex 和 settextalignment center。但是,当我设置 cell.label.attributedText =attributedTitleString (from a plain text) 时,字体是如此之小并且左对齐
  • 我认为这是不可能的。您只能检查您的 html 字符串是否包含 html 标签。 (以正则表达式为例)
  • 如果在分配纯文本后(重新)设置属性会发生什么?
  • @AminNegm-Awad 我不想在设置 html 文本的属性文本后总是设置字体,因为这将覆盖从 html 返回的字体

标签: html ios objective-c nsattributedstring nsmutableattributedstring


【解决方案1】:

这很好,你需要输入:

cell.label. attributedText = title; 也适用于普通文本。

因为它工作正常。运行以下代码。

//如果是HTML文本

NSString *htmlstr = `@"This is <font color='red'>simple</font>"`;

NSMutableAttributedString *attributedTitleString = [[NSMutableAttributedString alloc] initWithData:[htmlstr dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];

textField.attributedText =attributedTitleString;

textField.font = [UIFont fontWithName:@"vardana" size:20.0];

//如果是普通文本。

NSString *normalStr = @"This is Renuka";

NSMutableAttributedString *NorAttributedTitleString = [[NSMutableAttributedString alloc] initWithData:[normalStr dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];

textField.attributedText = NorAttributedTitleString;

textField.font = [UIFont fontWithName:@"vardana" size:20.0];

【讨论】:

  • 我不想总是在设置 html 文本的属性文本后设置字体,因为这将覆盖从 html 返回的字体
  • 我没听懂你,你只想改变普通文本的字体?
  • 仅适用于普通文本,因为字体是在 HTML 中自动设置的,以防字符串为 html 格式
【解决方案2】:

您可以检查您的字符串是否包含html标签:

// iOS8 +

NSString *string = @"<TAG>bla bla bla html</TAG>";

if ([string containsString:@"<TAG"]) {
    NSLog(@"html string");
} else {
    NSLog(@"no html string");
}

// iOS7 +

NSString *string = @"<TAG>bla bla bla html</TAG>";

if ([string rangeOfString:@"<TAG"].location != NSNotFound) {
    NSLog(@"html string");
} else {
    NSLog(@"no html string");
}

【讨论】:

    【解决方案3】:

    由于 HTML 和 XML 的语法和结构非常相似,您可以使用XMLDocument 来检查给定的数据对象是 HTML 还是纯文本。

    所以数据来自Data 类型,由URLSession dataTask 返回。

    // Default to plain text
    var options : [NSAttributedString.DocumentReadingOptionKey: Any] = [.documentType: NSAttributedString.DocumentType.plain]
    
    // This will succeed for HTML, but not for plain text                
    if let _ = try? XMLDocument(data: data, options: []) {
         options[.documentType] = NSAttributedString.DocumentType.html
    }
    
    guard let string = try? NSAttributedString(data: data, options: options, documentAttributes: nil) else { return }
    

    更新

    上面的方法有点作用。我遇到了一些 HTML 没有创建 XML 文档的问题。目前我有一个不同的解决方案,我不太喜欢。 由于症状是我们在属性字符串中只得到了一行,因此只需检查它并在只有一行的情况下创建一个新的属性字符串。

    var options : [NSAttributedString.DocumentReadingOptionKey: Any] = [.documentType: NSAttributedString.DocumentType.html]
    
    guard var string = try? NSAttributedString(data: data, options: options, documentAttributes: nil) else { return }
    
    if string.string.split(separator: "\n").count == 1 {
        options[.documentType] = NSAttributedString.DocumentType.plain
        guard let tempString = try? NSAttributedString(data: data, options: options, documentAttributes: nil) else { return }
        string = tempString
    } 
    

    【讨论】:

      猜你喜欢
      • 2015-12-08
      • 1970-01-01
      • 1970-01-01
      • 2013-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-30
      • 2014-02-09
      相关资源
      最近更新 更多