iOS通过html模版实现富文本编辑
2016-06-22 09:41
489 查看
在iOS开发中,常常会遇到一些富文本编辑,如新闻,公告等,NSMutableAttributedString又有一定的局限性,所以我想到用html 模版去加载富文本页面,根据所需要的格式,去构建相应的html界面。一个简单的html模版如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet"href="mystyle.css"type="text/css">
<style
type="text/css">
.sourceAndTime{
width:100%;
font-size:12pt;
line-height:40px;
color:#1b6761;
padding-right:8px;
padding-left:8px;
}
#news_title{
font-weight:bold;
font-size:20pt!important;
padding-right:8px;
padding-left:8px;
color:#57918b
}
#container{
font-size:$contentfontsize;
text-indent:1em;
padding-right:8px
padding-left:8px;
}
</style>
</head>
<body style="margin-left:10px;border: 0;">
<div >
<div id="news_title">$title</div>
<div class='sourceAndTime'>
<span>$time</span>
<span
style="padding-right:10px; float: right" >$source</span>
</div>
</div>
<div style="width: 100%;margin: 0 auto;height: auto;" >
<p style="text-align: center">
<img
src=$imgSrcstyle="width: 50%;height:auto"
/>
</p>
</div>
<div id="container">$content</div>
<br/><br/><br/><br/>
</body>
</html>
在模版中预留相应的唯一标志符,如$imgSrc等,然后在iOS端将这个html模版转换为一个字符串,然后再去替代这个字符串内的那些唯一标志符,具体iOS代码如下:
NSString *path = [[NSBundle mainBundle] pathForResource:@"news_detail" ofType:@"html"];
_webString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
_webString = [_webString stringByReplacingOccurrencesOfString:@"$title"withString:[_titleArray objectAtIndex:indexPath.row]];
_webString = [_webString stringByReplacingOccurrencesOfString:@"$time" withString:[CNotificationVC TimeformatFromSeconds:[[ _timeArray objectAtIndex:indexPath.row] longLongValue]]];
_webString = [_webString stringByReplacingOccurrencesOfString:@"$imgSrc"withString:[_imageArray objectAtIndex:indexPath.row]];
_webString = [_webString stringByReplacingOccurrencesOfString:@"$content"withString:[_contentArray objectAtIndex:indexPath.row]];
_webString = [_webString stringByReplacingOccurrencesOfString:@"$source" withString:[_adminArray objectAtIndex:indexPath.row]];
UIWebView *webView = [[UIWebViewalloc]initWithFrame:CGRectMake(0,64,
self.view.frame.size.width,self.view.frame.size.height)];
webView.delegate =self;
[webView setBackgroundColor:[UIColorclearColor]];
[webView setOpaque:NO];
[webView loadHTMLString:_webStrbaseURL:[NSURLfileURLWithPath:[[NSBundlemainBundle]
pathForResource:@"news_detail"ofType:@"html"]]];
[self.viewaddSubview:webView];
具体步骤就是如此,希望对大家有所帮助
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet"href="mystyle.css"type="text/css">
<style
type="text/css">
.sourceAndTime{
width:100%;
font-size:12pt;
line-height:40px;
color:#1b6761;
padding-right:8px;
padding-left:8px;
}
#news_title{
font-weight:bold;
font-size:20pt!important;
padding-right:8px;
padding-left:8px;
color:#57918b
}
#container{
font-size:$contentfontsize;
text-indent:1em;
padding-right:8px
padding-left:8px;
}
</style>
</head>
<body style="margin-left:10px;border: 0;">
<div >
<div id="news_title">$title</div>
<div class='sourceAndTime'>
<span>$time</span>
<span
style="padding-right:10px; float: right" >$source</span>
</div>
</div>
<div style="width: 100%;margin: 0 auto;height: auto;" >
<p style="text-align: center">
<img
src=$imgSrcstyle="width: 50%;height:auto"
/>
</p>
</div>
<div id="container">$content</div>
<br/><br/><br/><br/>
</body>
</html>
在模版中预留相应的唯一标志符,如$imgSrc等,然后在iOS端将这个html模版转换为一个字符串,然后再去替代这个字符串内的那些唯一标志符,具体iOS代码如下:
NSString *path = [[NSBundle mainBundle] pathForResource:@"news_detail" ofType:@"html"];
_webString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
_webString = [_webString stringByReplacingOccurrencesOfString:@"$title"withString:[_titleArray objectAtIndex:indexPath.row]];
_webString = [_webString stringByReplacingOccurrencesOfString:@"$time" withString:[CNotificationVC TimeformatFromSeconds:[[ _timeArray objectAtIndex:indexPath.row] longLongValue]]];
_webString = [_webString stringByReplacingOccurrencesOfString:@"$imgSrc"withString:[_imageArray objectAtIndex:indexPath.row]];
_webString = [_webString stringByReplacingOccurrencesOfString:@"$content"withString:[_contentArray objectAtIndex:indexPath.row]];
_webString = [_webString stringByReplacingOccurrencesOfString:@"$source" withString:[_adminArray objectAtIndex:indexPath.row]];
UIWebView *webView = [[UIWebViewalloc]initWithFrame:CGRectMake(0,64,
self.view.frame.size.width,self.view.frame.size.height)];
webView.delegate =self;
[webView setBackgroundColor:[UIColorclearColor]];
[webView setOpaque:NO];
[webView loadHTMLString:_webStrbaseURL:[NSURLfileURLWithPath:[[NSBundlemainBundle]
pathForResource:@"news_detail"ofType:@"html"]]];
[self.viewaddSubview:webView];
具体步骤就是如此,希望对大家有所帮助
相关文章推荐
- ios 动画学习的套路 (二)
- IOS热更新-JSPatch实现原理+Patch现场恢复
- JSBridge(Android和IOS平台)的设计和实现
- 转场动画
- 不容错过的iOS 8的导航交互
- 安装测试版ios10和xcode8.0beta(8S128d)
- ios加载第三方库:横竖屏切换错误
- iOS开发——SiriKit应用
- iOS开发人员需要了解的苹果开发者账号与邓白氏编码
- iOS开发实战——摄像头与相册权限获取逻辑优化
- iOS开发--AVFoundation自定义相机
- 『零行代码』解决键盘遮挡问题(iOS)
- iOS Quartz2D基本图形绘制
- iOS 开发各种传值
- textView限制字数(超简单,不走弯路)(解决联想输入及iOS7崩溃等问题)
- IOS SingleView
- iOS之设置用户头像的圆角
- iOS之iPhone解锁界面的"滑动来解锁"闪烁动画效果,好奇的赶紧进来取走,别看了,说的就是你0.0
- openCV 2.4.13 iOS background_segm.hpp 'list' file not found
- vmstat 与 iostat使用