您的位置:首页 > 理论基础 > 计算机网络

基于网络的客户端开发技巧——第二篇Webview及正则

2011-05-23 11:29 417 查看
目前微博客户端基本都使用Webview,这篇就详细讲解一下使用利用Webview展示微博的技巧。当然,主要以代码为主。Demo暂时就不提供了。

之前也有几篇博文提到相关的,这里简单总结一下。

为什么要用webview来展示呢,就我目前的经验来看,为了文字链接图片混排比较方便。
所以既然要混排,又不能让人觉得这是webview,首先就要禁止bounces,也就是边缘滚动。方法如下:
[(UIScrollView *)[[webview subviews] objectAtIndex:0] setBounces:NO];


webview的各种复制,全选禁止方法:
document.documentElement.style.webkitTouchCallout = “none”; //禁止弹出菜单

document.documentElement.style.webkitUserSelect = “none”;//禁止选中


具体代码可以看下
http://www.minroad.com/?p=275

http://www.minroad.com/?p=266

webview使用起来注意的问题有:
1.loadhtml相当慢,所以要做到良好的展示效果,想办法预加载吧。当然用JS也可以。
2.使用js的时候千万要外部导入js,不然会调试到头大都调试不好。。各种转义字符
3.对于iphone4,记得图片尺寸要双倍。
4.webview的html里面尽量用/”而不是’ 比如<img width=/”20/”>而不是<img width=’20′>

下面说一下正则,将链接替换的方法,用的RegexKitLite:
- (NSString *)replacePersonLink:(NSString *)_str

{

NSString *regexString       = @"@//b(//w+)//b";

NSString *replaceWithString = @"<a href=/"at:$1/">@$1</a>";

NSString *replacedString    = NULL;

NSLog(@"%@",replacedString);

return replacedString;

}


提取链接的方法:
NSString *linkString = [NSString stringWithFormat:@"%@ %@",[dic getWBText],[dic getWBRetweetedText]];

NSString *regexString  = @"//bhttps?://[a-zA-Z0-9//-.]+(?::(//d+))?(?:(?:/[a-zA-Z0-9//-._?,'+//&%$=~*!():@////]*)+)?";

NSArray *splitArray = [linkString componentsMatchedByRegex:regexString];


解析一个字符串的方法
NSDictionary *urlDictionary = [searchString
dictionaryByMatchingRegex:regexString withKeysAndCaptures:@"url", 1,
@"followed",2,@"source",3, NULL];

if (![[urlDictionary allKeys] containsObject:@"source"]) {

NSString *regexString  = @"<a href=/"/" rel=/"([^>]+)/">([^>]+)</a>";

urlDictionary = [searchString dictionaryByMatchingRegex:regexString withKeysAndCaptures:@"followed",1,@"source",2, NULL];


source string为”<a href=/”http://t.sina.com.cn/” rel=/”nofollow/”>新浪微博</a>”

利用这些,基本可以完成微博上的需求。同时可以利用JS实现渐近渐现等效果,用CSS将布局处理的更合理。不会用CSS就td吧。。。

摘自:http://www.minroad.com/?p=296
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: