UIWebView加载CSS样式的html
效果
源码
//// 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: @" \n" " \n" " \n" " \n" "" "穷玩车,富玩表,SB玩电脑?
" "别沮丧了。虽然你不曾拥有一场说走就走的旅行,但至少还有一个说胖就胖的体型呀。
" "虽然你的年龄过不了六一儿童节,但你的身高可以啊!
" "%@" " \n" "", [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:@"", imageSource, width];}@end
细节
1. 自己给一张图片测试
2. CSS样式
3. CSS学习地址
素材