Skip to content

ZhgChgLi/ZNSTextAttachment

Repository files navigation

ZNSTextAttachment

ZNSTextAttachment enables NSTextAttachment to download images from remote URLs, support both UITextView and UILabel.

Installation

Swift Package Manager

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",
    ],
    ...
)

CocoaPods

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '12.0'
use_frameworks!

target 'MyApp' do
  pod 'ZNSTextAttachment', '~> 1.1.9'
end

Usage

// 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

Technical Detail

image

Post: 手工打造 HTML 解析器的那些事(Traditional Chinese)

About

Other works

Swift Libraries

  • 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.

Integration Tools

  • 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.
  • linkyee is a fully customized, open-source LinkTree alternative deployed directly on GitHub Pages.

Donate

Buy Me A Coffe

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. :)