Swift 花式输出 String 效果.zip

  • S9_554110
    了解作者
  • 678.5KB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-06-03 02:41
    上传日期
Swift 花式输出 String 效果。.zip,Elegant & painless Attributed Strings in Swift
SwiftRichString.zip
内容介绍
<p align="center" > <img src="https://raw.githubusercontent.com/malcommac/SwiftRichString/master/SRRLogo.png" width=450px alt="SwiftRichString" title="SwiftRichString"> </p> [![Platform](https://img.shields.io/cocoapods/p/SwiftRichString.svg?style=flat)](http://cocoadocs.org/docsets/SwiftRichString) [![Version](https://img.shields.io/cocoapods/v/SwiftRichString.svg?style=flat)](http://cocoadocs.org/docsets/SwiftRichString) [![Carthage Compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage) [![Twitter](https://img.shields.io/badge/twitter-@danielemargutti-blue.svg?style=flat)](http://twitter.com/danielemargutti) SwiftRichString is a lightweight library which allows to create and manipulate attributed strings easily both in iOS, macOS, tvOS and even watchOS. It provides convenient way to store styles you can reuse in your app's UI elements, allows complex tag-based strings rendering and also includes integration with Interface Builder. It even support **iOS 11's Dynamic Type**! If you manipulate `NSAttributedString` in Swift, SwiftRichString allows you to keep your code manteniable, readable and easy to evolve. ## Features Highlights Want to know what SwiftRichString can do in your app? Lets take a look to these feature highlights! ### 1. Easy Styling The main concept behind this lib is the `Style`: a style is just a collection of text attributes you can apply to a string. The following example show how to create a style an produce an attributed string with it: ```swift let style = Style { $0.font = SystemFonts.AmericanTypewriter.font(size: 25) // just pass a string, one of the SystemFonts or an UIFont $0.color = "#0433FF" // you can use UIColor or HEX string! $0.underline = (.patternDot, UIColor.red) $0.alignment = .center } let attributedText = "Hello World!".set(style: style) // et voilà! ``` ### 2. Global Styles & Interface Builder Integration Styles can be also registered globally and reused in your app. Just define your own style and register using `Styles.register()` function: ```swift let myStyle = Style { // define style's attributes... } Styles.register("MyStyle", style: style) ``` Now you can reuse it everything in your app; SwiftRichString exposes a `styleName` property for the most common text containers and you can set it directly in Interface Builder: ### 3. Complex Rendering with tag-based strings SwiftRichString allows you to render complex strings by parsing text's tags: each style will be identified by an unique name (used inside the tag) and you can create a `StyleGroup` which allows you to encapsulate all of them and reuse as you need (clearly you can register it globally). ```swift // Create your own styles let normal = Style { $0.font = SystemFonts.Helvetica_Light.font(size: 15) } let bold = Style { $0.font = SystemFonts.Helvetica_Bold.font(size: 20) $0.color = UIColor.red $0.backColor = UIColor.yellow } let italic = normal.byAdding { $0.traitVariants = .italic } // Create a group which contains your style, each identified by a tag. let myGroup = StyleGroup(base: normal, ["bold": bold, "italic": italic]) // Use tags in your plain string let str = "Hello <bold>Daniele!</bold>. You're ready to <italic>play with us!</italic>" self.label?.attributedText = str.set(style: myGroup) ``` That's the result! -- ## Documentation **Are you using SwiftRichString 1.x in your project? Don't miss to take a look at [Migration section of the documentation](#migration).** You can still use the 1.x release by using tagged version 1.1.0. **Table Of Contents** Full changelog is available in [CHANGELOG.md](CHANGELOG.md) file. - [Versions (1.x, 2.x and old Swift 3.x branch)](#versions) - [Introduction to `Style`, `StyleGroup` & `StyleRegEx`](#stylestylegroup) - [Introduction](#introduction) - [String & Attributed String concatenation](#concatenation) - [Apply styles to `String` & `Attributed String`](#manualstyling) - [Fonts & Colors in `Style`](#fontscolors) - [Derivating a `Style`](#derivatingstyle) - [Support iOS's `Dynamic Type`](#dynamictype) - [Dynamic Attributes from Tag's Params](#dynamicattributes) - [The `StyleManager`](#stylemanager) - [Register globally available styles](#globalregister) - [Defer style creation on demand](#defer) - [Assign style using Interface Builder](#ib) - [All properties of `Style`](#props) Other info: - [Migration from SwiftRichString 1.x](#migration) - [Requirements](#requirements) - [Installation](#installation) - [Contributing](#contributing) - [Copyright](#copyright) <a name="versions"/ rel='nofollow' onclick='return false;'> ### Versions - **SwiftRichString 2.x branch (current)**. The latest version is [2.0.2](https://github.com/malcommac/SwiftRichString/releases/tag/2.0.2). - **SwiftRichString 1.x branch (supported)**. Use [1.1.0 tag](https://github.com/malcommac/SwiftRichString/releases/tag/1.1.0). Its compatible with Swift 4.x. - **Swift 3.x (no longer mantained)**. Use [0.9.1 release](https://github.com/malcommac/SwiftRichString/releases/tag/0.9.10). <a name="stylestylegroup"/ rel='nofollow' onclick='return false;'> ## Introduction to `Style`, `StyleGroup`, `StyleRegEx` <a name="introduction"/ rel='nofollow' onclick='return false;'> ### Introduction The main concept behind SwiftRichString is the use of `StyleProtocol` as generic container of the attributes you can apply to both `String` and `NSMutableAttributedString`. Concrete classes derivated by `StyleProtocol` are: `Style`, `StyleGroup` and `StyleRegEx`. Each of these classes can be used as source for styles you can apply to a string, substring or attributed string. #### `Style`: apply style to strings or attributed strings A `Style` is a class which encapsulate all the attributes you can apply to a string. The vast majority of the attributes of both AppKit/UIKit are currently available via type-safe properties by this class. Creating a `Style` instance is pretty simple; using a builder pattern approach the init class require a callback where the self instance is passed and allows you to configure your properties by keeping the code clean and readable: ```swift let style = Style { $0.font = SystemFonts.Helvetica_Bold.font(size: 20) $0.color = UIColor.green // ... set any other attribute } let attrString = "Some text".set(style: style) // attributed string ``` #### `StyleGroup`: Apply styles for tag-based complex string `Style` instances are anonymous; if you want to use a style instance to render a tag-based plain string you need to include it inside a `StyleGroup`. You can consider a `StyleGroup` as a container of `Styles` (but, in fact, thanks to the conformance to a common `StyleProtocol`'s protocol your group may contains other sub-groups too). ```swift let bodyStyle: Style = ... let h1Style: Style = ... let h2Style: Style = ... let group = StyleGroup(base: bodyStyle, ["h1": h1Style, "h2": h2Style]) let attrString = "Some <h1>text</h1>, <h2>welcome here</h2>".set(style: group) ``` The following code defines a group where: - we have defined a base style. Base style is the style applied to the entire string and can be used to provide a base ground of styles you want to apply to the string. - we have defined two other styles named `h1` and `h2`; these styles are applied to the source string when parser encounter some text enclosed by these tags. #### `StyleRegEx`: Apply styles via regular expressions `StyleRegEx` allows you to define a style which is applied when certain regular expression is matched inside the target string/attributed string. ```swift let emailPattern = "([A-Za-z0-9_\\-\\.\\+])+\\@([A-Za-z0-9_\\-\\.])+\\.([A-Za-z]+)" let style = StyleRegEx(pattern: emailPattern) { $0.color = UIColor.red $0.backColor = UIColor.yellow } let attrString = "My email is hello@danielemargutti.com and my website is http://www.danielemargutti.com".(style: style!) ``` The re
评论
    相关推荐
    • 动画效果swift
      能够分解图片进入界面和分解图片出来界面
    • swift-Windless又一款预显示布局实现方案以供应用动态加载数据视图
      Windless:又一款预显示布局实现方案,以供应用动态加载数据视图
    • Swift
      该项目是通过。 您将在下面找到一些有关如何执行常见任务的信息。 您可以在找到本指南的最新版本。 目录 更新到新版本 Create React App分为两个包: create-react-app是用于创建新项目的全局命令行实用程序。...
    • swift登录demo
      这是一个swift demo 包含swift的基本语法,使用pod 集成, 不使用 xib,纯代码编程, 包含网络请求封装类.. 枚举, 扩展,结构体,等等等~~
    • 30 mins or less-crx插件
      语言:English (United States) 终于有了Autolab专栏了! 此扩展在Autolab上创建了样式分级规则,允许TASwift对提交进行分级。
    • Swift-Basic-Lesson-01
      Swift-Basic-Lesson-01
    • Swift 常用设计模式
      Swift is still very, very young and many developers are still figuring out how to best use the language. There are countless tutorials about patterns and best practices, which makes it hard to see ...
    • swift 版俄罗斯方块
      作者agelessman,源码Tetris,很简单的用swift实现的俄罗斯方块游戏,仅供swift学习。
    • swiftascmaps:泰勒·斯威夫特(Taylor Swift)启发的色彩图
      泰勒·斯威夫特(Taylor Swift)彩色地图收藏。 包括基于以下相册的颜色图: 红色(红色,red_r) 1989年(nineteen_eighty_nine,nineteen_eighty_nine_r) 声誉(信誉,声誉_r) 情人(情人,lover_r) 民俗...
    • SIM800C_MQTT.rar
      使用SIM800C模块,使用MQTT协议,连接中国移动onenet平台,能实现数据的订阅、发布、存储等