SentenceKit 版本 0.3.0

SentenceKit 版本 0.3.0

Ricky Kirkendall 维护。



SentenceKit 版本 0.3.0

SentenceKit

SentenceKit 是一个用于构建自然语言用户界面的 iOS 框架。此类 UI 已成功用于 Beats by Dre (Apple Music 之前) 和 Philz Coffee 等应用程序;这两个项目都为本项目提供了灵感。自然语言界面享有让用户感到瞬间熟悉的优点。如果您会阅读,您就已经知道如何使用它。NLI 还有助于提供 UX 结构,因为句子本质上是叙述性的。使用起来也很愉快,因为您必须进入用户的故事,才能编写有关他们在您的应用程序中做什么的句子。

设计 SentenceKit API 的目标是尽可能减少将那些用户故事转换为功能性 UI 的过程 overhead,同时仍然允许进行定制和扩展。

示例

Example screenshot

// Define controls
let sizeChoice = MultiChoice(tag: "size")
let creamAmtChoice = MultiChoice("creamAmt")
let creamTypeChoice = MultiChoice("creamType")
let sweetnerAmtChoice = MultiChoice("sweetnerAmt")
let sweetnerTypeChoice = MultiChoice("sweetnerType")
let temperatureChoice = MultiChoice("temp")
let addlNotes = FreeEntry("addlNotes")
var controls: [ControlFragment] = [sizeChoice,
                creamAmtChoice,
                creamTypeChoice,
                sweetnerAmtChoice,
                sweetnerTypeChoice,
                temperatureChoice,
                addlNotes]


...

// Configure controls

sizeChoice.options = ["Large", "Small"]
creamAmtChoice.options = ["Creamy", "Medium", "Light", "None"]
creamTypeChoice.options = ["Cream", "Whole Milk", "2% Milk", "Low-Fat Milk",
                            "Non-Fat Milk", "Almond Milk", "Soy Milk", "Vanilla Soy"]
sweetnerAmtChoice.options = ["Sweet", "Medium", "Light", "None"]
sweetnerTypeChoice.options = ["Sugar", "Honey", "Splenda", "Stevia", "Sweet'N Low", "Equal"]
temperatureChoice.options = ["Hot", "Iced"]

// Build sentence by concatenating fragments

sentence += "I'll have a "
sentence += sizeChoice
sentence += ", with "
sentence += creamAmtChoice
sentence += " "
sentence += creamTypeChoice
sentence += "\nand\n"
sentence += sweetnerAmtChoice
sentence += " "
sentence += sweetnerTypeChoice
sentence += ", "
sentence += temperatureChoice
sentence += ".\n\n"
sentence += addlNotes

// Add resolutions to ensure the sentence always presents user selections in a sensible manner

sentence.resolutions += { if self.sweetnerAmtChoice.alias == "None" { self.sweetnerAmtChoice.alias = "No"; self.sweetnerTypeChoice.alias = "Sugar" }}
sentence.resolutions += { if self.sweetnerAmtChoice.alias == "Sweet" { self.sweetnerAmtChoice.alias = "lots of" }}
sentence.resolutions += { if self.creamAmtChoice.alias == "None" { self.creamAmtChoice.alias = "No"; self.creamTypeChoice.alias = "Cream" }}
sentence.resolutions += { if self.creamAmtChoice.alias == "Creamy" { self.creamAmtChoice.alias = "lots of" }}
sentence.resolutions += { for c in self.controls { c.alias = c.alias?.lowercased() } }

// Define a style to customize the appearance of your sentence UI, or use one of the presets

let style = Style.MintMojito        
sentenceView.style = style
view.backgroundColor = style.backgroundColor

// Present the sentence in a SentenceView
let sentenceView = SentenceView(autolayout: true)
sentenceView.sentence = sentence