Skip to content

Commit bff77aa

Browse files
committed
version 3.2.0, 添加AsyncImageAttachment支持
1 parent a24cf9e commit bff77aa

File tree

15 files changed

+337
-26
lines changed

15 files changed

+337
-26
lines changed

AttributedString.podspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Pod::Spec.new do |s|
22

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

77
s.homepage = "https://github.com/lixiang1994/AttributedString"

AttributedString.xcodeproj/project.pbxproj

+2
Original file line numberDiff line numberDiff line change
@@ -987,6 +987,7 @@
987987
9B34BD3C243DBC8100932E6C /* Debug */ = {
988988
isa = XCBuildConfiguration;
989989
buildSettings = {
990+
CODE_SIGN_IDENTITY = "Apple Development";
990991
CODE_SIGN_STYLE = Automatic;
991992
COMBINE_HIDPI_IMAGES = YES;
992993
DEFINES_MODULE = YES;
@@ -1014,6 +1015,7 @@
10141015
9B34BD3D243DBC8100932E6C /* Release */ = {
10151016
isa = XCBuildConfiguration;
10161017
buildSettings = {
1018+
CODE_SIGN_IDENTITY = "Apple Development";
10171019
CODE_SIGN_STYLE = Automatic;
10181020
COMBINE_HIDPI_IMAGES = YES;
10191021
DEFINES_MODULE = YES;

Demo-Mac/Demo-Mac.xcodeproj/project.pbxproj

+10-10
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,22 @@
99
/* Begin PBXBuildFile section */
1010
9B34BD6B243F16EE00932E6C /* TableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9B34BD6A243F16EE00932E6C /* TableViewCell.swift */; };
1111
9B34BD6D243F172E00932E6C /* AllViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9B34BD6C243F172E00932E6C /* AllViewController.swift */; };
12+
9B53DFEC28153DB300ACE7B5 /* AttributedString.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 9B34BD58243DC2F900932E6C /* AttributedString.framework */; };
13+
9B53DFED28153DB300ACE7B5 /* AttributedString.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 9B34BD58243DC2F900932E6C /* AttributedString.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
1214
9B6E89C123827C48009EBEBE /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9B6E89C023827C48009EBEBE /* AppDelegate.swift */; };
1315
9B6E89C323827C48009EBEBE /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9B6E89C223827C48009EBEBE /* ViewController.swift */; };
1416
9B6E89C523827C49009EBEBE /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 9B6E89C423827C49009EBEBE /* Assets.xcassets */; };
1517
9B6E89C823827C49009EBEBE /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 9B6E89C623827C49009EBEBE /* Main.storyboard */; };
16-
9BF2B27C27DA1B6900CE59D9 /* AttributedString.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 9B34BD58243DC2F900932E6C /* AttributedString.framework */; };
17-
9BF2B27D27DA1B6900CE59D9 /* AttributedString.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 9B34BD58243DC2F900932E6C /* AttributedString.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
1818
/* End PBXBuildFile section */
1919

