加载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 () <YYTextViewDelegate,UIWebViewDelegate>

@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 = @"<span style=\"font-size:13px;color:#ffa443\">%@</span>";
    
     NSString *str = [dic[@"url4"] stringByReplacingOccurrencesOfString:@"\n" withString:@"<br>"];
    
    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


最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容