BadasSwift
描述
一组 Swift 扩展和自定义功能库!
文档:
示例
要运行示例项目,克隆仓库,并在 Example 目录中首先运行 pod install
。
请注意,示例项目目前正在开发中。我仅用它来测试我编写的代码,因此目前可能有些混乱。
要求
- iOS 9.3+
- Xcode 9.4+
- Swift 4.0+
安装
BadasSwift可以通过CocoaPods获取。要安装它,只需将以下行添加到您的Podfile中
pod 'BadasSwift'
文档
扩展
-
包提供快速访问应用版本和构建号。
public var versionNumber: String? { get } public var buildVersionNumber: String? { get }
用法
print("Version number: \(Bundle.main.versionNumber)")
输出 -> "版本号:1.0"
-
字符集static let urlAllowed: CharacterSet
urlAllowed变量是以下CharacterSet联合
- .urlFragmentAllowed
- .urlHostAllowed
- .urlPasswordAllowed
- .urlQueryAllowed
- .urlUserAllowed
它允许快速将任何字符串转换为格式化的可接受URL字符串。
用法
var urlStr = "http://google.com/search?q=Hello there, have a good day." urlStr = urlStr.addingPercentEncoding(withAllowedCharacters: .urlAllowed)! print(urlStr)
输出 -> "http://google.com/search?q=Hello%20there,%20have%20a%20good%20day."
-
CLLocationManager
public static var isAuthorized: Bool { get }
如果用户授权始终或当使用时返回True
-
日期
返回指定格式的日期字符串。
public func formatedString(_ format: String) -> String
用法
print("Today's date is: \(Date().formatedString("EEEE, MMM d, yyyy"))")
输出 -> "今日日期为星期一,10月11日,2018年"
-
NSLayoutConstraint
为NSLayoutConstraint提供了一些实用的实时动画方法。
public func animateConstraintTo(_ value: CGFloat, for view: UIView, duration: Double) public func animateConstraintTo(_ value: CGFloat, for view: UIView, duration: Double, completion: @escaping () -> Void) public func setMultiplier(multiplier: CGFloat) -> NSLayoutConstraint
-
String
有一串充满HTML标签的字符串?使用以下方法将其转换为attributes string。
public var html2AttributedString: NSAttributedString? { get } public var html2String: String { get }
用法
let htmlStr = "<h1>Very Big Title</h1>" let attributedStr = htmlStr.html2AttributedString()
或者,如果你喜欢,你可以平滑地将任何字符串转换为MD5!
public func MD5Hex() -> String public func MD5base64() -> String
-
UIButton
func alignTextHorizontal(spacing: CGFloat = default)
-
UIImage
将图片转换为黑白(灰度)。
public func convertToGrayScale() -> UIImage
检查两张图片是否完全相同。
public func isEqualTo(_ image: UIImage) -> Bool
将任何图片缩放到指定的CGSize。
func scaleImage(scaleToSize: CGSize) -> UIImage
-
UIImageView
从其URL中检索任何网络图像并将其应用于图像视图。快速且异步!
public func setImageWithUrlString(_ urlStr: String, completion: @escaping (_ img: UIImage?) -> Void)
用法
anyImageView.setImageWithUrlString("http://something.com/someImage.png") { (img) in //the completion block send back the UIImage object that has been fetched. }
-
UIView
获取视图层次结构中的根视图。
public var rootView: UIView { get }
将任何视图的透明度动画到 1.0f。可以带或不带完成块调用。public func showWithDuration(_ duration: Double) public func showWithDuration(_ duration: Double, completion: @escaping () -> Void)
将任何视图的透明度动画到 0.0f。可以带或不带完成块调用。
public func hideWithDuration(_ duration: Double) public func hideWithDuration(_ duration: Double, completion: @escaping () -> Void)
给任何视图添加一个所需颜色的1px宽度边框。主要用于调试目的。public func putBorders(color: UIColor)
向任何视图添加一个所需颜色的垂直渐变图层。public func putGradient(_ colors: CGColor...)
基本将一个视图转换成 UIImage。public func takeSnapshot() -> UIImage?
-
UIViewController
public func setStatusBarColor(_ color: UIColor)
轻松更改任何视图控制器的状态栏颜色!
用法
override func viewDidLoad() { super.viewDidLoad() self.setStatusBarColor(UIColor.blue) }
-
URL
public static func verifyUrl(string: String?) -> Bool
如果 URL 正确/可访问,则返回 true。否则返回 false。
Models
-
SnappingCollectionViewLayout
这个类是 UICollectionViewFlowLayout 的子类。它为集合视图单元格提供简单的吸附功能。
用法
let snapLayout:SnappingCollectionViewLayout = SnappingCollectionViewLayout() snapLayout.scrollDirection = .horizontal // Optional, you can ajust inset to better match your cells size. snapLayout.sectionInset = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 20) anyCollectionView.collectionViewLayout = snapLayout
结果
Views
-
VoteView
这是一个非常简单的Objective-C编写的视图,用于创建让一些用户对某些东西进行评分的功能。可以是任何东西。
您可以轻松地继承这个IBDesignable视图,使其成为自己的视图。
它实现了几个代理- (UIImage*)setSelectedImgName; - (UIImage*)setUnselectedImgName; - (int)setImageNumber;
这三个在代码中都会在
draw(_ rect:CGRect)
方法中被调用。开发者可以根据自己的偏好,直接在Storyboard中设置UIImages和图片数量,也可以调用这些代理来设置它们。它们像生活中的很多事情一样:可选的。- (void)voteDidChange:(int)voteResult;
每次用户点击视图中的图像时,都会调用这个代理。只要投票有变化。
-
AwesomePageControl
这是一个编码定制的分段控制,其设计类似于用于Chrome手机应用新版的部分。
看看它是多么的美丽!
您可以直接使用它,但截至目前它仍在开发中,因此您可能需要尝试添加更多段,可能会遇到困难。
-> IBDesignable版本即将推出。无论如何,它只有一个代理方法,即
@objc optional func didTapControlAtIndex(_ index:Int) -> Void
每次有新的"段"被选中时,都会调用这个方法。
作者
morganberger, [email protected]
许可证
BadasSwift遵循MIT许可证。有关更多信息,请参阅LICENSE文件。