Skip to content

Commit

Permalink
version 3.2.0, 添加AsyncImageAttachment支持
Browse files Browse the repository at this point in the history
  • Loading branch information
lixiang1994 committed Apr 24, 2022
1 parent a24cf9e commit bff77aa
Show file tree
Hide file tree
Showing 15 changed files with 337 additions and 26 deletions.
2 changes: 1 addition & 1 deletion AttributedString.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Pod::Spec.new do |s|

s.name = "AttributedString"
s.version = "3.1.0"
s.version = "3.2.0"
s.summary = "基于Swift字符串插值快速构建你想要的富文本, 支持点击按住等事件获取, 支持多种类型过滤"

s.homepage = "https://github.com/lixiang1994/AttributedString"
Expand Down
2 changes: 2 additions & 0 deletions AttributedString.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -987,6 +987,7 @@
9B34BD3C243DBC8100932E6C /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
DEFINES_MODULE = YES;
Expand Down Expand Up @@ -1014,6 +1015,7 @@
9B34BD3D243DBC8100932E6C /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
DEFINES_MODULE = YES;
Expand Down
20 changes: 10 additions & 10 deletions Demo-Mac/Demo-Mac.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,22 @@
/* Begin PBXBuildFile section */
9B34BD6B243F16EE00932E6C /* TableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9B34BD6A243F16EE00932E6C /* TableViewCell.swift */; };
9B34BD6D243F172E00932E6C /* AllViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9B34BD6C243F172E00932E6C /* AllViewController.swift */; };
9B53DFEC28153DB300ACE7B5 /* AttributedString.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 9B34BD58243DC2F900932E6C /* AttributedString.framework */; };
9B53DFED28153DB300ACE7B5 /* AttributedString.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 9B34BD58243DC2F900932E6C /* AttributedString.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
9B6E89C123827C48009EBEBE /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9B6E89C023827C48009EBEBE /* AppDelegate.swift */; };
9B6E89C323827C48009EBEBE /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9B6E89C223827C48009EBEBE /* ViewController.swift */; };
9B6E89C523827C49009EBEBE /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 9B6E89C423827C49009EBEBE /* Assets.xcassets */; };
9B6E89C823827C49009EBEBE /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 9B6E89C623827C49009EBEBE /* Main.storyboard */; };
9BF2B27C27DA1B6900CE59D9 /* AttributedString.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 9B34BD58243DC2F900932E6C /* AttributedString.framework */; };
9BF2B27D27DA1B6900CE59D9 /* AttributedString.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 9B34BD58243DC2F900932E6C /* AttributedString.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
/* End PBXBuildFile section */

