加载html文本遇到\n不换行的解决办法


//
//  MYViewController.m
//  YYTextDemo
//
//  Created by 刘小二 on 16/6/13.
//  Copyright © 2016年 ibireme. All rights reserved.
//

#import "MYViewController.h"
#import "YYTextView.h"
@interface MYViewController () 

@property (nonatomic, strong) YYTextView *textView;
@property (nonatomic, strong) UIWebView *webView;
@end

@implementation MYViewController

- (void)loadView {

    UIScrollView *scrol = [[UIScrollView alloc] init];
    scrol.frame = [UIScreen mainScreen].bounds;
    scrol.contentSize = CGSizeMake(375, 10000);
    self.view = scrol;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.textView = [[YYTextView alloc] initWithFrame:self.view.bounds];
    
  NSDictionary *dic =   [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"1.plist" ofType:nil]];
    

    
    self.textView.text = dic[@"url"];
    self.textView.editable = NO;
    self.textView.allowsPasteAttributedString = NO ;
    self.textView.selectable =NO ;
    self.textView.allowsCopyAttributedString = NO;
    self.textView.dataDetectorTypes = UIDataDetectorTypeAll;
    self.textView.delegate = self;
//    [self.view addSubview:self.textView];
    
    
    self.webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:self.webView];
    self.webView.scrollView.scrollEnabled = NO;
//    self.webView.userInteractionEnabled = NO;
    
    NSString * formatString = @"%@";
    
     NSString *str = [dic[@"url4"] stringByReplacingOccurrencesOfString:@"\n" withString:@"
"]; NSString * htmlString = [NSString stringWithFormat:formatString,str]; [self.webView loadHTMLString:htmlString baseURL:nil]; self.webView.delegate = self; } - (void)textView:(YYTextView *)textView didTapHighlight:(YYTextHighlight *)highlight inRange:(NSRange)characterRange rect:(CGRect)rect { // void(^YYTextAction)(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect) highlight.tapAction = ^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect) { NSLog(@"%@",[text attributedSubstringFromRange:range].string); }; } -(void)webViewDidFinishLoad:(UIWebView *)webView { // NSString *jsString = [[NSString alloc] initWithFormat:@"document.body.style.fontSize=%f;document.body.style.color=%@",15.0,@"ffa443"]; // [webView stringByEvaluatingJavaScriptFromString:jsString]; [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitUserSelect='none';"]; // Disable callout [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitTouchCallout='none';"]; NSLog(@"%f",webView.scrollView.contentSize.height); CGRect frame = webView.frame; frame.size.height = webView.scrollView.contentSize.height; webView.frame = frame; } - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { if ([request.URL.absoluteString hasPrefix:@"http"]) { [[UIApplication sharedApplication]openURL:request.URL]; return false; } return true ; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end

你可能感兴趣的:(加载html文本遇到\n不换行的解决办法)