Wyświetlanie tekstu html w uitextview
Jak mogę wyświetlić tekst HTML w textview?
Na przykład,
string <h1>Krupal testing <span style="font-weight:
bold;">Customer WYWO</span></h1>
Załóżmy, że tekst jest pogrubiony, więc wyświetla się w textview jako pogrubienie ale chcę wyświetlać normalny tekst. Czy jest to możliwe w iPhone SDK?
11 answers
Użyj UIWebView na iOS 5-.
Na iOS 6+ możesz użyć UITextView.attributedString
, zobacz https://stackoverflow.com/a/20996085 Jak.
Jest też nieudokumentowana -[UITextView setContentToHTMLString:]
Metoda. Nie używaj tego, jeśli chcesz przesłać do AppStore.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 11:47:15
Użyj następującego bloku kodu dla ios 7+.
NSString *htmlString = @"<h1>Header</h1><h2>Subheader</h2><p>Some <em>text</em></p><img src='http://blogs.babble.com/famecrawler/files/2010/11/mickey_mouse-1097.jpg' width=70 height=100 />";
NSAttributedString *attributedString = [[NSAttributedString alloc]
initWithData: [htmlString dataUsingEncoding:NSUnicodeStringEncoding]
options: @{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType }
documentAttributes: nil
error: nil
];
textView.attributedText = attributedString;
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-04-13 08:57:36
Dla Swift 4 I Swift 4.2:
let htmlString = "<html>" +
"<head>" +
"<style>" +
"body {" +
"background-color: rgb(230, 230, 230);" +
"font-family: 'Arial';" +
"text-decoration:none;" +
"}" +
"</style>" +
"</head>" +
"<body>" +
"<h1>A title</h1>" +
"<p>A paragraph</p>" +
"<b>bold text</b>" +
"</body></head></html>"
let htmlData = NSString(string: htmlString).data(using: String.Encoding.unicode.rawValue)
let options = [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html]
let attributedString = try! NSAttributedString(data: htmlData!, options: options, documentAttributes: nil)
textView.attributedText = attributedString
Dla Swift 3:
let htmlString = "<html>" +
"<head>" +
"<style>" +
"body {" +
"background-color: rgb(230, 230, 230);" +
"font-family: 'Arial';" +
"text-decoration:none;" +
"}" +
"</style>" +
"</head>" +
"<body>" +
"<h1>A title</h1>" +
"<p>A paragraph</p>" +
"<b>bold text</b>" +
"</body></head></html>"
let htmlData = NSString(string: htmlString).data(using: String.Encoding.unicode.rawValue)
let attributedString = try! NSAttributedString(data: htmlData!, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil)
textView.attributedText = attributedString
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-09-20 14:43:11
BHUPI ' s
Odpowiedź jest poprawna, ale jeśli chcesz połączyć własną czcionkę z UILabel lub UITextView z zawartością HTML, musisz poprawić swój html trochę:
NSString *htmlString = @"<b>Bold</b><br><i>Italic</i><p> <del>Deleted</del><p>List<ul><li>Coffee</li><li type='square'>Tea</li></ul><br><a href='URL'>Link </a>";
htmlString = [htmlString stringByAppendingString:@"<style>body{font-family:'YOUR_FONT_HERE'; font-size:'SIZE';}</style>"];
/*Example:
htmlString = [htmlString stringByAppendingString:[NSString stringWithFormat:@"<style>body{font-family: '%@'; font-size:%fpx;}</style>",_myLabel.font.fontName,_myLabel.font.pointSize]];
*/
NSAttributedString *attributedString = [[NSAttributedString alloc]
initWithData: [htmlString dataUsingEncoding:NSUnicodeStringEncoding]
options: @{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType }
documentAttributes: nil
error: nil
];
textView.attributedText = attributedString;
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-05-23 04:16:02
Możesz spojrzeć na klasy OHAttributedLabel, użyłem ich do przezwyciężenia tego rodzaju problemu z moim textField. W ten sposób nadpisali metodę drawRect, aby uzyskać wymagany styl.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-09-16 11:27:44
Moja pierwsza odpowiedź została udzielona, zanim system iOS 7 wprowadził wyraźne wsparcie dla wyświetlania przypisanych ciągów we wspólnych kontrolkach. Możesz teraz ustawić attributedText
z UITextView
na NSAttributedString
utworzony z treści HTML za pomocą:
-(id)initWithData:(NSData *)data options:(NSDictionary *)options documentAttributes:(NSDictionary **)dict error:(NSError **)error
- initWithData: options: documentAttributes: error: (Apple Doc)
oryginalna odpowiedź, zachowana do historii:
Jeśli nie użyjesz UIWebView
, twoje rozwiązanie będzie polegać bezpośrednio na CoreText
. Jak podkreśla ElanthiraiyanS, niektóre projekty open source mają pojawiły się, aby uprościć renderowanie tekstu rich text. Polecam NSAttributedString-Dodatki-do-HTML (Edit: projekt został zastąpiony DTCoreText ), który oferuje klasy do generowania i wyświetlania przypisanych ciągów z HTML.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-06-14 11:01:29
ODPOWIEDŹ dopasowała się do mnie, że od BHUPI.
Przeniesienie kodu do swift jak poniżej:
Zwróć uwagę "allowLossyConversion: false"
Jeśli ustawisz wartość na true, wyświetli się czysty tekst.
let theString = "<h1>H1 title</h1><b>Logo</b><img src='http://www.aver.com/Images/Shared/logo-color.png'><br>~end~"
let theAttributedString = try! NSAttributedString(data: theString.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!,
options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
documentAttributes: nil)
UITextView_Message.attributedText = theAttributedString
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-10-07 09:50:51
Dla Swift3
let theString = "<h1>H1 title</h1><b>Logo</b><img src='http://www.aver.com/Images/Shared/logo-color.png'><br>~end~"
let theAttributedString = try! NSAttributedString(data: theString.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!,
options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
documentAttributes: nil)
UITextView_Message.attributedText = theAttributedString
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-06-14 08:41:32
W niektórych przypadkach UIWebView jest dobrym rozwiązaniem. Ponieważ:
- wyświetla tabele, obrazy, inne pliki Jest szybki (w porównaniu z NSAttributedString: NSHTMLTextDocumentType)
- jest po wyjęciu z pudełka
Używanie NSAttributedString może prowadzić do awarii, jeśli html jest złożony lub zawiera tabele (więc przykład )
Do wczytywania tekstu do widoku sieci web można użyć poniższego fragmentu (tylko przykład):
func loadHTMLText(_ text: String?, font: UIFont) {
let fontSize = font.pointSize * UIScreen.screens[0].scale
let html = """
<html><body><span style=\"font-family: \(font.fontName); font-size: \(fontSize)\; color: #112233">\(text ?? "")</span></body></html>
"""
self.loadHTMLString(html, baseURL: nil)
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-03-30 17:35:09
Możesz również użyć jeszcze jednego sposobu. Biblioteka Three20 oferuje metodę, za pomocą której możemy skonstruować stylowy widok tekstu. Możesz pobrać bibliotekę tutaj: http://github.com/facebook/three20/
Klasa TTStyledTextLabel ma metodę o nazwie textFromXHTML: myślę, że to by służyło temu celowi. Ale byłoby to możliwe w trybie readonly. Nie sądzę, że pozwoli to na pisanie lub edycję treści HTML.
Istnieje również pytanie, które może Ci w tym pomóc: HTML String zawartość dla UILabel i TextView
Mam nadzieję, że to pomoże.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 11:55:07
NSDoc zapisać plik tekstowy w łańcuchu do pliku html, a następnie jednocześnie załadować go do webview, który jest w tym samym miejscu, co UITextView..
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-09-21 20:17:45