I was wondering how it is possible to insert text between HTML tags in Cocoa. I am displaying source code in a NSTextView – example:
<html>
<head>
<title>test</title>
</head>
<body>hello!</body>
</html>
The code above can vary in size, but what should I do if I wanted to insert say <link rel="apple-touch-icon" href="webclip.png" /> at any place between the <head> tags?
Edit: To clarify, I am not looking on how to insert text into an NSTextView, but on how to insert the text between the head tags that are already in the textview. I’m working on a HTML editor.
Thank you! 🙂
Having not written an HTML editor before, I’m not entirely sure of the best approach for this. However, you’ll obviously need some way of parsing the HTML. Since you’re using Cocoa, you’ve got a few options.
NSXMLParserDelegate, which has methods that get called when the parser encounters various XML components (elements, attributes, text, etc.)NSStringobjects a lot.However, I’ve used libxml extensively in an iOS app for parsing XML and it works beautifully once you get used to the C-style interface. I’m sure it works just as nicely on Mac OS X, too. This is probably what I’d recommend for your problem.