ZNSTextAttachment 1.1.9

ZNSTextAttachment 1.1.9

ZhgChgLi 维护。



  • ZhgChgLi

ZNSTextAttachment

ZNSTextAttachment 使 NSTextAttachment 能够从远程 URL 下载图片,支持 UITextView 和 UILabel。

安装

Swift Package Manager

File > Swift Packages > Add Package Dependency
Add https://github.com/ZhgChgLi/ZNSTextAttachment.git
Select "Up to Next Major" with "1.1.6"

...
dependencies: [
  .package(url: "https://github.com/ZhgChgLi/ZNSTextAttachment.git", from: "1.1.6"),
]
...
.target(
    ...
    dependencies: [
        "ZNSTextAttachment",
    ],
    ...
)

CocoaPods

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

target 'MyApp' do
  pod 'ZNSTextAttachment', '~> 1.1.6'
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

技术详情

image

文章:手工打造 HTML 解析器那些事(繁体中文)

关于

其他作品

Swift 库

  • ZMarkupParser 是一个纯 Swift 库,帮助您将 HTML 字符串转换为具有自定义样式和标签的 NSAttributedString。
  • ZPlayerCacher 是 AVAssetResourceLoaderDelegate 协议的一种轻量级实现,允许 AVPlayerItem 支持缓存流媒体文件。
  • ZNSTextAttachment 允许 NSTextAttachment 从远程 URL 下载图片,支持 UITextView 和 UILabel。

集成工具

  • ZReviewTender 是一个从 App Store 和 Google Play 控制台获取应用评论并将其集成到工作流程中的工具。
  • ZMediumToMarkdown 是一个强大的工具,允许您轻松地将您的 Medium 帖文下载并转换为 Markdown 格式。

捐赠

Buy Me A Coffe

如果您觉得这个库很有用,请考虑给它加星或向您的朋友推荐。

请随意提出问题或通过拉取请求提交修复/贡献。 :)