Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 浏览文本,解析链接_Ios_Objective C_Cocoa Touch - Fatal编程技术网

Ios 浏览文本,解析链接

Ios 浏览文本,解析链接,ios,objective-c,cocoa-touch,Ios,Objective C,Cocoa Touch,我有包含链接的文本(这些是图像链接) 我需要解析文本以便构建它,当链接被取消计数时,图像被提取 例如: “我曾经有一只狐狸http://mysite.com/images/fox.jpg。 我还养了一只小狗http://mysite.com/images/dog.jpg” 让它看起来像这样: 我曾经养过一只狐狸 我还养了一只小狗和一只公鸡 我可以用什么来实现这个目标 我已经设置了视图,但现在它只显示我的文本。 如何将图像添加到该视图中? 如何解析它们? 最好的方法是什么?当您检测到图像链接时,您

我有包含链接的文本(这些是图像链接) 我需要解析文本以便构建它,当链接被取消计数时,图像被提取

例如:

“我曾经有一只狐狸
http://mysite.com/images/fox.jpg
。 我还养了一只小狗
http://mysite.com/images/dog.jpg

让它看起来像这样:

我曾经养过一只狐狸

我还养了一只小狗和一只公鸡

我可以用什么来实现这个目标

我已经设置了视图,但现在它只显示我的文本。 如何将图像添加到该视图中? 如何解析它们?
最好的方法是什么?

当您检测到图像链接时,您可以首先将图像链接包装在
img src=
标记中(最有可能使用
stringbyreplacingoccurrencesofstring:withString:
将其转换为HTML)

因此,将所有链接转换为:

http://mysite.com/images/rooster.jpg


拥有HTML后,可以将其加载到
UIWebView


希望这能有所帮助。

您可以使用
UIWebView
来显示文本,无论在哪里找到有效的图像链接,都可以将其包装在
标记中

要解析这些链接,可以使用正则表达式。您可能需要自己调整正则表达式以达到精确的规格,但粗略的jist应该是
http(?:s)?:/。+\(?:jpg | jpeg | png | gif | bmp)
。这将获取一个以http或https开头,以.jpg、.png等结尾的Web链接(顺便说一句,未经测试)

在代码中,您可以将其作为


我们可以编写html代码并在textview中显示

UIWebView* webView = ...;
[webView loadHTMLString:webViewStr baseURL:nil];
NSString*regExpString=@“http(?:s):/。+\\(?:jpg | jpeg | png | gif | bmp)”;
NSString*yourStr=@“带有图像和链接的测试文章带有图像和链接的测试文章http://image.gif";
NSRegularExpression*storyRegex=[[NSRegularExpression alloc]initWithPattern:regExpString选项:nsregularexpressioncase不敏感错误:nil];
NSString*tempStr=[storyRegex StringByReplacingMatchesInstalling:yourStr选项:0范围:NSMakerRange(0,storyStr.length),带模板:@”“;
NSString*htmlStr=[NSString stringWithFormat:@“%@”,tempStr];
[self.txtView设置值:htmlStr forKey:@“contentToHTMLString”];
http://mysite.com/images/rooster.jpg
<img src="http://mysite.com/images/rooster.jpg">
NSString *regExpString = @"http(?:s)?://.+\\.(?:jpg|jpeg|png|gif|bmp)";
NSString *storyStr = @"...";    

NSRegularExpression *storyRegex = [[NSRegularExpression alloc] initWithPattern:regExpString
                                                                      options:NSRegularExpressionCaseInsensitive
                                                                        error:nil];

NSString* webViewStr = [storyRegex stringByReplacingMatchesInString:storyStr options:0 range:NSMakeRange(0, storyStr.length) withTemplate:@"<br\><img src=\"$0\"></img><br\>"];
UIWebView* webView = ...;
[webView loadHTMLString:webViewStr baseURL:nil];
NSString *regExpString = @"http(?:s)?://.+\\.(?:jpg|jpeg|png|gif|bmp)";
NSString *yourStr = @"Test Article with Image and LinksTest Article with Image and Links http://image.gif";
NSRegularExpression *storyRegex = [[NSRegularExpression alloc] initWithPattern:regExpString options:NSRegularExpressionCaseInsensitive error:nil];

NSString* tempStr = [storyRegex stringByReplacingMatchesInString:yourStr options:0 range:NSMakeRange(0, storyStr.length) withTemplate:@"<br\><img src=\"$0\"></img><br\>"];
NSString *htmlStr = [NSString stringWithFormat:@"<html> %@ </html>",tempStr];
[self.txtView setValue:htmlStr forKey:@"contentToHTMLString"];