我将NSAttributedString用于2件事:
1.使用HTML格式的字符串填充UITextView
2.根据内容长度调整TextView的大小
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithData:[MyHTMLString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
myTextView.attributedText = attributedString;
UITextView *calculationView = [[UITextView alloc] init];
[calculationView setAttributedText:attributedString];
CGSize size = [calculationView sizeThatFits:CGSizeMake(screenWidth,FLT_MAX)];
CGRect frame_summary = CGRectMake(0, y, size.width, size.height);
myTextView.frame = frame_summary;
它在IOS 7.1上运行良好,但在IOS 6.1的第一行崩溃(EXC_BAD_ACCES)
据我所知NSAttributedString在IOS 6上工作。
有什么线索吗?
最后,还有在IOS 5上使用的任何建议或其他选择吗?
提前致谢
- (id)initWithData:(NSData *)data options:(NSDictionary *)options documentAttributes:(NSDictionary **)dict error:(NSError **)error
仅可用于iOS 7.0。以及您在选项中使用的标志。(NSDocumentTypeDocumentAttribute和NSHTMLTextDocumentType)。
您应该改用以下代码:
- (id)initWithString:(NSString *)str attributes:(NSDictionary *)attrs;
您可以str
像这样从NSData创建:
- (NSString *)initWithData:(NSData *)data encoding:(NSStringEncoding)encoding;
祝好运。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句