[IOS]修改HTML显示内容和显示字体

[IOS]修改HTML显示内容和显示字体


DEMO地址:http://download.csdn.net/detail/u012881779/8915483

对于内文页往往会有一些奇葩需求,没法简单的在客户端或者是后台解决,于是就需要两边配合一下。后台将HTML模板写得自由度高些,前端就按照不同情况自己来处理。

修改显示内容比较容易,获取到HTML数据后直接替换掉特定关键字后的内容就可以了。

更换显示字体分两种:一种是系统自带字体;另一种是需要自己导入字体(.TTF)。

先介绍导入字体,以kaiti.TTF(经典楷体简)为例:

1,先去网上下载楷体字体(kaiti.TTF,百度上可以下载到各种字体)

2,将kaiti.TTF拖到项目中,在工程的info中添加键“Fonts provided by application” ,然后添加item0:kaiti.TTF(可以在这里添加多个字体)

3,在Copy Boundle Resources添加字体kaiti.TTF

好了,这里就可以使用楷体了,注意要使用字体的Family name,而不是字体的文件名


例中我导入的字体文件名是kaiti,它的family name实际是“经典楷体简”



#import "ViewController.h"
#import "ASIHTTPRequest.h"

@interface ViewController () <ASIHTTPRequestDelegate>
@property (weak, nonatomic) IBOutlet UIWebView *webviewOne;
@property (weak, nonatomic) IBOutlet UIWebView *webviewTwo;
@property (strong, nonatomic) NSString *link;
@property (strong, nonatomic) ASIHTTPRequest   *asiRequest;
@end

@implementation ViewController
@synthesize link = _link;
@synthesize asiRequest = _asiRequest;

- (void)dealloc{
    _asiRequest = nil;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    _link = @"http://www.163.com/";
}

/// 原始效果
- (IBAction)methodsOneAction:(id)sender {
    NSURL *url =[NSURL URLWithString:_link];
    NSURLRequest *request =[NSURLRequest requestWithURL:url];
    [_webviewOne loadRequest:request];
    //[_webviewOne setScalesPageToFit:YES];
    
    //隐藏滚动条
    _webviewOne.backgroundColor=[UIColor clearColor];
    _webviewOne.opaque = NO;
    for (UIView *aView in [_webviewOne subviews]){
        [aView setBackgroundColor:[UIColor clearColor]];
        if ([aView isKindOfClass:[UIScrollView class]]){
            UIScrollView *tempSV = (UIScrollView *)aView;
            [tempSV setShowsHorizontalScrollIndicator:NO];
            [tempSV setShowsVerticalScrollIndicator:NO];
            [tempSV setBounces:NO];
            [tempSV setContentSize:CGSizeMake(1,tempSV.contentSize.height )];
            for (UIView *shadowView in tempSV.subviews){
                if ([shadowView isKindOfClass:[UIImageView class]]){
                    //上下滚动出边界时的黑色的图片 也就是拖拽后的上下阴影
                    shadowView.hidden = YES;
                }
            }
        }
    }
}

/// 修改效果
- (IBAction)methodsTwoAction:(id)sender {
    NSURL *url = [NSURL URLWithString:_link];
    _asiRequest = [ASIHTTPRequest requestWithURL:url];
    _asiRequest.delegate = self;
    _asiRequest.cachePolicy = ASIDoNotWriteToCacheCachePolicy;
    [_asiRequest startAsynchronous];
}

-(void)changeHtmlWithFont:(NSString *)html{
    NSMutableString* tempHtml = [NSMutableString stringWithString:html];
    
    //更换字体
    NSString *fontPath = [NSString stringWithFormat:@"%@",[[NSBundle mainBundle] pathForResource:@"kaiti" ofType:@"TTF"]];
    NSString* htmls  = [tempHtml stringByReplacingOccurrencesOfString:@"@palmtrends@" withString:fontPath options:NSRegularExpressionSearch range:NSMakeRange(0, tempHtml.length)];
    htmls  = [htmls stringByReplacingOccurrencesOfString:@"KaiTi" withString:@"经典楷体简" options:NSRegularExpressionSearch range:NSMakeRange(0, htmls.length)];
    
    //标题
    htmls  = [htmls stringByReplacingOccurrencesOfString:@"新闻" withString:@"/*这*/" options:NSRegularExpressionSearch range:NSMakeRange(0, tempHtml.length)];
    htmls  = [htmls stringByReplacingOccurrencesOfString:@"独家" withString:@"/*样*/" options:NSRegularExpressionSearch range:NSMakeRange(0, tempHtml.length)];
    htmls  = [htmls stringByReplacingOccurrencesOfString:@"体育" withString:@"/*就*/" options:NSRegularExpressionSearch range:NSMakeRange(0, tempHtml.length)];
    htmls  = [htmls stringByReplacingOccurrencesOfString:@"娱乐" withString:@"/*替*/" options:NSRegularExpressionSearch range:NSMakeRange(0, tempHtml.length)];
    htmls  = [htmls stringByReplacingOccurrencesOfString:@"女人" withString:@"/*换*/" options:NSRegularExpressionSearch range:NSMakeRange(0, tempHtml.length)];
    htmls  = [htmls stringByReplacingOccurrencesOfString:@"财经" withString:@"/*了*/" options:NSRegularExpressionSearch range:NSMakeRange(0, tempHtml.length)];
    
    //替换标题栏下方的图组
    htmls = [self changePicsAction:htmls ];
    
    //UIWebView加载HTML
    [_webviewTwo loadHTMLString:htmls baseURL:[NSURL URLWithString:nil]];
}

/// 替换图组图片
- (NSString *)changePicsAction:(NSString *)htmls{
    //替换标题栏下方的图组
    NSRange picsStartRange = [htmls rangeOfString:@"data-img=\""];
    NSRange picsEndRange = picsStartRange;
    for (int i = picsStartRange.location + picsStartRange.length; i<htmls.length; i++) {
        picsEndRange = NSMakeRange(i, 2);
        NSString *subTempStr = [htmls substringWithRange:picsEndRange];
        if([subTempStr isEqualToString:@"\">"]){
            break;
        }
    }
    NSRange huanwei = NSMakeRange(picsStartRange.location+picsStartRange.length, picsEndRange.location-(picsStartRange.location+picsStartRange.length));
    NSString *picsStr = [htmls substringWithRange:huanwei];
    
    htmls  = [htmls stringByReplacingOccurrencesOfString:picsStr withString:@"http://img3.cache.netease.com/photo/0001/2015-03-07/AK2L9TV819BR0001.jpg||http://img4.cache.netease.com/photo/0001/2015-03-07/AK2LB88519BR0001.jpg||http://img2.cache.netease.com/photo/0001/2015-03-07/AK2LBJ2F19BR0001.jpg" options:NSRegularExpressionSearch range:huanwei];
    
    return htmls;
}

#pragma mark ASIHTTPRequestDelegate
/// 请求成功
- (void)requestFinished:(ASIHTTPRequest *)request{
    NSString *stringJSON = [request responseString];
    //改变HTML参数
    [self changeHtmlWithFont:stringJSON];

    [request clearDelegatesAndCancel];
    request.delegate = nil;
    [request cancel];
}

/// 请求失败
- (void)requestFailed:(ASIHTTPRequest *)request{
    [request clearDelegatesAndCancel];
    request.delegate = nil;
    [request cancel];
}

@end
 

示意图:







评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值