如果你想通过阅读源码来学习 iOS 相关的编程知识,那么通过本项目你可以学到如下一些知识点:
SwiftyText 提供了一些 NSAttributedString 以及 NSMutableAttributedString 的扩展方法,方便使用者快捷设置自己的 Attributed String
extension NSAttributedString {
public func isValidRange(range: NSRange) -> Bool
public func entireRange() -> NSRange
public func proposedSizeWithConstrainedSize(constrainedSize: CGSize, exclusionPaths: [UIBezierPath]?, lineBreakMode: NSLineBreakMode?, maximumNumberOfLines: Int?) -> CGSize //计算最佳Size
public func neighbourFontDescenderWithRange(range: NSRange) -> CGFloat
}
extension NSMutableAttributedString {
public var font: UIFont?
public var foregroundColor: UIColor?
public func setFont(font: UIFont?, range: NSRange)
public func setForegroundColor(foregroundColor: UIColor?, range: NSRange)
public func setLink(link: SwiftyText.SwiftyTextLink?, range: NSRange)
public func insertAttachment(attachment: SwiftyText.SwiftyTextAttachment, atIndex loc: Int)
}
这里创建一个 SwiftyLabel,代码如下:
let label = SwiftyLabel(frame: CGRectMake(0, 0, 300, 400))
label.center = self.view.center
label.delegate = self
label.backgroundColor = UIColor(red: 243/255.0, green: 1, blue: 236/255.0, alpha: 1)
label.text = "Swift is a powerful and intuitive programming language for iOS, OS X, tvOS, and watchOS. https://developer.apple.com/swift/resources/ . Writing Swift code is interactive and fun, the syntax is concise yet expressive, and apps run lightning-fast. Swift is ready for your next project — or addition into your current app — because Swift code works side-by-side with Objective-C. "
label.textContainerInset = UIEdgeInsetsMake(6, 6, 6, 6)
label.font = UIFont.systemFontOfSize(14)
label.textColor = UIColor.blackColor()
label.firstLineHeadIndent = 24
label.drawsTextAsynchronously = true
这里的链接指的是文本中可以点击的内容。链接属性在 SwiftyText 中使用 SwiftyTextLink 来表示,SwiftyTextLink 中包含如下的一些属性
代码示例:
let link = SwiftyTextLink()
link.URL = NSURL(string: "https://developer.apple.com/swift/")
link.attributes = [NSForegroundColorAttributeName:UIColor(red: 0, green: 122/255.0, blue: 1.0, alpha: 1.0),NSUnderlineStyleAttributeName:NSUnderlineStyle.StyleSingle.rawValue]
label.setLink(link, range: NSMakeRange(0, 5))
在很多情况下我们需要对特定的模式的文本做特殊处理,诸如文本替换,或者特定的文本设置特定的属性,诸如颜色,字体等,这个时候我们便可以通过实现 SwiftyTextParser 协议来实现自己的 Text Parser,设置到 SwiftyLabel 中。Text Parser 存在的好处在于处理逻辑的复用。在 SwiftyText 中定义了一种叫做 Detector 的特殊 Text Parser,可以通过设置正则以及对应的属性的方式来创建一个 Parser。还有一个特殊的 Text Parser 叫做 SwiftyTextSuperParser,它其实就是一个 parser container,是一个 Text Parser 的容器,这样就可以将多个 Text Parser 合并成一个。
下面主要讲解下 Detector
let detector = SwiftyTextDetector.detectorWithType([.URL,.Address])
if detector != nil {
label.parser = detector
}
SwiftyTextAttachment 在 NSTextAttachment 上做了增强,同时支持基于图片以及基于 UIView 的 Attachment。图片,UIView 类型的附件都支持和文本在纵向上的各种对齐方式:靠顶对齐,居中,靠底对齐,缩放以适配文本高度,都支持通过设置 padding 来控制前后的 padding。图片 Attachment 还支持通过设置 imageSize 来控制图片的大小(当垂直对齐为 靠顶对齐,居中,靠底对齐时起作用)
let imageAttachment = SwiftyTextAttachment()
imageAttachment.image = UIImage(named: "logo")
imageAttachment.attachmentTextVerticalAlignment = .Top
label.insertAttachment(imageAttachment, atIndex: label.textStorage.length)
let sliderAttachment = SwiftyTextAttachment()
let slider = UISlider()
sliderAttachment.contentView = slider;
sliderAttachment.padding = 3.0
sliderAttachment.attachmentTextVerticalAlignment = .Center
label.insertAttachment(sliderAttachment, atIndex: 8)
下图为 demo 的效果截图:
其余的Text Kit的特性,比如exclusionPaths,可以通过SwiftyLabel的exclusionPaths属性进行设置。
SwiftyText 基于 MIT 许可证开源。具体详情请查看根目录下的LICENSE文件。