只需要将UIView放入您控制器中的任何位置,并使其成为TFBubbleItUpView的子类。因为它配置为IBDesignable,所以将显示出来。内容大小由视图本身计算,无需使用高度约束。只需在Interface Builder中将内联大小设置为占位符 - 宽度检查无,对于高度选择最适合您的选项。
还有一个可用的代理(命名为bubbleItUpDelegate,因为TFBubbleItUpView实际上是UICollectionView的子类)以及当前的一个方法:func bubbleItUpViewDidFinishEditingBubble(view: TFBubbleItUpView, text: String)。
您可以使用setItem([TFBubbleItem])(其中TFBubbleItem是一个结构体,其初始化器接受字符串TFBubbleItem(text: “Hullo!”))来预设值。
如果您想从视图中访问所有项,有多种方法可以实现,其中一种是validStrings() -> [String]。因为有时会有空项,此方法将为您筛选它们并只发送有效的字符串。
在所有配置中,可以在项被吹泡之前验证它们。有一个typealias称为Validation,实际上只是一个函数定义。
public typealias Validation = String -> Bool
验证函数接受一个字符串并返回一个布尔值,表示字符串是否有效。这些方法应该是基本的。TFBubbleItUpValidation作为公开类已提供两种方法 - testEmptiness()和testEmailAddress()。这些方法返回Validation函数,您可以使用如下方式调用验证:testEmptiness()(“a text”)。这种将函数评估转换为的技术称为Currying。您可以选择提供自己的Validation。
func testSomething() -> Validation {
return { text in
return text == "something"
}
}
这允许我们通过TFBubbleItUpValidation.combine(v1: Validation, v2: Validation)函数或提供的运算符|>>轻松组合验证。
let validation = TFBubbleItUpValidation.testEmptiness() |>> TFBubbleItUpValidation.testEmailAddress()
验证可以通过配置应用于TFBubbleItUpView
TFBubbleItUpViewConfiguration.itemValidation = validation
我们还可以通过以下方式验证用户可以输入的项的最大数量:
TFBubbleItUpViewConfiguration.numberOfItems = .Quantity(5) // default .Unlimited
BubbleItUp具有高度可配置性。有一个名为TFBubbleItUpViewConfiguration的配置文件,其中包含用于配置的类变量。
这是外观和功能的混合。我想指出skipOnWhitespace和skipOnReturnKey属性,通过它们可以更改文本周围泡泡创建的行为(请参阅下面的文档注释)。
/// Background color for cell in normal state
public static var viewBackgroundColor: UIColor = UIColor(red: 0.918, green: 0.933, blue: 0.949, alpha: 1.00)
/// Background color for cell in edit state
public static var editBackgroundColor: UIColor = UIColor.whiteColor()
/// Background color for cell in invalid state
public static var invalidBackgroundColor: UIColor = UIColor.whiteColor()
/// Font for cell in normal state
public static var viewFont: UIFont = UIFont.systemFontOfSize(12.0)
/// Font for cell in edit state
public static var editFont: UIFont = UIFont.systemFontOfSize(12.0)
/// Font for cell in invalid state
public static var invalidFont: UIFont = UIFont.systemFontOfSize(12.0)
/// Font color for cell in view state
public static var viewFontColor: UIColor = UIColor(red: 0.353, green: 0.388, blue: 0.431, alpha: 1.00)
/// Font color for cell in edit state
public static var editFontColor: UIColor = UIColor(red: 0.510, green: 0.553, blue: 0.596, alpha: 1.00)
/// Font color for cell in invalid state
public static var invalidFontColor: UIColor = UIColor(red: 0.510, green: 0.553, blue: 0.596, alpha: 1.00)
/// Corner radius for cell in view state
public static var viewCornerRadius: Float = 2.0
/// Corner radius for cell in edit state
public static var editCornerRadius: Float = 2.0
/// Corner radius for cell in invalid state
public static var invalidCornerRadius: Float = 2.0
/// Height for item
public static var cellHeight: Float = 25.0
/// View insets
public static var inset: UIEdgeInsets = UIEdgeInsetsMake(5, 5, 5, 5)
/// Interitem spacing
public static var interitemSpacing: CGFloat = 5.0
/// Line spacing
public static var lineSpacing: CGFloat = 5.0
/// Keyboard type
public static var keyboardType: UIKeyboardType = UIKeyboardType.EmailAddress
/// Keyboard return key
public static var returnKey: UIReturnKeyType = UIReturnKeyType.Done
/// Field auto-capitalization type
public static var autoCapitalization: UITextAutocapitalizationType = UITextAutocapitalizationType.None
/// Field auto-correction type
public static var autoCorrection: UITextAutocorrectionType = UITextAutocorrectionType.No
/// If true it creates new item when user types whitespace
public static var skipOnWhitespace: Bool = true
/// If true it creates new item when user press the keyboards return key. Otherwise resigns first responder
public static var skipOnReturnKey: Bool = false
/// Number of items that could be written
public static var numberOfItems: NumberOfItems = .Unlimited
/// Item has to pass validation before it can be bubbled
public static var itemValidation: Validation? = nil
TFBubbleItUp使用Swift 2.0。目标部署iOS 8.0及以上版本。
TFBubbleItUp可以通过CocoaPods获取。要安装它,只需将以下行添加到您的Podfile中
pod "TFBubbleItUp"
Ales Kocur,[email protected]
TFBubbleItUp根据MIT授权可用。有关更多信息,请参阅LICENSE文件。