Reputation: 2937
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! :)
Upvotes: 0
Views: 347
Reputation: 26859
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.)NSString
objects a lot.Upvotes: 1
Reputation: 96353
A text view owns a text storage, which is a kind of mutable attributed string. You can replace an empty range with the string you want to insert to insert it.
Of course, you will need to parse the HTML to determine where to insert it.
Upvotes: 0