zoukankan      html  css  js  c++  java
  • IOS UIWebView引用外部CSS样式(转载)

    首先,将要引用的CSS样式导入到工程文件,然后我们可以自己拼装一个网页并引用这个样式,具体代码实现如下:

    -(void)viewDidLoad

    {

      [super viewDidLoad];

    NSString *title = @"My Awesome Page";

    NSString *cssName =@"screen.css";

    NSString *h1=@"Hello there !";

    NSString *p1 =@"Thanks for visiting my web page.";

    NSString *p2  =@"I hope you like it."

    NSMutableString *htmlString =[[NSMutableString alloc]initWithString:@"<html>"];

    [htmlString appendString:@"<head>"];

    [htmlString appendString:@"<title>"];

    [htmlString appendString:title];

    [htmlString appendString:@"<link rel ="stylesheet" href = " "];

    [htmlString appendString:cssName];

    [htmlString appendString:@"" type="text/css" />"];

    [htmlString appendString:@"</head>"];

    [htmlString appendString:@"<body>"];

    [htmlString appendString:"<h1 class = "loud" >"];

    [htmlString appendString:h1];

    [htmlString appendString:@"</h1>"];

    [htmlString appendString:@"<p id="lighlight">"];

    [htmlString appendString:p1];

    [htmlString appendString:@"</p>"];

    [htmlString appendString:@"<p>"];

    [htmlString appendString:p2];

    [htmlString appendString:@"</p>"];

    [htmlString appendString:@"<ul>"];

    [htmlString appendString:@"<li class ="loud">Pizza</li>"];

    [htmlString appendString:@"<li>Beer</li>"];

    [htmlString appendString:"<li>Dogs</li>"];

    [htmlString appendString:@"</ul>"];

    [htmlString appendString:@"</body>"];

    [htmlStirng appendString:@"</html>"];

    NSString *path =[[NSBundle mainBundle]bundlePath];

    NSURL *baseURL =[NSURL fileURLWithPath:path];

    [webView loadHTMLString:htmlString baseURL:baseURL];

    }

    screen.css中的内容

    body{

      font-size:12px;

      font-weight:bold;

      font-family: Arial;

    }

    a { font-style: italic; }

    h1 a { font-style:italic; }

    .loud {font-style: italic; }

    #highlight { background-color: yellow; }

  • 相关阅读:
    选择
    git使用笔记
    “子查询返回的值不止一个。当子查询跟随在 =、!=、<、<=、>、>= 之后,或子查询用作表达式时,这种情况是不允许的。”SQL查询错误解析
    sql学习笔记
    python爬虫
    线性代数知识点
    centos7 安装git
    感知机——学习笔记
    Logistic Regression学习笔记
    朴素贝叶斯分类--笔记
  • 原文地址:https://www.cnblogs.com/alihaiseyao/p/3458752.html
Copyright © 2011-2022 走看看