如何从字符串中删除html标签?

时间:2016-02-07 20:06:59

标签: html ios swift2

我有一个包含html代码的字符串  let htmlString = <p style=\"text-align: right;\"> text and text

我想忽略html代码,并且只有一个字符串,只有文字。

谢谢。

3 个答案:

答案 0 :(得分:4)

您可以使用NSAttributedString从字符串中删除html标记。

请找到以下代码:

let htmlString = "<p style=\"text-align: right;\"> text and text"

    do {
        let encodedData = htmlString.dataUsingEncoding(NSUTF8StringEncoding)!
        let attributedOptions : [String: AnyObject] = [
            NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
            NSCharacterEncodingDocumentAttribute: NSUTF8StringEncoding
        ]
        let attributedString = try NSAttributedString(data: encodedData, options: attributedOptions, documentAttributes: nil)

        print("final strings :",attributedString.string)

    } catch {
        fatalError("Unhandled error: \(error)")
    }

希望它适合你!!!

  

您还可以为可重用性创建字符串扩展名:

extension String {
init(htmlString: String) {
    do {
        let encodedData = htmlString.dataUsingEncoding(NSUTF8StringEncoding)!
        let attributedOptions : [String: AnyObject] = [
            NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
            NSCharacterEncodingDocumentAttribute: NSUTF8StringEncoding
        ]
        let attributedString = try NSAttributedString(data: encodedData, options: attributedOptions, documentAttributes: nil)
        self.init(attributedString.string)
    } catch {
        fatalError("Unhandled error: \(error)")
    }
  }
}

Swift 3.0 - (Xcode 8.2)更新

extension String {

    var normalizedHtmlString : String {

        do {
            if let encodedData = self.data(using: .utf8){
                let attributedOptions : [String: AnyObject] = [
                    NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType as AnyObject,
                    NSCharacterEncodingDocumentAttribute: NSNumber(value: String.Encoding.utf8.rawValue)
                ]
                let attributedString = try NSAttributedString(data: encodedData, options: attributedOptions, documentAttributes: nil)
                if let stringNormalized = String.init(attributedString.string){
                    return stringNormalized
                }
            }
        }
        catch {
            assert(false, "Please check string")
            //fatalError("Unhandled error: \(error)")
        }
        return self
    }
}

并调用htmlString方法:

let yourHtmlString = "<p style=\"text-align: right;\"> text and text"
let decodedString = String(htmlString:yourHtmlString)

答案 1 :(得分:-1)

您可以尝试使用 - [NSAttributedString initWithData:options:documentAttributes:error:];

+ (NSAttributedString*)attributedStringForHTMLStrippingWithHTMLString:(NSString*)htmlString error:(NSError**)error
{
    NSAttributedString          *result                 = nil;
    NSMutableAttributedString   *attributedString       = nil;
    NSData                      *htmlStringData         =   [htmlString dataUsingEncoding:NSUTF8StringEncoding];
    NSDictionary                *options                =   @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
                                                              NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)};


    attributedString = [[NSMutableAttributedString alloc] initWithData:htmlStringData
                                                               options:options
                                                    documentAttributes:nil
                                                                 error:error];



    result = [attributedString copy];

    return result;
}

+ (NSString*)stripStringOfHTMLTags:(NSString*)htmlString
{
    NSString            *result             =   nil;
    NSError             *error              =   nil;
    NSAttributedString  *attributedString   =   [self attributedStringForHTMLStrippingWithHTMLString:htmlString error:&error];

    result = [attributedString string];

    return result;
}

答案 2 :(得分:-3)

尝试使用如下的css文件:

HTML文件:

<p id="myText" />

CSS文件:

#myText
{
text-align: right;
}