HTML Format in UITextView
The problem there is that you have to change the Character Encoding options from NSUnicodeStringEncoding to NSUTF8StringEncoding to load your of your html the proper way. I think you should create a string extension read-only computed property to convert your html code to attributed string:
Xcode 8.3.1 • Swift 3.1
extension Data {
var attributedString: NSAttributedString? {
do {
return try NSAttributedString(data: self, options:[NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: String.Encoding.utf8.rawValue], documentAttributes: nil)
} catch {
print(error)
}
return nil
}
}
extension String {
var data: Data {
return Data(utf8)
}
}
let htmlStringCode = "Für mehr Informationen klicken sie <a href=\"http://www.samplelink.com/subpage.php?id=8\">here</a>"
htmlStringCode.data.attributedString?.string ?? "" // "Für mehr Informationen klicken sie here"
in your case
yourTextView.attributedText = htmlStringCode.data.attributedString
Display html text in uitextview
Use a UIWebView on iOS 5-.
On iOS 6+ you can use UITextView.attributedString
, see https://stackoverflow.com/a/20996085 for how.
There's also an undocumented -[UITextView setContentToHTMLString:]
method. Do not use this if you want to submit to AppStore.
Displaying HTML content in UITextView
Try This
var attrStr = try! NSAttributedString(
data: "<b><i>text</i></b>".data(using: String.Encoding.unicode, allowLossyConversion: true)!,
options:[NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil)
YOUR_TEXTVIEW.attributedText = attrStr
Swift: Display HTML data in a label or textView
For Swift 5:
extension String {
var htmlToAttributedString: NSAttributedString? {
guard let data = data(using: .utf8) else { return nil }
do {
return try NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html, .characterEncoding:String.Encoding.utf8.rawValue], documentAttributes: nil)
} catch {
return nil
}
}
var htmlToString: String {
return htmlToAttributedString?.string ?? ""
}
}
Then, whenever you want to put HTML text in a UITextView use:
textView.attributedText = htmlText.htmlToAttributedString
How to maintain indentations of HTML content in UITextView in Swift?
Just if you receive Html data Display it by using attributedText
Convert html to AttributedString
:
// Read Html of Your example From Bundle
guard let htmlFile = Bundle.main.path(forResource: "dataHtml", ofType: "html") else { return}
// Safe convert html to string
guard let html = try? String(contentsOfFile: htmlFile, encoding: String.Encoding.utf8)else { return}
let options = [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html]
// Safe string to Data
guard let htmlData = NSString(string: html).data(using: String.Encoding.unicode.rawValue) else { return}
// Safe attributedString from data
guard let attributedString = try? NSAttributedString(data: htmlData, options: options, documentAttributes: nil) else { return}
textView.attributedText = attributedString
textView.textColor = .red // you can comment or change font and color
Result on your example :
HTML string in UITextView AppStore rejection
Part 1 - App Store rejection will only be because of one of the reasons in the App Review Guidelines. There's plenty to worry about there without considering how you format some text. But I doubt your text formatting alone would get you rejected.
Check this and read it carefully: https://developer.apple.com/app-store/review/guidelines/
Part 2 - Is it a UITextField
or a UITextView
. If your text is getting smaller than you've set in a UITextField
it's probably because you have adjustsFontSizeToFitWidth
set to YES
and the text is wider than the text field. Fixing that would require us to see more of you code and maybe a screenshot.
If it's a UITextView
check this answer: How to change the uitextview font size. See if you have it set to "Selectable" in the storyboard.
But you might want to make it just a label.
Related Topics
How to Set Cmake_C_Compiler and Cmake_Cxx_Compiler for Building Assimp for iOS
Wireless iPhone App Distribution - Problem with Itms-Services Protocol
Attempting to Load the View of a View Controller While It Is Deallocating... Uisearchcontroller
Share Extension to Open Containing App
Take a Picture on iPhone Without Showing Controls
Ios7 Uiswitch Its Event Valuechanged: Calling Continuously Is This Bug or What..
Display Done Button on Uipickerview
Modal Segue, Navigation Bar Disappears
Ios: How to Find the Creation Date of a File
Using Existing System Sounds in iOS App [Swift|
Add Uipickerview in Uiactionsheet from iOS 8 Not Working
Querying iOS Keychain Using Swift
Autolayout Complains About Constraints for 2 Uitextfields with No Borders
How to Fix the "Uipopovercontroller Is Deprecated" Warning