UIWebView加载CSS样式的html

UIWebView加载CSS样式的html

607542-20160719214230732-1377584391.gif

 

效果

607542-20160719214335466-312213109.png

 

源码

//
//  ViewController.m
//  CSS
//
//  Created by YouXianMing on 16/7/19.
//  Copyright © 2016年 YouXianMing. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    CGFloat width             = self.view.bounds.size.width;
    CGFloat height            = self.view.bounds.size.height;
    
    NSString *cssContentString = [NSString stringWithFormat:
                                  @"<html> \n"
                                  
                                  "<head> \n"
                                  "<style type=\"text/css\"> \n"
                                  "h1       {font-size: 20; font-family: \"Heiti SC\"; color: 4C4741;}\n"
                                  "body     {font-size: 15; font-family: \"Heiti SC\"; color: 4C4741;}\n"
                                  "p.style1 {text-indent: 2em;} \n"
                                  "p.style2 {text-indent: 2em; color: 000000;} \n"
                                  "</style> \n"
                                  "</head> \n"
                                  
                                  "<body>"
                                  "<h1 align=\"center\">穷玩车,富玩表,SB玩电脑😭</h1>"
                                  "<p class=\"style1\">别沮丧了。虽然你不曾拥有一场说走就走的旅行,但至少还有一个说胖就胖的体型呀。</p>"
                                  "<p class=\"style2\"><i>虽然你的年龄过不了六一儿童节,但你的身高可以啊!</i></p>"
                                  "%@"
                                  "</body> \n"
                                  
                                  "</html>", [self htmlForJPGImage:[UIImage imageNamed:@"IMG_2845.jpg"]]];
    
    UIWebView *webView      = [[UIWebView alloc] initWithFrame:CGRectMake(0, 40, width, height - 40)];
    webView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:webView];
    [webView loadHTMLString:cssContentString baseURL:nil];
}

- (NSString *)htmlForJPGImage:(UIImage *)image {
    
    CGFloat   width       = self.view.bounds.size.width;
    NSData   *imageData   = UIImageJPEGRepresentation(image,1.0);
    NSString *imageSource = [NSString stringWithFormat:@"data:image/jpg;base64,%@",[imageData base64EncodedStringWithOptions:0]];
    return [NSString stringWithFormat:@"<img src=\"%@\" width = %f />", imageSource, width];
}

@end

 

细节

1. 自己给一张图片测试

607542-20160719214530982-2084616708.png

2. CSS样式

607542-20160719214604951-180119531.png

3. CSS学习地址

http://www.w3school.com.cn/css/index.asp

 

素材

607542-20160719214708701-408994234.jpg

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值