How do I convert uitextview content to include simplified HTML Tags and ultimately be a string to send?
up vote
1
down vote
favorite
I am trying to send the contents of a textView to a web service, but I want it to include HTML tags BUT the simplified version. See code for details:
let attrStr = NSAttributedString(string: self.textView.text)
let documentAttributes = [NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType]
do {
let htmlData = try attrStr.data(from: NSMakeRange(0, attrStr.length), documentAttributes: documentAttributes)
if let htmlString = String(data: htmlData, encoding: String.Encoding.utf8) {
print("YOUR HTMLSTRING IS: (htmlString)")
}
} catch {
print("ERROR CREATING HTML FROM ATTRIBUTED STRING")
}
htmlString is the whole shebang, something like this:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="Content-Style-Type" content="text/css">
<title></title>
<meta name="Generator" content="Cocoa HTML Writer">
<style type="text/css">
And then the whole HTML, body, class, double quotes tags from HTML. But the web service cannot accept those, especially the double
quotes I believe is the problem. Android side is able to send only the
important stuff such as the br
and the p tags. That's really all I
need.
So question:
How do I simplify what I'm sending from the whole DOCTYPE html tag to
the "Test 123 <br> <p> Test 123"?
html ios swift uitextview
|
show 1 more comment
up vote
1
down vote
favorite
I am trying to send the contents of a textView to a web service, but I want it to include HTML tags BUT the simplified version. See code for details:
let attrStr = NSAttributedString(string: self.textView.text)
let documentAttributes = [NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType]
do {
let htmlData = try attrStr.data(from: NSMakeRange(0, attrStr.length), documentAttributes: documentAttributes)
if let htmlString = String(data: htmlData, encoding: String.Encoding.utf8) {
print("YOUR HTMLSTRING IS: (htmlString)")
}
} catch {
print("ERROR CREATING HTML FROM ATTRIBUTED STRING")
}
htmlString is the whole shebang, something like this:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="Content-Style-Type" content="text/css">
<title></title>
<meta name="Generator" content="Cocoa HTML Writer">
<style type="text/css">
And then the whole HTML, body, class, double quotes tags from HTML. But the web service cannot accept those, especially the double
quotes I believe is the problem. Android side is able to send only the
important stuff such as the br
and the p tags. That's really all I
need.
So question:
How do I simplify what I'm sending from the whole DOCTYPE html tag to
the "Test 123 <br> <p> Test 123"?
html ios swift uitextview
Here is a sample example of what you can do for bold & italic: stackoverflow.com/questions/44027651/… You might need to adapt according to what tags you want. Also, it's strange that your webservice doesn't parse correctly the double quotes, what do you do exactly? Do you do a GET request and put the text in the URL? if yes, you might want to percent escape the characters.
– Larme
Nov 12 at 15:33
Based on my previous answer, I'd go with<br>
replacingn
, and add<p>
&</p>
for eachNSAttributedString.key.paragraph
found.
– Larme
Nov 12 at 15:52
What would I replace "NSFontAttributeName" with in my case? Thanks
– alwongg
Nov 12 at 16:16
NSAttributedString.Key.paragraphStyle
which in Swift 3 (I guess that's the version you use), should beNSParagraphStyleAttributeName
.
– Larme
Nov 12 at 16:18
I am trying to follow your sample example to work in my case but I am having some trouble. When I use NSParagraphStyleAttributeName, and get a value in the closure, I am casting value as! NSParagraphStyle but then doing value dot property only shows customMirror, debugDescription etc, nothing showing I can check for /p or /br
– alwongg
Nov 12 at 16:32
|
show 1 more comment
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I am trying to send the contents of a textView to a web service, but I want it to include HTML tags BUT the simplified version. See code for details:
let attrStr = NSAttributedString(string: self.textView.text)
let documentAttributes = [NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType]
do {
let htmlData = try attrStr.data(from: NSMakeRange(0, attrStr.length), documentAttributes: documentAttributes)
if let htmlString = String(data: htmlData, encoding: String.Encoding.utf8) {
print("YOUR HTMLSTRING IS: (htmlString)")
}
} catch {
print("ERROR CREATING HTML FROM ATTRIBUTED STRING")
}
htmlString is the whole shebang, something like this:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="Content-Style-Type" content="text/css">
<title></title>
<meta name="Generator" content="Cocoa HTML Writer">
<style type="text/css">
And then the whole HTML, body, class, double quotes tags from HTML. But the web service cannot accept those, especially the double
quotes I believe is the problem. Android side is able to send only the
important stuff such as the br
and the p tags. That's really all I
need.
So question:
How do I simplify what I'm sending from the whole DOCTYPE html tag to
the "Test 123 <br> <p> Test 123"?
html ios swift uitextview
I am trying to send the contents of a textView to a web service, but I want it to include HTML tags BUT the simplified version. See code for details:
let attrStr = NSAttributedString(string: self.textView.text)
let documentAttributes = [NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType]
do {
let htmlData = try attrStr.data(from: NSMakeRange(0, attrStr.length), documentAttributes: documentAttributes)
if let htmlString = String(data: htmlData, encoding: String.Encoding.utf8) {
print("YOUR HTMLSTRING IS: (htmlString)")
}
} catch {
print("ERROR CREATING HTML FROM ATTRIBUTED STRING")
}
htmlString is the whole shebang, something like this:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="Content-Style-Type" content="text/css">
<title></title>
<meta name="Generator" content="Cocoa HTML Writer">
<style type="text/css">
And then the whole HTML, body, class, double quotes tags from HTML. But the web service cannot accept those, especially the double
quotes I believe is the problem. Android side is able to send only the
important stuff such as the br
and the p tags. That's really all I
need.
So question:
How do I simplify what I'm sending from the whole DOCTYPE html tag to
the "Test 123 <br> <p> Test 123"?
html ios swift uitextview
html ios swift uitextview
edited Nov 12 at 17:58
rmaddy
237k27308375
237k27308375
asked Nov 12 at 15:22
alwongg
164
164
Here is a sample example of what you can do for bold & italic: stackoverflow.com/questions/44027651/… You might need to adapt according to what tags you want. Also, it's strange that your webservice doesn't parse correctly the double quotes, what do you do exactly? Do you do a GET request and put the text in the URL? if yes, you might want to percent escape the characters.
– Larme
Nov 12 at 15:33
Based on my previous answer, I'd go with<br>
replacingn
, and add<p>
&</p>
for eachNSAttributedString.key.paragraph
found.
– Larme
Nov 12 at 15:52
What would I replace "NSFontAttributeName" with in my case? Thanks
– alwongg
Nov 12 at 16:16
NSAttributedString.Key.paragraphStyle
which in Swift 3 (I guess that's the version you use), should beNSParagraphStyleAttributeName
.
– Larme
Nov 12 at 16:18
I am trying to follow your sample example to work in my case but I am having some trouble. When I use NSParagraphStyleAttributeName, and get a value in the closure, I am casting value as! NSParagraphStyle but then doing value dot property only shows customMirror, debugDescription etc, nothing showing I can check for /p or /br
– alwongg
Nov 12 at 16:32
|
show 1 more comment
Here is a sample example of what you can do for bold & italic: stackoverflow.com/questions/44027651/… You might need to adapt according to what tags you want. Also, it's strange that your webservice doesn't parse correctly the double quotes, what do you do exactly? Do you do a GET request and put the text in the URL? if yes, you might want to percent escape the characters.
– Larme
Nov 12 at 15:33
Based on my previous answer, I'd go with<br>
replacingn
, and add<p>
&</p>
for eachNSAttributedString.key.paragraph
found.
– Larme
Nov 12 at 15:52
What would I replace "NSFontAttributeName" with in my case? Thanks
– alwongg
Nov 12 at 16:16
NSAttributedString.Key.paragraphStyle
which in Swift 3 (I guess that's the version you use), should beNSParagraphStyleAttributeName
.
– Larme
Nov 12 at 16:18
I am trying to follow your sample example to work in my case but I am having some trouble. When I use NSParagraphStyleAttributeName, and get a value in the closure, I am casting value as! NSParagraphStyle but then doing value dot property only shows customMirror, debugDescription etc, nothing showing I can check for /p or /br
– alwongg
Nov 12 at 16:32
Here is a sample example of what you can do for bold & italic: stackoverflow.com/questions/44027651/… You might need to adapt according to what tags you want. Also, it's strange that your webservice doesn't parse correctly the double quotes, what do you do exactly? Do you do a GET request and put the text in the URL? if yes, you might want to percent escape the characters.
– Larme
Nov 12 at 15:33
Here is a sample example of what you can do for bold & italic: stackoverflow.com/questions/44027651/… You might need to adapt according to what tags you want. Also, it's strange that your webservice doesn't parse correctly the double quotes, what do you do exactly? Do you do a GET request and put the text in the URL? if yes, you might want to percent escape the characters.
– Larme
Nov 12 at 15:33
Based on my previous answer, I'd go with
<br>
replacing n
, and add <p>
& </p>
for each NSAttributedString.key.paragraph
found.– Larme
Nov 12 at 15:52
Based on my previous answer, I'd go with
<br>
replacing n
, and add <p>
& </p>
for each NSAttributedString.key.paragraph
found.– Larme
Nov 12 at 15:52
What would I replace "NSFontAttributeName" with in my case? Thanks
– alwongg
Nov 12 at 16:16
What would I replace "NSFontAttributeName" with in my case? Thanks
– alwongg
Nov 12 at 16:16
NSAttributedString.Key.paragraphStyle
which in Swift 3 (I guess that's the version you use), should be NSParagraphStyleAttributeName
.– Larme
Nov 12 at 16:18
NSAttributedString.Key.paragraphStyle
which in Swift 3 (I guess that's the version you use), should be NSParagraphStyleAttributeName
.– Larme
Nov 12 at 16:18
I am trying to follow your sample example to work in my case but I am having some trouble. When I use NSParagraphStyleAttributeName, and get a value in the closure, I am casting value as! NSParagraphStyle but then doing value dot property only shows customMirror, debugDescription etc, nothing showing I can check for /p or /br
– alwongg
Nov 12 at 16:32
I am trying to follow your sample example to work in my case but I am having some trouble. When I use NSParagraphStyleAttributeName, and get a value in the closure, I am casting value as! NSParagraphStyle but then doing value dot property only shows customMirror, debugDescription etc, nothing showing I can check for /p or /br
– alwongg
Nov 12 at 16:32
|
show 1 more comment
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53265176%2fhow-do-i-convert-uitextview-content-to-include-simplified-html-tags-and-ultimate%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53265176%2fhow-do-i-convert-uitextview-content-to-include-simplified-html-tags-and-ultimate%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Here is a sample example of what you can do for bold & italic: stackoverflow.com/questions/44027651/… You might need to adapt according to what tags you want. Also, it's strange that your webservice doesn't parse correctly the double quotes, what do you do exactly? Do you do a GET request and put the text in the URL? if yes, you might want to percent escape the characters.
– Larme
Nov 12 at 15:33
Based on my previous answer, I'd go with
<br>
replacingn
, and add<p>
&</p>
for eachNSAttributedString.key.paragraph
found.– Larme
Nov 12 at 15:52
What would I replace "NSFontAttributeName" with in my case? Thanks
– alwongg
Nov 12 at 16:16
NSAttributedString.Key.paragraphStyle
which in Swift 3 (I guess that's the version you use), should beNSParagraphStyleAttributeName
.– Larme
Nov 12 at 16:18
I am trying to follow your sample example to work in my case but I am having some trouble. When I use NSParagraphStyleAttributeName, and get a value in the closure, I am casting value as! NSParagraphStyle but then doing value dot property only shows customMirror, debugDescription etc, nothing showing I can check for /p or /br
– alwongg
Nov 12 at 16:32