ZNSTextAttachment enables NSTextAttachment to download images from remote URLs, support both UITextView and UILabel.
- part of ZMarkupParser
File > Swift Packages > Add Package Dependency
Add https://github.com/ZhgChgLi/ZNSTextAttachment.git
Select "Up to Next Major" with "1.1.9"
or
...
dependencies: [
.package(url: "https://github.com/ZhgChgLi/ZNSTextAttachment.git", from: "1.1.9"),
]
...
.target(
...
dependencies: [
"ZNSTextAttachment",
],
...
)
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '12.0'
use_frameworks!
target 'MyApp' do
pod 'ZNSTextAttachment', '~> 1.1.9'
end
// ZNSTextAttachment with placeHolder Image
let attachment = ZNSTextAttachment(imageURL: URL(string: "https://zhgchg.li/assets/a5643de271e4/1*A0yXupXW9-F9ZWe4gp2ObA.jpeg")!, imageWidth: 300, placeholderImage: UIImage(systemName: "viewfinder.circle.fill")?.withTintColor(.red, renderingMode: .alwaysOriginal))
let attributedString = NSMutableAttributedString()
attributedString.append(NSAttributedString(string: "ZMarkupParser is a pure-Swift library that helps you to convert HTML strings to NSAttributedString with customized style and tags.\n"))
attributedString.append(NSAttributedString(string: "- Parse HTML strings using pure-Swift and regular expressions.\n"))
attributedString.append(NSAttributedString(attachment: attachment))
attachment.dataSource = self // if not assign, will use URLSession as default
attachment.delegate = self
// UITextView:
textView.attributedText = attributedString
// ZNSTextAttachmentLabel, auto binding
label.attributedText = attributedString
// UILabel
attachment.register(label) // need binding
label.attributedText = attributedString
Post: 手工打造 HTML 解析器的那些事(Traditional Chinese)
- ZMarkupParser is a pure-Swift library that helps you to convert HTML strings to NSAttributedString with customized style and tags.
- ZPlayerCacher is a lightweight implementation of the AVAssetResourceLoaderDelegate protocol that enables AVPlayerItem to support caching streaming files.
- ZNSTextAttachment enables NSTextAttachment to download images from remote URLs, support both UITextView and UILabel.
- ZReviewTender is a tool for fetching app reviews from the App Store and Google Play Console and integrating them into your workflow.
- ZMediumToMarkdown is a powerful tool that allows you to effortlessly download and convert your Medium posts to Markdown format.
If you find this library helpful, please consider starring the repo or recommending it to your friends.
Feel free to open an issue or submit a fix/contribution via pull request. :)