HTMLTextConverter
一个简单的转换器类,它接收包含 HTML 元素文本的字符串,例如 'href' 字符串和标签
,并返回带转换元素的属性字符串。例如,'href' 字符串将被转换为在相应位置具有链接的字符串。转换器使用 HTML 封装器模板并在文档中插入文本。转换器接受针对每个文本和链接样式的 NSAttributes。
转换器接收 NSAttributes,将它们转换为 CSS-样式,并将这些样式插入到 HTML-封装器中相应的占位符 "$text-style$"(用于常规文本样式)和 "$link-style$"(用于链接样式)中。
此外,转换器还接受自定义的 HTML-封装器。只需要提供封装器的 URL。转换器在 HTML-封装器中使用 "$text-content$" 作为占位符。这是在 HTML-封装器中插入文本输入的位置。因此,自定义封装器需要在 HTML 文档的主体中使用此占位符。对于样式,占位符是 "$text-style$" 用于文本样式和 "$link-style$" 用于链接样式。自定义封装器不需要使用这些占位符,但它可以选择使用。
安装
通过在 Podfile 中添加以下内容来通过 Cocoapods 进行安装:
pod 'APLHrefStringConverter', '1.0.7'
用法
import APLHrefStringConverter
像这样使用HTMLTextConverter
let testString = "A string with <a href=\"http://www.apploft.de\">a link</a>"
let linkTextAttributes: [NSAttributedString.Key: Any] = [
NSAttributedString.Key.underlineStyle: NSUnderlineStyle.single.rawValue,
NSAttributedString.Key.foregroundColor: UIColor.white,
NSAttributedString.Key.underlineColor: UIColor.white,
NSAttributedString.Key.font: UIFont.systemFont(ofSize: 18)
]
let textAttributes: [NSAttributedString.Key: Any] = [NSAttributedString.Key.foregroundColor: UIColor.brown]
let htmlFormattedString = HTMLTextConverter.convert(testString, linkAttributes: linkTextAttributes, stringAttributes: textAttributes)
使用HTMLTextConverter与UITextView的示例
UITextView有一个属性可以自动检测链接。因此,与这个库结合使用UITextView非常有用。为了使该功能正常工作,textView的属性property isEditable需要设置为false。此外,UITextView提供了一个属性linkTextAttributes,用于在attributedString中设置链接的样式。这将会覆盖转换后的链接样式。因此,当使用UITextView时,要么使用该属性来设置链接样式,要么将链接属性设置为[:]
textView.attributedText = htmlFormattedString
textView.linkTextAttributes = [:]
textView.delegate = self
textview.dataDetectorTypes
textView.isEditable = false
使用HTMLTextConverter与UILabel的示例
UILabel无法自动检测链接,但仍然可以使用此转换器显示和格式化html标签的文本。警告:尽管UILabel无法自动检测链接,但使用HTMLTextConverter时,仍然可以通过linkAttributes显示和格式化链接。这可能会导致在UI上出现错误。(示例在样例项目中)
label.attributedText = htmlFormattedString
使用HTMLTextConverter与HTML-Wrapper的示例
HTML-Custom-Wrapper.html
<!DOCTYPE html>
<html>
<head>
<style>
body {
font-family: 'Droid Sans';
font-style: normal;
font-weight: 400;
margin: 50px;
}
h1 {
font-size: 35px;
font-weight: normal;
margin-top: 5px;
text-align: center;
}
li {
background-color: #3aa5c0;
}
</style>
</head>
<body>
<div class="Text">
$text-content$
</div>
</body>
</html>
用法
let htmlWrapperLabel = UILabel()
let string = "<h1>Ihr Partner für Erfolg in der App Welt!</h1><br><li>apploft</li>ist eine Tech Agentur der mobilen Welt, die 2008 als Ausgründung aus Apple gestartet ist. Heute begleiten wir viele Kunden auf dem Weg zum Erfolg. Unsere Kernbereiche sind dabei iOS, Android, tvOS, Android TV, AR, VR, Bots und Skills und unser Leistungsportfolio erstreckt sich von Strategieberatung, Design Thinking, Agile Coaching, UX, UI-Design, über die Software-Entwicklung bis hin zum Aufbau und Betrieb Cloud basierter Backend-Services."
//loading the htmlWrapper file
let customHTMLWrapper = Bundle.main.url(forResource: "HTML-Custom-Wrapper", withExtension: ".html")
htmlWrapperLabel.attributedText = HTMLTextConverter.convert(string, htmlWrapperURL: customHTMLWrapper, encoding: .isoLatin1)
APLHrefStringConverter(已过时)
一个简单的转换类,它接受包含 'hrefs' 的字符串作为输入,并返回带有相应位置链接的富文本字符串。
安装
通过在 Podfile 中添加以下内容来通过 Cocoapods 进行安装:
pod 'APLHrefStringConverter', '0.0.4'
使用方法
导入头文件
#import "APLHrefStringConverter.h"
使用 APLHrefStringConverter 的方式如下
...APLHrefStringConverter
self.textView.delegate = self;
...
NSString *testString = @"A string with <a href=\"http://www.apploft.de\">a link</a>";
NSMutableAttributedString *mutableStringWithHref = [APLHrefStringConverter convert:testString];
self.textView.attributedString = mutableStringWithHref;
self.textView.dataDetectorTypes = UIDataDetectorTypeLink;
...
// Implement the delegate method extending the UITextViewDelegate method
// in order to be informed about URLs being touched by the user
-(void)textView:(APLUrlTextView *)label didSelectLinkWithURL:(NSURL *)url {
...
}
Swift 版本
import APLHrefStringConverter
self.textView.delegate = self
let testString = "A string with <a href=\"http://www.apploft.de\">a link</a>"
let stringWithLink = APLHrefStringConverter.convert(testString, linkAttributes: linkTextAttributes, stringAttributes: attributes)
textView.linkTextAttributes = [:]