2020
/* Begin PBXCopyFilesBuildPhase section */
21-
9BF2B27E27DA1B6A00CE59D9 /* Embed Frameworks */ = {
21+
9B53DFEE28153DB300ACE7B5 /* Embed Frameworks */ = {
2222
isa = PBXCopyFilesBuildPhase;
2323
buildActionMask = 2147483647;
2424
dstPath = "";
2525
dstSubfolderSpec = 10;
2626
files = (
27-
9BF2B27D27DA1B6900CE59D9 /* AttributedString.framework in Embed Frameworks */,
27+
9B53DFED28153DB300ACE7B5 /* AttributedString.framework in Embed Frameworks */,
2828
);
2929
name = "Embed Frameworks";
3030
runOnlyForDeploymentPostprocessing = 0;
@@ -49,7 +49,7 @@
4949
isa = PBXFrameworksBuildPhase;
5050
buildActionMask = 2147483647;
5151
files = (
52-
9BF2B27C27DA1B6900CE59D9 /* AttributedString.framework in Frameworks */,
52+
9B53DFEC28153DB300ACE7B5 /* AttributedString.framework in Frameworks */,
5353
);
5454
runOnlyForDeploymentPostprocessing = 0;
5555
};
@@ -114,7 +114,7 @@
114114
9B6E89B923827C48009EBEBE /* Sources */,
115115
9B6E89BA23827C48009EBEBE /* Frameworks */,
116116
9B6E89BB23827C48009EBEBE /* Resources */,
117-
9BF2B27E27DA1B6A00CE59D9 /* Embed Frameworks */,
117+
9B53DFEE28153DB300ACE7B5 /* Embed Frameworks */,
118118
);
119119
buildRules = (
120120
);
@@ -314,10 +314,10 @@
314314
buildSettings = {
315315
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
316316
CODE_SIGN_ENTITLEMENTS = "Demo-Mac/Demo_Mac.entitlements";
317-
CODE_SIGN_IDENTITY = "-";
317+
CODE_SIGN_IDENTITY = "Apple Development";
318318
CODE_SIGN_STYLE = Automatic;
319319
COMBINE_HIDPI_IMAGES = YES;
320-
DEVELOPMENT_TEAM = "";
320+
DEVELOPMENT_TEAM = 8G74YECJ4Z;
321321
ENABLE_HARDENED_RUNTIME = YES;
322322
INFOPLIST_FILE = "Demo-Mac/Info.plist";
323323
LD_RUNPATH_SEARCH_PATHS = (
@@ -336,10 +336,10 @@
336336
buildSettings = {
337337
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
338338
CODE_SIGN_ENTITLEMENTS = "Demo-Mac/Demo_Mac.entitlements";
339-
CODE_SIGN_IDENTITY = "-";
339+
CODE_SIGN_IDENTITY = "Apple Development";
340340
CODE_SIGN_STYLE = Automatic;
341341
COMBINE_HIDPI_IMAGES = YES;
342-
DEVELOPMENT_TEAM = "";
342+
DEVELOPMENT_TEAM = 8G74YECJ4Z;
343343
ENABLE_HARDENED_RUNTIME = YES;
344344
INFOPLIST_FILE = "Demo-Mac/Info.plist";
345345
LD_RUNPATH_SEARCH_PATHS = (
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"info" : {
3-
"version" : 1,
4-
"author" : "xcode"
3+
"author" : "xcode",
4+
"version" : 1
55
}
6-
}
6+
}

Demo-Mac/Demo-Mac/Base.lproj/Main.storyboard

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<document type="com.apple.InterfaceBuilder3.Cocoa.Storyboard.XIB" version="3.0" toolsVersion="16097" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" initialViewController="B8D-0N-5wS">
2+
<document type="com.apple.InterfaceBuilder3.Cocoa.Storyboard.XIB" version="3.0" toolsVersion="20037" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" initialViewController="B8D-0N-5wS">
33
<dependencies>
4-
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="16097"/>
4+
<deployment identifier="macosx"/>
5+
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="20037"/>
56
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
67
</dependencies>
78
<scenes>
@@ -814,7 +815,7 @@
814815
<color key="backgroundColor" name="controlBackgroundColor" catalog="System" colorSpace="catalog"/>
815816
<color key="gridColor" name="gridColor" catalog="System" colorSpace="catalog"/>
816817
<tableColumns>
817-
<tableColumn width="195" minWidth="40" maxWidth="1000" id="0qa-uw-dST">
818+
<tableColumn width="186" minWidth="40" maxWidth="1000" id="0qa-uw-dST">
818819
<tableHeaderCell key="headerCell" lineBreakMode="truncatingTail" borderStyle="border">
819820
<color key="textColor" name="headerTextColor" catalog="System" colorSpace="catalog"/>
820821
<color key="backgroundColor" name="headerColor" catalog="System" colorSpace="catalog"/>

Demo-Mac/Demo-Mac/Demo_Mac.entitlements

+8-4
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
33
<plist version="1.0">
44
<dict>
5-
<key>com.apple.security.app-sandbox</key>
6-
<true/>
7-
<key>com.apple.security.files.user-selected.read-only</key>
8-
<true/>
5+
<key>com.apple.security.app-sandbox</key>
6+
<true/>
7+
<key>com.apple.security.files.user-selected.read-only</key>
8+
<true/>
9+
<key>com.apple.security.network.client</key>
10+
<true/>
11+
<key>com.apple.security.network.server</key>
12+
<true/>
913
</dict>
1014
</plist>

Demo-Mac/Demo-Mac/Info.plist

+5
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,10 @@
3434
<true/>
3535
<key>UIUserInterfaceStyle</key>
3636
<string>Light</string>
37+
<key>NSAppTransportSecurity</key>
38+
<dict>
39+
<key>NSAllowsArbitraryLoads</key>
40+
<true/>
41+
</dict>
3742
</dict>
3843
</plist>
+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"info" : {
3-
"version" : 1,
4-
"author" : "xcode"
3+
"author" : "xcode",
4+
"version" : 1
55
}
6-
}
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"images" : [
3+
{
4+
"filename" : "placeholder.png",
5+
"idiom" : "universal"
6+
}
7+
],
8+
"info" : {
9+
"author" : "xcode",
10+
"version" : 1
11+
}
12+
}
Loading

Demo/Demo/Details/AttachmentViewController.swift

