ARTICLE DETAIL

资讯详情

深耕郑州网站建设与运营推广的一线实战洞察。

SelectableTextView进阶技巧:用framesOfWordsMatchingValidator打造星星特效与实战锦集

SelectableTextView进阶技巧:用framesOfWordsMatchingValidator打造星星特效与实战锦集 SelectableTextView进阶技巧用framesOfWordsMatchingValidator打造星星特效与实战锦集【免费下载链接】SelectableTextViewA text view that supports selection and expansion项目地址: https://gitcode.com/gh_mirrors/se/SelectableTextViewSelectableTextView是一款专为 iOS 开发的 Swift 文本视图组件核心能力是文本选择高亮与文本展开。它的进阶接口framesOfWordsMatchingValidator可以获取任意匹配文字的精确坐标本文带你用它打造点击话题弹出星星粒子的炫酷特效并附赠一份常用进阶技巧锦集。一、SelectableTextView 解决了什么问题原生的UILabel和UITextView对部分文字可点击/可高亮的支持非常有限。SelectableTextView 通过Validator校验器机制优雅地解决了这个问题⭐文本选择为任意规则的文字话题、链接、占位符注册点击回调与高亮样式⭐文本展开折叠长文本一键显示More.../Less展开按钮⭐Interface Builder 支持属性面板直接拖拽配置⭐滚动支持内置滚动容器并转发滚动事件二、核心APIframesOfWordsMatchingValidator这是本进阶技巧的主角。它的签名非常简洁public func framesOfWordsMatchingValidator(_ validator: TextSelectionValidator) - [CGRect]传入一个校验器它会返回该视图内所有匹配文字的位置矩形数组坐标已加上文本边距可直接用于动画定位。其实现位于 SelectableTextView.swift遍历每个文字 Cell对校验通过的词取出布局属性并转换坐标。实际开发中官方 Demo 把它封装成了一个便捷计算属性见 ViewController.swiftvar hashtagFrame: CGRect? { let frames textView.framesOfWordsMatchingValidator(HashtagTextValidator()) return frames.first }有了这个 frame任何跟随文字的动画都能精确起飞 。三、星星粒子特效分步实现教程完整可运行代码见 ViewController.swift下面拆解为 4 步。第 1 步为话题文字注册点击回调HashtagTextValidator是内置的话题校验器定义于 TextValidators.swift注册后所有#话题都变成可点击的textView.registerValidator(HashtagTextValidator()) { (text, validator) in self.createParticles(seconds: 1) }第 2 步定位粒子发射器中心利用上一步的 frame 计算话题文字的中心点并创建CAEmitterLayerif let frame hashtagFrame { let particleEmitter CAEmitterLayer() let center CGPoint(x: frame.origin.x frame.width / 2, y: frame.origin.y frame.height / 2) particleEmitter.emitterPosition center particleEmitter.emitterShape kCAEmitterLayerPoint particleEmitter.emitterSize frame.size textView.layer.addSublayer(particleEmitter) }第 3 步配置星星粒子CAEmitterCell粒子使用 Demo 中的小星星贴图 star.png关键参数birthRate 12每秒 12 颗、velocity 50、emissionRange 2π全方向喷发、黄色着色。第 4 步自动清理DispatchQueue.main.asyncAfter(deadline: .now() seconds) { particleEmitter.removeFromSuperlayer() }1 秒后自动移除图层特效不留痕迹。整个特效不到 50 行代码核心就在于framesOfWordsMatchingValidator提供了精准的锚点坐标。四、实战锦集6 个高频进阶技巧技巧 1用 \0 转义字符防止误高亮文字中如果高亮词后面紧跟普通标点容易吸附进高亮范围。用空字符\0截断即可详见 README.mdlet text The period next to the #Hashtag\0. Will not be highlighted...技巧 2自定义校验器改变文字颜色通过ContainerTextSelectionValidator协议可以给匹配文字附加样式。例如把所有UI开头的词显示为橙色示例见 UIClassValidator.swifttextView.registerValidator(UIClassValidator())技巧 3添加展开/收起按钮长文本折叠成 2 行点击More...展开全部再点Less收起textView.addExpansionButton(collapsedState: (More..., 2), expandedState: (Less, 0), attributes: attributes)技巧 4Interface Builder 可视化配置SelectableTextView标记为IBDesignable字号、颜色、行距、内边距、行数等属性都能在故事板右侧面板中直接调整所见即所得技巧 5调整 Tab 字符宽度Tab 默认渲染为 4 个空格可通过全局配置修改见 README.mdTabTextModelConfig.numberOfSpaces 2技巧 6Delegate 解决校验器冲突当多个校验器同时命中一段文字时比如#前缀校验器和自定义校验器都匹配#Stars可通过代理方法resolveValidationConflictsForSelectableTextView决定优先执行哪个协议定义见 README.md默认取注册顺序中第一个。五、上手路径速查想了解什么去哪里看星星特效完整 DemoExamples/SelectableTestView-Example/ViewController.swift全部 API 文档README.md内置校验器源码Source/TextSelectionValidator/单元/集成测试用例SelectableTextViewTests/Pod 安装配置SelectableTextView.podspec一句话总结registerValidator负责哪里能点framesOfWordsMatchingValidator负责点在哪里——两者配合就能在 SelectableTextView 上实现任意跟随文字的位置动画星星粒子只是起点。【免费下载链接】SelectableTextViewA text view that supports selection and expansion项目地址: https://gitcode.com/gh_mirrors/se/SelectableTextView创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表