/* Begin PBXCopyFilesBuildPhase section */
9BF2B27E27DA1B6A00CE59D9 /* Embed Frameworks */ = {
9B53DFEE28153DB300ACE7B5 /* Embed Frameworks */ = {
isa = PBXCopyFilesBuildPhase;
buildActionMask = 2147483647;
dstPath = "";
dstSubfolderSpec = 10;
files = (
9BF2B27D27DA1B6900CE59D9 /* AttributedString.framework in Embed Frameworks */,
9B53DFED28153DB300ACE7B5 /* AttributedString.framework in Embed Frameworks */,
);
name = "Embed Frameworks";
runOnlyForDeploymentPostprocessing = 0;
Expand All @@ -49,7 +49,7 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
9BF2B27C27DA1B6900CE59D9 /* AttributedString.framework in Frameworks */,
9B53DFEC28153DB300ACE7B5 /* AttributedString.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -114,7 +114,7 @@
9B6E89B923827C48009EBEBE /* Sources */,
9B6E89BA23827C48009EBEBE /* Frameworks */,
9B6E89BB23827C48009EBEBE /* Resources */,
9BF2B27E27DA1B6A00CE59D9 /* Embed Frameworks */,
9B53DFEE28153DB300ACE7B5 /* Embed Frameworks */,
);
buildRules = (
);
Expand Down Expand Up @@ -314,10 +314,10 @@
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CODE_SIGN_ENTITLEMENTS = "Demo-Mac/Demo_Mac.entitlements";
CODE_SIGN_IDENTITY = "-";
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
DEVELOPMENT_TEAM = "";
DEVELOPMENT_TEAM = 8G74YECJ4Z;
ENABLE_HARDENED_RUNTIME = YES;
INFOPLIST_FILE = "Demo-Mac/Info.plist";
LD_RUNPATH_SEARCH_PATHS = (
Expand All @@ -336,10 +336,10 @@
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CODE_SIGN_ENTITLEMENTS = "Demo-Mac/Demo_Mac.entitlements";
CODE_SIGN_IDENTITY = "-";
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
DEVELOPMENT_TEAM = "";
DEVELOPMENT_TEAM = 8G74YECJ4Z;
ENABLE_HARDENED_RUNTIME = YES;
INFOPLIST_FILE = "Demo-Mac/Info.plist";
LD_RUNPATH_SEARCH_PATHS = (
Expand Down
6 changes: 3 additions & 3 deletions Demo-Mac/Demo-Mac/Assets.xcassets/Contents.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"info" : {
"version" : 1,
"author" : "xcode"
"author" : "xcode",
"version" : 1
}
}
}
7 changes: 4 additions & 3 deletions Demo-Mac/Demo-Mac/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.Storyboard.XIB" version="3.0" toolsVersion="16097" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" initialViewController="B8D-0N-5wS">
<document type="com.apple.InterfaceBuilder3.Cocoa.Storyboard.XIB" version="3.0" toolsVersion="20037" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" initialViewController="B8D-0N-5wS">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="16097"/>
<deployment identifier="macosx"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="20037"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
Expand Down Expand Up @@ -814,7 +815,7 @@
<color key="backgroundColor" name="controlBackgroundColor" catalog="System" colorSpace="catalog"/>
<color key="gridColor" name="gridColor" catalog="System" colorSpace="catalog"/>
<tableColumns>
<tableColumn width="195" minWidth="40" maxWidth="1000" id="0qa-uw-dST">
<tableColumn width="186" minWidth="40" maxWidth="1000" id="0qa-uw-dST">
<tableHeaderCell key="headerCell" lineBreakMode="truncatingTail" borderStyle="border">
<color key="textColor" name="headerTextColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="headerColor" catalog="System" colorSpace="catalog"/>
Expand Down
12 changes: 8 additions & 4 deletions Demo-Mac/Demo-Mac/Demo_Mac.entitlements
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.security.app-sandbox</key>
<true/>
<key>com.apple.security.files.user-selected.read-only</key>
<true/>
<key>com.apple.security.app-sandbox</key>
<true/>
<key>com.apple.security.files.user-selected.read-only</key>
<true/>
<key>com.apple.security.network.client</key>
<true/>
<key>com.apple.security.network.server</key>
<true/>
</dict>
</plist>
5 changes: 5 additions & 0 deletions Demo-Mac/Demo-Mac/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,10 @@
<true/>
<key>UIUserInterfaceStyle</key>
<string>Light</string>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
</dict>
</plist>
6 changes: 3 additions & 3 deletions Demo/Demo/Assets.xcassets/Contents.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"info" : {
"version" : 1,
"author" : "xcode"
"author" : "xcode",
"version" : 1
}
}
}
12 changes: 12 additions & 0 deletions Demo/Demo/Assets.xcassets/placeholder.imageset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"images" : [
{
"filename" : "placeholder.png",
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
50 changes: 50 additions & 0 deletions Demo/Demo/Details/AttachmentViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ class AttachmentViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
textView.textContainer.lineFragmentPadding = 0
textView.textContainerInset = .zero

// 网络图片链接与占位图
let url = URL(string: "https://avatars.githubusercontent.com/u/13112992?s=400&u=bb452b153d9d5342877ebd8179b04fbbae41f3d0&v=4")
let placeholder = UIImage(named: "placeholder")


// 创建一些自定义视图控件
let customView = UIView(frame: .init(x: 0, y: 0, width: 100, height: 100))
Expand Down Expand Up @@ -56,6 +63,12 @@ class AttachmentViewController: UIViewController {
-----------------------
AsyncImageAttachment only support UITextView (以下异步图片附件仅UITextView支持):
This is a remote image URL -> \(.image(url, placeholder: placeholder))
-----------------------
\("ViewAttachment only support UITextView (以下视图附件仅UITextView支持):", .font(.systemFont(ofSize: 24, weight: .medium)))
\("Change something", .foreground(.blue), .action(clicked))
Expand All @@ -71,3 +84,40 @@ class AttachmentViewController: UIViewController {
)
}
}

/*

ASAttributedString.AsyncImageAttachment.Loader = AsyncImageAttachmentKingfisherLoader.self


import Kingfisher

public class AsyncImageAttachmentKingfisherLoader: NSObject, AsyncImageAttachmentLoader {

private var downloadTask: Kingfisher.DownloadTask?

public var isLoading: Bool {
return downloadTask != nil
}

public func loadImage(with url: URL, completion: @escaping (Result<Image, Error>) -> Void) {
downloadTask = KingfisherManager.shared.retrieveImage(with: url) { [weak self] (result) in
guard let self = self else { return }
self.downloadTask = nil

switch result {
case .success(let value):
completion(.success(value.image))
case .failure(let error):
completion(.failure(error))
}
}
}

public func cancel() {
downloadTask?.cancel()
downloadTask = nil
}
}

*/
5 changes: 5 additions & 0 deletions Demo/Demo/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,10 @@
</array>
<key>UIUserInterfaceStyle</key>
<string>Light</string>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
</dict>
</plist>
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
- [x] Support for all `NSAttributedString.Key` functions.
- [x] Support iOS & macOS & watchOS & tvOS.
- [x] Support text and attachment click or press event callback, support highlight style.
- [x] Support async image attachment, you can load remote image to `UITextView`.
- [x] Support view attachment, you can add custom view to `UITextView`.
- [x] Continue to add more new features.

Expand Down Expand Up @@ -163,6 +164,27 @@ textView.attributed.text = """
"""
```

#### Attachment Async Image: (Only supports iOS: UITextView)

```swift
// ASAttributedString.AsyncImageAttachment

textView.attributed.text = """
\(.image(url, placeholder: xxxxx))
"""
```

Custom loader:

```swift
ASAttributedString.AsyncImageAttachment.Loader = AsyncImageAttachmentKingfisherLoader.self
```

Please read the demo `AttachmentViewController.swift` file for details.


#### Attachment View: (Only supports iOS: UITextView)

```swift
Expand Down
22 changes: 22 additions & 0 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
- [x] 支持全部`NSAttributedString.Key`特性.
- [x] 支持 iOS & macOS & watchOS & tvOS.
- [x] 支持文本和附件点击或长按事件回调, 支持高亮样式.
- [x] 支持异步图片附件, 可以将远程图片资源加载到`UITextView`中.
- [x] 支持视图附件, 可以将自定义视图通过富文本添加到`UITextView`中.
- [x] 更多新特性的不断加入.

Expand Down Expand Up @@ -162,6 +163,27 @@ textView.attributed.text = """
"""
```

#### 附件 异步图片: (仅支持 iOS: UITextView)

```swift
// ASAttributedString.AsyncImageAttachment

textView.attributed.text = """
\(.image(url, placeholder: xxxxx))
"""
```

自定义加载器 例如使用Kingfisher加载图片, 默认加载器使用的是URLSession:

```swift
ASAttributedString.AsyncImageAttachment.Loader = AsyncImageAttachmentKingfisherLoader.self
```

具体细节请查看Demo中`AttachmentViewController.swift`文件.


#### 附件 视图: (仅支持 iOS: UITextView)

```swift
Expand Down
Loading

0 comments on commit bff77aa

Please sign in to comment.