+50
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ class AttachmentViewController: UIViewController {
1515

1616
override func viewDidLoad() {
1717
super.viewDidLoad()
18+
textView.textContainer.lineFragmentPadding = 0
19+
textView.textContainerInset = .zero
20+
21+
// 网络图片链接与占位图
22+
let url = URL(string: "https://avatars.githubusercontent.com/u/13112992?s=400&u=bb452b153d9d5342877ebd8179b04fbbae41f3d0&v=4")
23+
let placeholder = UIImage(named: "placeholder")
24+
1825

1926
// 创建一些自定义视图控件
2027
let customView = UIView(frame: .init(x: 0, y: 0, width: 100, height: 100))
@@ -56,6 +63,12 @@ class AttachmentViewController: UIViewController {
5663
5764
-----------------------
5865
66+
AsyncImageAttachment only support UITextView (以下异步图片附件仅UITextView支持):
67+
68+
This is a remote image URL -> \(.image(url, placeholder: placeholder))
69+
70+
-----------------------
71+
5972
\("ViewAttachment only support UITextView (以下视图附件仅UITextView支持):", .font(.systemFont(ofSize: 24, weight: .medium)))
6073
6174
\("Change something", .foreground(.blue), .action(clicked))
@@ -71,3 +84,40 @@ class AttachmentViewController: UIViewController {
7184
)
7285
}
7386
}
87+
88+
/*
89+
90+
ASAttributedString.AsyncImageAttachment.Loader = AsyncImageAttachmentKingfisherLoader.self
91+
92+
93+
import Kingfisher
94+
95+
public class AsyncImageAttachmentKingfisherLoader: NSObject, AsyncImageAttachmentLoader {
96+
97+
private var downloadTask: Kingfisher.DownloadTask?
98+
99+
public var isLoading: Bool {
100+
return downloadTask != nil
101+
}
102+
103+
public func loadImage(with url: URL, completion: @escaping (Result<Image, Error>) -> Void) {
104+
downloadTask = KingfisherManager.shared.retrieveImage(with: url) { [weak self] (result) in
105+
guard let self = self else { return }
106+
self.downloadTask = nil
107+
108+
switch result {
109+
case .success(let value):
110+
completion(.success(value.image))
111+
case .failure(let error):
112+
completion(.failure(error))
113+
}
114+
}
115+
}
116+
117+
public func cancel() {
118+
downloadTask?.cancel()
119+
downloadTask = nil
120+
}
121+
}
122+
123+
*/

Demo/Demo/Info.plist

+5
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,10 @@
4343
</array>
4444
<key>UIUserInterfaceStyle</key>
4545
<string>Light</string>
46+
<key>NSAppTransportSecurity</key>
47+
<dict>
48+
<key>NSAllowsArbitraryLoads</key>
49+
<true/>
50+
</dict>
4651
</dict>
4752
</plist>

README.md

+22
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
- [x] Support for all `NSAttributedString.Key` functions.
2020
- [x] Support iOS & macOS & watchOS & tvOS.
2121
- [x] Support text and attachment click or press event callback, support highlight style.
22+
- [x] Support async image attachment, you can load remote image to `UITextView`.
2223
- [x] Support view attachment, you can add custom view to `UITextView`.
2324
- [x] Continue to add more new features.
2425

@@ -163,6 +164,27 @@ textView.attributed.text = """
163164
"""
164165
```
165166

167+
#### Attachment Async Image: (Only supports iOS: UITextView)
168+
169+
```swift
170+
// ASAttributedString.AsyncImageAttachment
171+
172+
textView.attributed.text = """
173+
174+
\(.image(url, placeholder: xxxxx))
175+
176+
"""
177+
```
178+
179+
Custom loader:
180+
181+
```swift
182+
ASAttributedString.AsyncImageAttachment.Loader = AsyncImageAttachmentKingfisherLoader.self
183+
```
184+
185+
Please read the demo `AttachmentViewController.swift` file for details.
186+
187+
166188
#### Attachment View: (Only supports iOS: UITextView)
167189

168190
```swift

README_CN.md

+22
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
- [x] 支持全部`NSAttributedString.Key`特性.
1818
- [x] 支持 iOS & macOS & watchOS & tvOS.
1919
- [x] 支持文本和附件点击或长按事件回调, 支持高亮样式.
20+
- [x] 支持异步图片附件, 可以将远程图片资源加载到`UITextView`中.
2021
- [x] 支持视图附件, 可以将自定义视图通过富文本添加到`UITextView`中.
2122
- [x] 更多新特性的不断加入.
2223

@@ -162,6 +163,27 @@ textView.attributed.text = """
162163
"""
163164
```
164165

166+
#### 附件 异步图片: (仅支持 iOS: UITextView)
167+
168+
```swift
169+
// ASAttributedString.AsyncImageAttachment
170+
171+
textView.attributed.text = """
172+
173+
\(.image(url, placeholder: xxxxx))
174+
175+
"""
176+
```
177+
178+
自定义加载器 例如使用Kingfisher加载图片, 默认加载器使用的是URLSession:
179+
180+
```swift
181+
ASAttributedString.AsyncImageAttachment.Loader = AsyncImageAttachmentKingfisherLoader.self
182+
```
183+
184+
具体细节请查看Demo中`AttachmentViewController.swift`文件.
185+
186+
165187
#### 附件 视图: (仅支持 iOS: UITextView)
166188

167189
```swift

0 commit comments

Comments
 (0)