{"id":1190,"date":"2025-05-31T11:31:49","date_gmt":"2025-06-05T03:38:49","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=1190"},"modified":"2025-06-05T11:31:49","modified_gmt":"2025-06-05T03:38:49","slug":"uitextviewhtml","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/uitextviewhtml\/","title":{"rendered":"Swift UITextView \u8b80\u53d6 HTML \u7684\u6700\u4f73\u5be6\u8e10\uff082025 \u6700\u65b0\u8a9e\u6cd5\uff09"},"content":{"rendered":"<h2>\u4f7f\u7528 Swift \u8b80\u53d6 HTML \u5230 UITextView<\/h2>\n<p>\u5728 Swift \u958b\u767c\u4e2d\uff0c\u5e38\u5e38\u9700\u8981\u5728 <code>UITextView<\/code> \u4e2d\u986f\u793a HTML \u683c\u5f0f\u7684\u5167\u5bb9\u3002\u9019\u7bc7\u6587\u7ae0\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528\u6700\u65b0\u7684 Swift \u8a9e\u6cd5\u5c07 HTML \u5167\u5bb9\u6b63\u78ba\u986f\u793a\u5728 <code>UITextView<\/code> \u4e2d\uff0c\u4e26\u63d0\u4f9b\u5b8c\u6574\u7684\u5be6\u4f5c\u7bc4\u4f8b\u3001\u932f\u8aa4\u6392\u9664\u65b9\u6cd5\u53ca\u5ef6\u4f38\u61c9\u7528\u3002<\/p>\n<h3>\u57fa\u672c\u7684 HTML \u5167\u5bb9<\/h3>\n<p>\u6211\u5011\u9996\u5148\u9700\u8981\u6e96\u5099\u4e00\u6bb5 HTML \u5167\u5bb9\uff0c\u9019\u6bb5\u5167\u5bb9\u5c07\u88ab\u986f\u793a\u5728 <code>UITextView<\/code> \u4e2d\uff1a<\/p>\n<pre data-language=HTML><code class=\"language-markup line-numbers\">\u5951\u7d04\u7de8\u865f00261034115&lt;br&gt;&lt;a href='https:\/\/docs.google.com\/gview?202003\/00261034115-contract.pdf'&gt;\u5408\u7d04\u9023\u7d50&lt;\/a&gt;&lt;br&gt;\u89aa\u611b\u7684\u6703\u54e1\u60a8\u597d\u3002<\/code><\/pre>\n<h3>\u7279\u6b8a HTML href \u624b\u52d5\u8f49\u63db\u6210 UITextView \u9023\u7d50<\/h3>\n<p>\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u5c07\u7de8\u5beb\u4e00\u500b\u51fd\u6578\u4f86\u8655\u7406 HTML \u4e2d\u7684\u9023\u7d50\uff0c\u4e26\u5c07\u5176\u8f49\u63db\u70ba\u53ef\u9ede\u64ca\u7684\u683c\u5f0f\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">func findHtmlHref(textView: UITextView, content: String) -> UITextView {\n    if !content.contains(\"href\") {\n        let newContent = content.replacingOccurrences(of: \"&lt;br&gt;\", with: \"\\n\")\n        textView.text = newContent\n        return textView\n    }\n\n    do {\n        let doc: Document = try SwiftSoup.parse(content)\n        let link: Element = try doc.select(\"a\").first() ?? Element(\"a\")\n        let linkHref: String = try link.attr(\"href\")\n        let linkText: String = try link.text()\n\n        var newContent = content.replacingOccurrences(of: \"&lt;a href='\\(linkHref)'&gt;\u5408\u7d04\u9023\u7d50&lt;\/a&gt;\", with: \"@Terms@\")\n        newContent = newContent.replacingOccurrences(of: \"&lt;br&gt;\", with: \"\\n\")\n\n        textView.text = newContent\n        textView.attributedText = textView.attributedText?.replace(placeholder: \"@Terms@\", with: linkText, url: linkHref)\n\n        return textView\n    } catch {\n        print(\"Error parsing HTML: \\(error)\")\n        return textView\n    }\n}<\/code><\/pre>\n<h3>\u5982\u4f55\u547c\u53eb\u51fd\u6578<\/h3>\n<p>\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528\u4e0a\u8ff0\u51fd\u6578\u7684\u7bc4\u4f8b\uff1a<\/p>\n<pre><code class=\"line-numbers\">contentTextView = findHtmlHref(textView: yourTextView, content: yourContent)<\/code><\/pre>\n<h3>\u7d50\u679c\u5c55\u793a<\/h3>\n<p><img decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2021\/03\/IMG_1164.jpg\" alt=\"\u5c55\u793a\u7d50\u679c\" title=\"\"><\/p>\n<h3>\u5c07 HTML \u76f4\u63a5\u585e\u5165 UITextView<\/h3>\n<p>\u9664\u4e86\u624b\u52d5\u8655\u7406\u9023\u7d50\uff0c\u6211\u5011\u9084\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u64f4\u5c55\u5c07 HTML \u76f4\u63a5\u8f49\u63db\u70ba <code>NSAttributedString<\/code>\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">extension String {\n    var htmlToAttributedString: NSAttributedString? {\n        guard let data = data(using: .utf8) else { return nil }\n        do {\n            return try NSAttributedString(data: data, options: [.documentType: .html, .characterEncoding: String.Encoding.utf8.rawValue], documentAttributes: nil)\n        } catch {\n            print(\"Error converting HTML to NSAttributedString: \\(error)\")\n            return nil\n        }\n    }\n\n    var htmlToString: String {\n        return htmlToAttributedString?.string ?? \"\"\n    }\n}<\/code><\/pre>\n<h3>\u547c\u53eb\u64f4\u5c55<\/h3>\n<p>\u4f7f\u7528\u64f4\u5c55\u5c07 HTML \u5167\u5bb9\u8f09\u5165\u5230 <code>UITextView<\/code> \u4e2d\uff1a<\/p>\n<pre><code class=\"line-numbers\">contentTextView.attributedText = yourHtmlString.htmlToAttributedString<\/code><\/pre>\n<h3>\u7d50\u679c\u5c55\u793a<\/h3>\n<p><img decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2021\/03\/IMG_1164.jpg\" alt=\"\u5c55\u793a\u7d50\u679c\" title=\"\"><\/p>\n<h2>\u5e38\u898b\u554f\u984c\u89e3\u7b54 (Q&#038;A)<\/h2>\n<h3>Q1: \u70ba\u4ec0\u9ebc\u6211\u7684 HTML \u5167\u5bb9\u6c92\u6709\u6b63\u78ba\u986f\u793a\uff1f<\/h3>\n<p>A1: \u78ba\u4fdd\u4f60\u7684 HTML \u5b57\u4e32\u662f\u6709\u6548\u7684\uff0c\u4e26\u4e14\u6aa2\u67e5\u662f\u5426\u6709\u6b63\u78ba\u8655\u7406\u6240\u6709\u7684 HTML \u6a19\u7c64\u3002\u5982\u679c\u51fa\u73fe\u932f\u8aa4\uff0c\u6aa2\u67e5\u63a7\u5236\u53f0\u7684\u932f\u8aa4\u4fe1\u606f\u4ee5\u7372\u5f97\u66f4\u591a\u63d0\u793a\u3002<\/p>\n<h3>Q2: \u5982\u4f55\u5728 UITextView \u4e2d\u8655\u7406\u591a\u500b\u9023\u7d50\uff1f<\/h3>\n<p>A2: \u4f60\u53ef\u4ee5\u5728 <code>findHtmlHref<\/code> \u51fd\u6578\u4e2d\u4f7f\u7528\u8ff4\u5708\u904d\u6b77\u6240\u6709\u7684\u9023\u7d50\uff0c\u4e26\u70ba\u6bcf\u500b\u9023\u7d50\u751f\u6210\u53ef\u9ede\u64ca\u7684\u6587\u672c\u3002\u9019\u6a23\u53ef\u4ee5\u8b93 UITextView \u540c\u6642\u652f\u6301\u591a\u500b\u9023\u7d50\u3002<\/p>\n<h3>Q3: \u6709\u6c92\u6709\u66f4\u7c21\u55ae\u7684\u65b9\u5f0f\u4f86\u986f\u793a HTML\uff1f<\/h3>\n<p>A3: \u5982\u679c\u4f60\u7684\u9700\u6c42\u4e0d\u9700\u8981\u7279\u5225\u7684\u683c\u5f0f\u5316\uff0c\u4f7f\u7528\u4e0a\u9762\u7684 <code>htmlToAttributedString<\/code> \u64f4\u5c55\u53ef\u80fd\u662f\u6700\u7c21\u55ae\u7684\u65b9\u5f0f\u3002\u9019\u6a23\u53ef\u4ee5\u76f4\u63a5\u5c07 HTML \u8f49\u63db\u70ba <code>NSAttributedString<\/code>\uff0c\u4e26\u986f\u793a\u5728 <code>UITextView<\/code> \u4e2d\u3002<\/p>\n<div style=\"font-family: 'times new roman'; text-align: left;\">\n    <span style=\"font-size: x-large;\"><br \/>\n        <b style=\"background-color: white;\">Swift \u66f4\u591a\u6587\u7ae0<\/b><br \/>\n    <\/span>\n<\/div>\n<p><a href=\"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-340\/\" title=\"Swift \u5f48\u51fa\u8996\u7a97 AlertController \u7684\u4f7f\u7528\u65b9\u6cd5 \ud83d\udca5\" rel=\"internal\">Swift \u5f48\u51fa\u8996\u7a97 AlertController \u7684\u4f7f\u7528\u65b9\u6cd5 \ud83d\udca5<\/a><br \/>\n<a href=\"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-8\/\" title=\"Swift \u5224\u65b7\u87a2\u5e55\u65b9\u5411 \ud83d\udcf1\" rel=\"internal\">Swift \u5224\u65b7\u87a2\u5e55\u65b9\u5411 \ud83d\udcf1<\/a><br \/>\n<a href=\"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-6\/\" title=\"Swift Core Data \u5be6\u73fe \ud83d\udcbe\ud83d\udd25\" rel=\"internal\">Swift Core Data \u5be6\u73fe \ud83d\udcbe\ud83d\udd25<\/a><br \/>\n<a href=\"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-4\/\" title=\"Swift UISegmentedControl \ud83d\udcbb\u5206\u6bb5\u63a7\u5236\u5668\uff01\" rel=\"internal\">Swift UISegmentedControl \ud83d\udcbb\u5206\u6bb5\u63a7\u5236\u5668\uff01<\/a><br \/>\n<a href=\"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-5\/\" title=\"Swift \u5be6\u73fe\u62bd\u5c5c\u6548\u679c \ud83e\uddf9\" rel=\"internal\">Swift \u5be6\u73fe\u62bd\u5c5c\u6548\u679c \ud83e\uddf9<\/a><br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528 Swift \u8b80\u53d6 HTML \u5230 UIText&#8230;<\/p>\n","protected":false},"author":1,"featured_media":1191,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"aside","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[5],"tags":[],"class_list":["post-1190","post","type-post","status-publish","format-aside","has-post-thumbnail","hentry","category-swift","post_format-post-format-aside"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2021\/03\/IMG_1164.jpg","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-jc","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/1190","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=1190"}],"version-history":[{"count":3,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/1190\/revisions"}],"predecessor-version":[{"id":7578,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/1190\/revisions\/7578"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/1191"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=1190"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=1190"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=1190"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}