Created
December 30, 2016 10:58
-
-
Save JigarM/5e14a354981252c6b19ff42bd28071d3 to your computer and use it in GitHub Desktop.
Xamarin IOS C# NSAttributedString
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//https://forums.xamarin.com/discussion/18460/uitextview-and-html | |
//https://gist.github.com/adrianstevens/933d66faaea83e165f5f | |
using System; | |
using MonoTouch.UIKit; | |
using MonoTouch.Foundation; | |
namespace YourNameSpaceGoesHere | |
{ | |
public class AttributedTextToHTML | |
{ | |
public AttributedTextToHTML () | |
{ | |
} | |
public static NSAttributedString GetAttributedStringFromHtml(NSString html) | |
{ | |
NSData htmlData = html.DataUsingEncoding (NSStringEncoding.UTF8); | |
NSAttributedStringDocumentAttributes importParams = new NSAttributedStringDocumentAttributes(); | |
importParams.DocumentType = NSDocumentType.HTML; | |
NSError error = new NSError (); | |
error = null; | |
NSDictionary dict = new NSDictionary (); | |
var attrString = new NSAttributedString (htmlData, importParams, out dict, ref error); | |
return attrString; | |
} | |
public static NSAttributedString GetAttributedStringFromHtml(NSUrl html) | |
{ | |
// NSData htmlData = html.DataUsingEncoding (NSStringEncoding.UTF8); | |
NSAttributedStringDocumentAttributes importParams = new NSAttributedStringDocumentAttributes(); | |
importParams.DocumentType = NSDocumentType.HTML; | |
NSError error = new NSError (); | |
error = null; | |
NSDictionary dict = new NSDictionary (); | |
var attrString = new NSAttributedString (html, ref error); | |
//var attrString = new NSAttributedString (htmlData, importParams, out dict, ref error); | |
return attrString; | |
} | |
/// <summary> | |
/// Gets the HTML from attributed string. | |
/// </summary> | |
/// <returns>The HTML from attributed string.</returns> | |
/// <param name="attributedString">Attributed string.</param> | |
public static NSString GetHTMLFromAttributedString(NSAttributedString attributedString) | |
{ | |
NSAttributedStringDocumentAttributes attr = new NSAttributedStringDocumentAttributes (); | |
attr.DocumentType = NSDocumentType.HTML; | |
NSDictionary exportParams = new NSDictionary (); | |
// NSData htmlData = new NSData (); | |
NSError error = new NSError (); | |
var attrString = new NSAttributedString (attributedString.Value, attr, ref error); | |
var retString = new NSString (attrString.Value); | |
//new NSRange (0, 100); | |
//NSString retString = new NSString( | |
//NSString retString = new NSString (htmlData, NSStringEncoding.UTF8); | |
return retString; | |
// var attributes = new NSAttributedStringDocumentAttributes (); | |
// attributes.DocumentType = NSDocumentType.HTML; | |
//attributes.StringEncoding = NSStringEncoding.UTF8; | |
//var error = new NSError (); | |
//var htmlString = new NSAttributedString (text, attributes, ref error); | |
//return htmlString; | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment