【问题标题】:Html string convert into attributed string with \U00002028\n ioshtml字符串转换为属性字符串与\U00002028\n ios
【发布时间】:2016-06-20 18:02:13
【问题描述】:

Html 字符串从服务器获取我尝试转换为属性字符串。 使用

NSAttributedString *attributedString = [[NSAttributedString alloc]
                                        initWithData: [htmlString dataUsingEncoding:NSUnicodeStringEncoding]
                                        options: @{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: [NSNumber numberWithInt:NSUTF8StringEncoding] }
                                        documentAttributes: nil
                                        error: nil
                                        ];

具有 \U00002028\n 的属性字符串导致显示字符串在文本中出现间距和换行。如何才能删除这样的东西。

【问题讨论】:

  • 试试这个 NSString *string = [attributedString.string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];这种方式来实现前导和尾随空格。

标签: ios objective-c iphone


【解决方案1】:

从 NSMutableAttributedString 中修剪前导和尾随空格

NSString * _strAchievementMsg= @"  Hi Test the White space    ";

NSString * htmlString = [NSString stringWithFormat:@"<html><div>%@</div></html>",_strAchievementMsg];

NSMutableAttributedString *attString = [[NSMutableAttributedString alloc]
                                        initWithData: [htmlString dataUsingEncoding:NSUnicodeStringEncoding]
                                        options: @{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: [NSNumber numberWithInt:NSUTF8StringEncoding] }
                                        documentAttributes: nil
                                        error: nil
                                        ];
NSCharacterSet *charSet = [NSCharacterSet whitespaceAndNewlineCharacterSet];
NSRange range           = [attString.string rangeOfCharacterFromSet:charSet];
while (range.length != 0 && range.location == 0)
{
    [attString replaceCharactersInRange:range withString:@""];
    range = [attString.string rangeOfCharacterFromSet:charSet];
}

// Trim trailing whitespace and newlines.
range = [attString.string rangeOfCharacterFromSet:charSet
                                          options:NSBackwardsSearch];
while (range.length != 0 && NSMaxRange(range) == attString.length)
{
    [attString replaceCharactersInRange:range
                             withString:@""];
    range = [attString.string rangeOfCharacterFromSet:charSet
                                              options:NSBackwardsSearch];
}

【讨论】:

  • 你能解释一下你在每个while循环中做了什么吗?只是为了获得更多解释,它可以帮助其他人。
【解决方案2】:

在 iOS 7 中,UIKit 添加了一个 initWithData:options:documentAttributes:error: 方法,可以使用 HTML 初始化一个 NSAtttributedString 请试试这个

[[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUTF8StringEncoding] 
                                 options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
                                           NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)} 
                      documentAttributes:nil error:nil];

【讨论】:

    【解决方案3】:
    NSString *string = [attributedString.string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; 
    

    只去除前导和尾随空格。

    NSArray *components = [string componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    string = [components componentsJoinedByString:@""];
    

    【讨论】:

    • 但我希望属性字符串仍然不是简单字符串
    • 接下来将其转换为属性字符串。
    • @gap 能够将 Nsattributestring 修剪为 Nsstring
    • NSString *string = [attributedString.string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];这种方式来实现前导和尾随空格。
    • @RaviMalviya 你可以看到这个NSHipster nscharacterset
    猜你喜欢
    • 1970-01-01
    • 2016-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-24
    相关资源
    最近更新 更多