NSRegularExpression: Replace url text with tags

前端 未结 2 884
逝去的感伤
逝去的感伤 2021-01-12 02:43

I am currently using a UIWebView to stylize posts from twitter. Some tweets of course contain URL\'s, but do not contain the tags. I am able to pull o

2条回答
  •  甜味超标
    2021-01-12 03:09

    You could use stringByReplacingOccurrencesOfString:withString: to search for your match and replace it with the HTML link.

    NSString *htmlTweet = [tweet stringByReplacingOccurrencesOfString:match withString:html];
    

    (You might also use the range that you get from rangeOfFirstMatchInString:options:range in stringByReplacingCharactersInRange:withString: but I'm not sure what happens you pass a string thats longer than the ranges length in this case).

    Note that your search will only find the first link in a tweet, and if there are several matches you'll miss those.

提交回复
热议问题