在开发中,我们加载HTML最常用的是UIWebView,其实UITextView也可以加载HTML代码。
- (void)viewDidLoad {
[super viewDidLoad];
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 40, kScreenWidth, kScreenHeight - 50)];
[self.view addSubview:textView];
//HTML代码
NSString *htmlStr = @"
hiuehuirhfiefguihuiefuwifhewufcehsifhoifhiofhwohfwiohfwoicehicehiheifjkhuhuihwefhuiwhfibfiwehfurwhfuiwhfuiwefhuirwhfiw
hviurhrihvuerhviohvoiwehvewiohiowhviowvhoiheiohihuihguguygigdusichvregtgtgegtgvrtvtrbtrbtrbtrbtrbtbtvrgeggergrbrt
hiuehuirhfiefguihuiefuwifhewufcehsicehicehiheifjkhuhuihwefhuiwhfibfiwehfurwhfuiwhfuiwefhuirwhfiw
";
//对图片大小进行处理,适应屏幕宽度
NSString *newString = [htmlStr stringByReplacingOccurrencesOfString:@"
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithData:[newString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
textView.attributedText = attributedString;
}
注意:在info.plist中配置:
App Transport Security Settings
Allow Arbitrary Loads
注:
// 当前屏幕宽度
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
// 当前屏幕高度
#define kScreenHeight [UIScreen mainScreen].bounds.size.height
转载时请注明出处及相应链接,本文永久地址:https://blog.yayuanzi.com/17376.html
微信打赏
支付宝打赏
感谢您对作者Alina的打赏,我们会更加努力! 如果您想成为作者,请点我