Skip to content

Commit

Permalink
重构Action, 支持多Action设置以及点击长按共存.
Browse files Browse the repository at this point in the history
  • Loading branch information
lixiang1994 committed Mar 10, 2022
1 parent 51173fa commit aca8827
Show file tree
Hide file tree
Showing 11 changed files with 380 additions and 223 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 = "2.2.2"
s.version = "3.0.0"
s.summary = "基于Swift字符串插值快速构建你想要的富文本, 支持点击按住等事件获取, 支持多种类型过滤"

s.homepage = "https://github.com/lixiang1994/AttributedString"
Expand Down
12 changes: 6 additions & 6 deletions AttributedString.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -833,7 +833,7 @@
APPLICATION_EXTENSION_API_ONLY = YES;
CODE_SIGN_STYLE = Automatic;
DEFINES_MODULE = YES;
DEVELOPMENT_TEAM = B9D8DJR5J5;
DEVELOPMENT_TEAM = "";
DYLIB_COMPATIBILITY_VERSION = 1;
DYLIB_CURRENT_VERSION = 1;
DYLIB_INSTALL_NAME_BASE = "@rpath";
Expand Down Expand Up @@ -861,7 +861,7 @@
APPLICATION_EXTENSION_API_ONLY = YES;
CODE_SIGN_STYLE = Automatic;
DEFINES_MODULE = YES;
DEVELOPMENT_TEAM = B9D8DJR5J5;
DEVELOPMENT_TEAM = "";
DYLIB_COMPATIBILITY_VERSION = 1;
DYLIB_CURRENT_VERSION = 1;
DYLIB_INSTALL_NAME_BASE = "@rpath";
Expand All @@ -888,7 +888,7 @@
buildSettings = {
CODE_SIGN_STYLE = Automatic;
DEFINES_MODULE = YES;
DEVELOPMENT_TEAM = B9D8DJR5J5;
DEVELOPMENT_TEAM = "";
DYLIB_COMPATIBILITY_VERSION = 1;
DYLIB_CURRENT_VERSION = 1;
DYLIB_INSTALL_NAME_BASE = "@rpath";
Expand All @@ -915,7 +915,7 @@
buildSettings = {
CODE_SIGN_STYLE = Automatic;
DEFINES_MODULE = YES;
DEVELOPMENT_TEAM = B9D8DJR5J5;
DEVELOPMENT_TEAM = "";
DYLIB_COMPATIBILITY_VERSION = 1;
DYLIB_CURRENT_VERSION = 1;
DYLIB_INSTALL_NAME_BASE = "@rpath";
Expand Down Expand Up @@ -943,7 +943,7 @@
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_STYLE = Automatic;
DEVELOPMENT_TEAM = B9D8DJR5J5;
DEVELOPMENT_TEAM = "";
INFOPLIST_FILE = Tests/Info.plist;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
Expand All @@ -967,7 +967,7 @@
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_STYLE = Automatic;
DEVELOPMENT_TEAM = B9D8DJR5J5;
DEVELOPMENT_TEAM = "";
INFOPLIST_FILE = Tests/Info.plist;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
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 @@ -7,24 +7,24 @@
objects = {

/* Begin PBXBuildFile section */
9B34BD5C243DC33500932E6C /* AttributedString.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 9B34BD58243DC2F900932E6C /* AttributedString.framework */; };
9B34BD5D243DC33500932E6C /* AttributedString.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 9B34BD58243DC2F900932E6C /* AttributedString.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
9B34BD6B243F16EE00932E6C /* TableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9B34BD6A243F16EE00932E6C /* TableViewCell.swift */; };
9B34BD6D243F172E00932E6C /* AllViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9B34BD6C243F172E00932E6C /* AllViewController.swift */; };
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 */
9B34BD5E243DC33500932E6C /* Embed Frameworks */ = {
9BF2B27E27DA1B6A00CE59D9 /* Embed Frameworks */ = {
isa = PBXCopyFilesBuildPhase;
buildActionMask = 2147483647;
dstPath = "";
dstSubfolderSpec = 10;
files = (
9B34BD5D243DC33500932E6C /* AttributedString.framework in Embed Frameworks */,
9BF2B27D27DA1B6900CE59D9 /* AttributedString.framework in Embed Frameworks */,
);
name = "Embed Frameworks";
runOnlyForDeploymentPostprocessing = 0;
Expand All @@ -49,7 +49,7 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
9B34BD5C243DC33500932E6C /* AttributedString.framework in Frameworks */,
9BF2B27C27DA1B6900CE59D9 /* AttributedString.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -114,7 +114,7 @@
9B6E89B923827C48009EBEBE /* Sources */,
9B6E89BA23827C48009EBEBE /* Frameworks */,
9B6E89BB23827C48009EBEBE /* Resources */,
9B34BD5E243DC33500932E6C /* Embed Frameworks */,
9BF2B27E27DA1B6A00CE59D9 /* 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 = "Apple Development";
CODE_SIGN_IDENTITY = "-";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
DEVELOPMENT_TEAM = B9D8DJR5J5;
DEVELOPMENT_TEAM = "";
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 = "Apple Development";
CODE_SIGN_IDENTITY = "-";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
DEVELOPMENT_TEAM = B9D8DJR5J5;
DEVELOPMENT_TEAM = "";
ENABLE_HARDENED_RUNTIME = YES;
INFOPLIST_FILE = "Demo-Mac/Info.plist";
LD_RUNPATH_SEARCH_PATHS = (
Expand Down
2 changes: 1 addition & 1 deletion Demo/Demo/Details/ActionViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class ActionViewController: UIViewController {
}

label.attributed.text = """
This is \("Label", .font(.systemFont(ofSize: 50)), .action(clicked))
This is \("Label", .font(.systemFont(ofSize: 50)), .action(clicked), .action(.press, pressed))
This is a picture -> \(.image(#imageLiteral(resourceName: "huaji"), .custom(size: .init(width: 100, height: 100))), action: clicked) -> Displayed in custom size.
Expand Down
5 changes: 2 additions & 3 deletions Demo/Demo/Details/CheckingViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ class CheckingViewController: ViewController<CheckingView> {
super.viewDidLoad()

// 添加电话号码类型监听
container.label.attributed.observe([.phoneNumber], highlights: [.foreground(#colorLiteral(red: 0.4745098054, green: 0.8392156959, blue: 0.9764705896, alpha: 1))]) { (result) in
container.label.attributed.observe(.phoneNumber, with: .init(.click, highlights: [.foreground(#colorLiteral(red: 0.4745098054, green: 0.8392156959, blue: 0.9764705896, alpha: 1))], with: { (result) in
print(result)
}
}))
// 添加默认类型监听
container.textView.attributed.observe(highlights: [.foreground(#colorLiteral(red: 0.5568627715, green: 0.3529411852, blue: 0.9686274529, alpha: 1))]) { (result) in
print(result)
Expand All @@ -42,7 +42,6 @@ class CheckingViewController: ViewController<CheckingView> {
string.add(attributes: [.foreground(#colorLiteral(red: 0.9529411793, green: 0.6862745285, blue: 0.1333333403, alpha: 1)), .font(.systemFont(ofSize: 20, weight: .medium))], checkings: [.phoneNumber])
string.add(attributes: [.foreground(#colorLiteral(red: 0.1764705926, green: 0.4980392158, blue: 0.7568627596, alpha: 1)), .font(.systemFont(ofSize: 20, weight: .medium))], checkings: [.link])
string.add(attributes: [.foreground(#colorLiteral(red: 0.1764705926, green: 0.01176470611, blue: 0.5607843399, alpha: 1)), .font(.systemFont(ofSize: 20, weight: .medium))], checkings: [.date])
string.add(attributes: [.font(.systemFont(ofSize: 20, weight: .medium))], checkings: [.action])
container.label.attributed.text = string
}

Expand Down
8 changes: 5 additions & 3 deletions Sources/Action.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ extension ASAttributedString {
/// 触发回调
let callback: (Result) -> Void

/// 内部处理
/// 内部使用
internal var isExternal: Bool = true
internal var handle: (() -> Void)?

public init(_ trigger: Trigger = .click, highlights: [Highlight] = .defalut, with callback: @escaping (Result) -> Void) {
Expand All @@ -45,10 +46,11 @@ extension ASAttributedString {
self.callback = callback
}

init(_ trigger: Trigger = .click, highlights: [Highlight] = .defalut) {
internal init(_ trigger: Trigger, _ highlights: [Highlight], _ callback: @escaping (Result) -> Void) {
self.trigger = trigger
self.highlights = highlights
self.callback = { _ in }
self.callback = callback
self.isExternal = false
}
}
}
Expand Down
16 changes: 16 additions & 0 deletions Sources/AttributedString.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,22 @@ public struct ASAttributedString {
return
}

#if os(iOS) || os(macOS)
// 合并多个Action
var attributes = attributes

let actions = attributes.compactMap {
$0.attributes[.action] as? Attribute.Action
}
if !actions.isEmpty {
attributes.removeAll(where: {
$0.attributes.keys.contains(.action)
})
attributes.append(.init(attributes: [.action: actions]))
}

#endif

// 获取通用属性
var temp: [NSAttributedString.Key: Any] = [:]
attributes.forEach { temp.merge($0.attributes, uniquingKeysWith: { $1 }) }
Expand Down
38 changes: 32 additions & 6 deletions Sources/Checking.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ extension ASAttributedString.Checking {
/// 正则表达式
case regex(NSAttributedString)
#if os(iOS) || os(macOS)
case action(ASAttributedString.Action.Result.Content)
case action([ASAttributedString.Action])
#endif
#if !os(watchOS)
case attachment(NSTextAttachment)
Expand All @@ -61,6 +61,31 @@ extension ASAttributedString.Checking {
}
}

#if os(iOS) || os(macOS)

extension ASAttributedString.Checking {

public struct Action {
public typealias Trigger = ASAttributedString.Action.Trigger
public typealias Highlight = ASAttributedString.Action.Highlight

/// 触发类型
let trigger: Trigger
/// 高亮属性
let highlights: [Highlight]
/// 触发回调
let callback: (Result) -> Void

public init(_ trigger: Trigger = .click, highlights: [Highlight] = .defalut, with callback: @escaping (Result) -> Void) {
self.trigger = trigger
self.highlights = highlights
self.callback = callback
}
}
}

#endif

extension ASAttributedString.Checking.Result {

public struct Date {
Expand Down Expand Up @@ -168,15 +193,16 @@ extension ASAttributedString {
let substring = value.attributedSubstring(from: match.range)
result[match.range] = (checking, .regex(substring))
}

#if os(iOS) || os(macOS)
case .action:
let actions: [NSRange: ASAttributedString.Action] = value.get(.action)
for action in actions where !contains(action.key) {
result[action.key] = (.action, .action(value.get(action.key).content))
let ranges: [NSRange: [Action]] = value.get(.action)
for range in ranges where !contains(range.key) {
let actions = range.value.filter({ $0.isExternal })
result[range.key] = (.action, .action(actions))
}
#endif

#if !os(watchOS)
case .attachment:
let attachments: [NSRange: NSTextAttachment] = value.get(.attachment)
Expand Down
Loading

0 comments on commit aca8827

Please sign in to comment.