aryaxt / iOS-Rich-Text-Editor

A Rich Text Editor for iOS
Other
893 stars 158 forks source link

Getting HTML String from server and initializing rich text with it #38

Closed alideo closed 10 years ago

alideo commented 10 years ago

Hi,

I have a scenario where i am getting HTML string from server and then I have to show it to the user which can be further edited using rich text editor. In that case the NSData approach outlined in the core data issue cannot be used. I tried some other libs to convert html string to NSAttributedString as well. Could you point me in the right direction regarding this issue. Thanking you in advance for your help.

aryaxt commented 10 years ago

Hi, What is the exact problem? The html convertor used by this editor is a custom one written by me, and it's very basic. I'm planning to switch to using apple's new api for this conversion (iOS7) Give the code below a try and let me know if it works.

NSError error ; NSData data = [htmlString dataUsingEncoding:NSUTF8StringEncoding]; NSAttributedString *str = [[NSAttributedString alloc] initWithData:data
options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: [NSNumber numberWithInt:NSUTF8StringEncoding]} documentAttributes:nil error:&error];

myRichTextEditor.attributedText = str;

alideo commented 10 years ago

This code is related to iOS 7 i think I have to make the app compatible with iOS 6 also. The basic problem is as follows:

1) App recieves xml from server 2) xml has a html string tag in it 3) the html tag gives something like: < p >< b >Test< /b > Hello < i >world< /i >< /p > (spaces are intentional) 4) I want to pass this on to your editor. I can do it using the attributedText property exposed. 5) The issue is converting the above mentioned html string into NSAttributedString. Do i have to make a parser and then make the attributed string as the editor does when a property is selected? or does the library have some function which will take this html string and return the appropriate NSAttributedString.

I hope I am able to better explain the scenario.

aryaxt commented 10 years ago

I have a solution for iOS7 on the second branch. That doesn't work on iOS 6 though. Just search for html to NSAttributedString, I've see a couple of categories that do that. Convert the html to NSAttributedString and set it as the attributedText property of the rcih-text-editor and it should work.

https://github.com/Cocoanetics/DTCoreText