HTML字符串使用\ U00002028 \ n iOS转换为属性字符串 [英] Html string convert into attributed string with \U00002028\n ios

查看:175
本文介绍了HTML字符串使用\ U00002028 \ n iOS转换为属性字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HTML字符串从服务器获取,我尝试将其转换为属性字符串.使用

Html string get it from server I tried convert into attributed string. using

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

具有\ U00002028 \ n的属性字符串会导致显示字符串在文本中出现间距和换行符.如何能够删除此类内容.

attribute string having \U00002028\n that causes display string got spacing and line breaking in text.How can be able to remove such thing.

推荐答案

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

Trim leading and trailing whitespaces from 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];
}

这篇关于HTML字符串使用\ U00002028 \ n iOS转换为属性字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