簡體   English   中英

檢查NSString是否為html字符串?

[英]Check if an NSString is an html string?

我正在創建一個應顯示字符串列表的應用程序,該字符串是從服務器返回的,可以是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;

我曾嘗試在論壇上進行搜索,但仍未得到我的問題的任何答案。

這工作正常,您需要輸入:

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標簽:

// 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");
}

由於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
} 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM