Objective c 目标c将html转换为AttributeText
我正在尝试将Objective c 目标c将html转换为AttributeText,objective-c,Objective C,我正在尝试将html转换为nsattributed字符串,以放入UITextView中。代码不起作用,我收到错误信息: 编辑:结果是数据库将其值存储为字符串,这给了我上一个字符串错误。新错误:当我将属性字符串添加到uitextview时,它在代码的最后一行崩溃 [__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object from objects[0] 这个错误似乎告诉我索引0处
html
转换为nsattributed字符串
,以放入UITextView
中。代码不起作用,我收到错误信息:
编辑:结果是数据库将其值存储为字符串,这给了我上一个字符串错误。新错误:当我将属性字符串添加到uitextview
时,它在代码的最后一行崩溃
[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object from objects[0]
这个错误似乎告诉我索引0处的字典为零
NSString *htmlString = @"<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><meta http-equiv="Content-Style-Type" content="text/css"><title></title><meta name="Generator" content="Cocoa HTML Writer"><style type="text/css">p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px ".SF UI Text"; color: #ff0000; background-color: #ffffff}span.s1 {font-family: ".SFUIText-Regular"; font-weight: normal; font-style: normal; font-size: 14.00pt; text-decoration: line-through}</style></head><body><p class="p1"><span class="s1">Your Message Here!</span></p</body></html>"";
NSDictionary *options = @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType};
NSAttributedString *attrString = [[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUTF8StringEncoding] options:options documentAttributes:nil error:nil];
tx.attributedText = attrString;
当我在inspector或xcode中使用textedit功能查看它时,它显示为属性文本
以防万一,我将添加捕获字符串的代码
- (NSString *)requestMessageReply{
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setHTTPMethod:@"GET"];
[request setURL:[NSURL URLWithString:@"myUrlGoesHere"]];
NSError *error = [[NSError alloc] init];
NSHTTPURLResponse *responseCode = nil;
NSData *oResponseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&responseCode error:&error];
if([responseCode statusCode] != 200){
NSLog(@"Error getting %@, HTTP status code %li", @"/messages/3dba5c7db8696a08160f33537b000cda", (long)[responseCode statusCode]);
}
NSString * responseString = [[NSString alloc] initWithData:oResponseData encoding:NSUTF8StringEncoding];
NSData *jsonData = [responseString dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData:jsonData options: NSJSONReadingMutableContainers error:nil];
NSString * message = (NSString *)JSON[@"message"];
return message;
}
将@放在字符串的开头->*htmlString=@“…我只是粘贴了值,这不是错误。很抱歉。谢谢你能听到。错误在哪一行?您是否在
PUBLIC
之后转义引号?它实际上是一个完整的字符串,我刚刚粘贴了该文本,以便大家可以看到值。它发生在最后一行。tx.attributeText=attrString;xcode中检查器中的值实际上显示红色属性文本。将@放在字符串的开头->*htmlString=@“…我只是粘贴了值,这不是错误。很抱歉。谢谢你能听到。错误在哪一行?您是否在PUBLIC
之后转义引号?它实际上是一个完整的字符串,我刚刚粘贴了该文本,以便大家可以看到值。它发生在最后一行。tx.attributeText=attrString;xcode中检查器中的值实际上显示红色属性文本。
- (NSString *)requestMessageReply{
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setHTTPMethod:@"GET"];
[request setURL:[NSURL URLWithString:@"myUrlGoesHere"]];
NSError *error = [[NSError alloc] init];
NSHTTPURLResponse *responseCode = nil;
NSData *oResponseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&responseCode error:&error];
if([responseCode statusCode] != 200){
NSLog(@"Error getting %@, HTTP status code %li", @"/messages/3dba5c7db8696a08160f33537b000cda", (long)[responseCode statusCode]);
}
NSString * responseString = [[NSString alloc] initWithData:oResponseData encoding:NSUTF8StringEncoding];
NSData *jsonData = [responseString dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData:jsonData options: NSJSONReadingMutableContainers error:nil];
NSString * message = (NSString *)JSON[@"message"];
return message;
}