I have a NSString
stored in cell.lblTitle.text
. I am converting this NSString
to NSMutableAttributedString
with following code.
我有一个存储在cell.lblTitle.text中的NSString。我正在使用以下代码将此NSString转换为NSMutableAttributedString。
text = [[NSMutableAttributedString alloc] initWithData:[cell.lblTitle.text dataUsingEncoding:NSUTF8StringEncoding]
options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)}
documentAttributes:nil error:nil];
[text setAttributes:@{NSFontAttributeName:labelFont} range:NSMakeRange(0, [text length])];
[text addAttribute:NSForegroundColorAttributeName
value:[UIColor darkGrayColor]
range:NSMakeRange(0, [text length])];
When I print cell.lblTitle.text
and its length in console then the following is my output:
当我在控制台中打印cell.lblTitle.text及其长度时,以下是我的输出:
po cell.lblTitle.text
Address: HM<MM-
UU@FF-
Mobile,
Alabama,
123456-
United States
po [cell.lblTitle.text length]
61
And when I print text which is my NSMutableAttributedString
. The output in console is:
当我打印文本时,我的NSMutableAttributedString。控制台中的输出是:
po text
Address: HM{
NSColor = "UIDeviceWhiteColorSpace 0.333333 1";
NSFont = "<UICTFont: 0x7aebc0f0> font-family: \"Futura-Medium\"; font-weight: normal; font-style: normal; font-size: 15.00pt";
}
po [text length]
11
Following is my crash log:
以下是我的崩溃日志:
*** Terminating app due to uncaught exception 'NSRangeException', reason: 'NSMutableRLEArray objectAtIndex:effectiveRange:: Out of bounds'
***由于未捕获的异常'NSRangeException'而终止应用程序,原因:'NSMutableRLEArray objectAtIndex:effectiveRange :: Out of bounds'
So, the string after special character "<" is not identified and my app crashes. How can I manage this special character "<" so I get the output [text length] = 61
.
因此,特殊字符“<”之后的字符串未被识别,我的应用程序崩溃。如何管理这个特殊字符“<”,所以我得到输出[文本长度] = 61。
Any help would be appreciated.
任何帮助,将不胜感激。
1 个解决方案
#1
0
Your text isn't html so don't try to create it as such. Just use initWithString:
to create the attributed string directly from the text.
你的文字不是HTML,所以不要试图创建它。只需使用initWithString:直接从文本创建属性字符串。
#1
0
Your text isn't html so don't try to create it as such. Just use initWithString:
to create the attributed string directly from the text.
你的文字不是HTML,所以不要试图创建它。只需使用initWithString:直接从文本创建属性字符串。