Skip to content

Releases: lixiang1994/AttributedString

增加文本检查特性, 控件增加监听不同类型的点击事件支持, 优化API

04 Jul 06:58
f89b3ed
Compare
Choose a tag to compare

New Version ヾ(@^▽^@)ノ 💥

特性:

增加文本检查特性, 可过滤匹配指定类型的内容, 例如: Action类型, 正则表达式类型, NSDataDetector自带的类型等.

  • UILabel & UITextView & NSTextField 原支持Action的控件 增加了监听方法, 可监听某一个或多个类型的点击事件.

  • 为某一个或多个类型 添加新的属性 或 为某一个或多个类型 覆盖原有属性.

应用:

  • 例如为整段文本的电话号码类型增加点击事件获取 跳转拨号.

  • 例如在View的tintAdjustmentMode.dimmed时, 设置Action类型的颜色转为灰色.

  • 例如为整段文本添加一个正则表达式类型 为符合表达式的内容增加高亮的颜色.

更多细节可参考README 和 Demo, 如果你有更好的想法 可以Issues留言.


API

Changed:

AttributedString:

old:

init(_: String)

new:

init(string: String)

AttributedString.Attribute:

old:

func color(_: Color)

new:

func foreground(_: Color)

Add:

Class:

AttributedString.Checking:

extension AttributedString {
        
    public enum Checking: Hashable {
        /// 自定义范围
        case range(NSRange)
        /// 正则表达式
        case regex(String)
        #if os(iOS) || os(macOS)
        case action
        #endif
        ///
        case date
        case link
        case address
        case phoneNumber
        case transitInformation
    }
}

AttributedString.Checking.Result:

extension AttributedString.Checking {
    
    public enum Result {
        /// 自定义范围
        case range(NSAttributedString)
        /// 正则表达式
        case regex(NSAttributedString)
        #if os(iOS) || os(macOS)
        case action(AttributedString.Action.Result)
        #endif
        
        case date(Date)
        case link(URL)
        case address(Address)
        case phoneNumber(String)
        case transitInformation(TransitInformation)
    }
}

Founction:

AttributedString:

init(string: String, _: Attribute...)

init(string: String, with: [Attribute])
func isContentEqual(_: AttributedString?)
mutating func add(attributes: [Attribute], range: NSRange)

mutating func set(attributes: [Attribute], range: NSRange)
mutating func add(attributes: [Attribute], checkings: [Checking])

mutating func set(attributes: [Attribute], checkings: [Checking])

UILabel & UITextView & NSTextField:

func observe(_ checking: Checking, highlights: [Highlight], with: @escaping (Checking.Result) -> Void)

func observe(_ checkings: [Checking], highlights: [Highlight], with: @escaping (Checking.Result) -> Void)
func remove(checking: Checking)

func remove(checkings: [Checking])

Remove:

AttributedString:

init<T>(_ value: T)

修复UILabel 点击范围计算错误的问题

22 Jun 06:59
Compare
Choose a tag to compare
1.4.1

发布1.4.1, 修复UILabel 点击范围计算错误的问题

Action 增加触发方式, 高亮样式设置, 支持单击或按住

20 Jun 06:10
Compare
Choose a tag to compare
  • 修复 UILabel 点击位置错误问题
  • Action 增加触发方式, 高亮样式设置, 支持单击或按住
点击:
// 文本
let a: AttributedString = .init("lee", .action({  }))
// 附件 (图片)
let b: AttributedString = .init(.image(image), action: {
    // code
})

// 建议使用函数作为参数 语法上比直接使用闭包更加整洁.
func clicked() {
    // code
}
// 正常初始化
let c: AttributedString = .init("lee", .action(clicked))
let d: AttributedString = .init(.image(image), action: clicked)
// 字面量初始化
let e: AttributedString = "\("lee", .action(clicked))"
let f: AttributedString = "\(.image(image), action: clicked)"

// 获取更多信息 
func clicked(_ result: AttributedString.Action.Result) {
    switch result.content {
    case .string(let value):
        print("点击了文本: \(value) range: \(result.range)")
                
    case .attachment(let value):
        print("点击了附件: \(value) range: \(result.range)")
    }
}

label.attributed.text = "This is \("Label", .font(.systemFont(ofSize: 20)), .action(clicked))"
textView.attributed.text = "This is a picture \(.image(image, .custom(size: .init(width: 100, height: 100))), action: clicked) Displayed in custom size."
按住:
func pressed(_ result: AttributedString.Action.Result) {
    switch result.content {
    case .string(let value):
        print("按住了文本: \(value) range: \(result.range)")
                
    case .attachment(let value):
        print("按住了附件: \(value) range: \(result.range)")
    }
}

label.attributed.text = "This is \("Long Press", .font(.systemFont(ofSize: 20)), .action(.press, pressed))"
textView.attributed.text = "This is a picture \(.image(image, .custom(size: .init(width: 100, height: 100))), trigger: .press, action: pressed) Displayed in custom size."
高亮样式:
func clicked(_ result: AttributedString.Action.Result) {
    switch result.content {
    case .string(let value):
        print("点击了文本: \(value) range: \(result.range)")
                
    case .attachment(let value):
        print("点击了附件: \(value) range: \(result.range)")
    }
}

label.attributed.text = "This is \("Label", .font(.systemFont(ofSize: 20)), .action([.color(.blue)], clicked))"
自定义:
// 触发方式为 按住, 高亮样式为 蓝色背景色和白色文字
let custom = AttributedString.Action(.press, highlights: [.background(.blue), .color(.white)]) { (result) in
    switch result.content {
    case .string(let value):
        print("按住了文本: \(value) range: \(result.range)")
        
    case .attachment(let value):
        print("按住了附件: \(value) range: \(result.range)")
    }
}

label.attributed.text = "This is \("Custom", .font(.systemFont(ofSize: 20)), .action(custom))"
textView.attributed.text = "This is a picture \(.image(image, .original(.center)), action: custom) Displayed in original size."

增加SPM支持

15 Jun 04:41
Compare
Choose a tag to compare

Swift Package Manager for Apple platforms

Select Xcode menu File > Swift Packages > Add Package Dependency and enter repository URL with GUI.

Repository: https://github.com/lixiang1994/AttributedString

Swift Package Manager

Add the following to the dependencies of your Package.swift:

.package(url: "https://github.com/lixiang1994/AttributedString.git", from: "version")

macOS NSTextField 增加action支持

12 Jun 05:41
Compare
Choose a tag to compare

完善+运算符扩展

09 Jun 03:46
Compare
Choose a tag to compare
1.3.1

1.3.1 完善+扩展

增加点击事件支持

05 Jun 09:14
Compare
Choose a tag to compare

点击事件回调 仅支持iOS - UILabel / UITextView.
优化初始化方法.

支持 macOS, tvOS, watchOS

10 Apr 12:49
Compare
Choose a tag to compare

暂定兼容版本
iOS >= 9.0
macOS >= 10.13
tvOS >= 10.0
watchOS >= 5.0

完善扩展

06 Mar 07:52
Compare
Choose a tag to compare

补充了对String,NSAttributedString, AttributedString.Style + += 的支持

增加 + 运算符扩展

21 Nov 04:46
Compare
Choose a tag to compare
let a: AttributedString = .init("123", .background(.blue))
let b: AttributedString = .init("456", .background(.blue))
textView.attributed.text = a + b
        
textView.attributed.text += "test"