0

I have some HTML content having its own styling(font and text colour) properties. I need to load this HTML content to a UITextView, but the UITextView loads the HTML content using the styling properties of the HTML content and not the UIFont and colour property of UITextView.

What I need:

I need to load the HTML content to UITextView with the UIFont and textColour properties of the UITextView.

3 Answers3

3

let this be the HTML content

<span style=\134"color: rgb(173, 173, 173); font-family: titilliumtitle12regular; font-size: 16px; text-transform: none; float: none; display: inline !important;\134">testing purpose Matchboxx is the first social sporting application that gives full control to sports professionals and enthusiasts who desire to set-up, manage, participate or watch sports teams in any location around the world. This, coupled with a suite of social tools which provide users with ease-of-use sport event management, provides a unique and rewarding experience to users around the world. Join today and get started!Testing</span><br>

This thing worked for me,

loading HTML content to NSAttributedString

NSString *htmlString = HTML_Content;
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithData: [htmlString dataUsingEncoding:NSUnicodeStringEncoding] options: @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType} documentAttributes:nil error: nil];

Then Adding an attribute for UIFont

[attributedString addAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"nexabold" size:16.0],NSForegroundColorAttributeName:[UIColor blackColor]} range:NSMakeRange(0, attributedString.length)];

setting the UITextView with Attributed Text

txtView.text = @"";
txtView.attributedText = attributedString;
txtView.textAlignment = NSTextAlignmentJustified;
0

// try like this

NSString *text = textView.text;

// This will give me an attributedString with the base text-style
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:text];
[attributedString addAttribute:NSForegroundColorAttributeName
                             value:[UIColor redColor]
                             range:NSMakeRange(0, text.length)];

textView.attributedText = attributedString;
Satyanarayana
  • 1,059
  • 6
  • 16
0

Try this :

UIWebView *webView  = [[UIWebView alloc]initWithFrame:self.view.bounds];
[webView loadHTMLString:@"your string" baseURL:nil];
[yourTextView addSubview:webView];
Bista
  • 7,869
  • 3
  • 27
  • 55
Pavankumar
  • 288
  • 1
  • 10