个人在写项目中常常用到的一些代码片段,会不断更新
// 获取屏幕宽度
func deviceWidth() -> (CGFloat) {
let width = UIScreen.mainScreen().bounds.size.width
return width
}
// 获取屏幕高度
func deviceHeight() -> (CGFloat) {
let height = UIScreen.mainScreen().bounds.size.height
return height
}
// viewController添加到另一个viewController
func addChildVC(viewController: UIViewController) {
self.addChildViewController(viewController)
self.view.addSubview(viewController.view)
viewController.didMoveToParentViewController(self)
}
// viewController从另一个viewController移除
func removeChildVC(viewController: UIViewController){
viewController.view.removeFromSuperview()
viewController.willMoveToParentViewController(nil)
viewController.removeFromParentViewController()
}
// block常用的弱指针
weak var weakSelf = self
guard let _weakSelf = weakSelf else{return}
// 获取文字的size
extension String{
func qb_stringSize(font: UIFont, maxWidth: CGFloat, maxHeight: CGFloat) -> CGSize{
let arrt: Dictionary<String, UIFont> = [NSFontAttributeName: font]
let maxSize: CGSize = CGSize(width: maxWidth, height: maxHeight)
let options: NSStringDrawingOptions = [.TruncatesLastVisibleLine,.UsesLineFragmentOrigin,.UsesFontLeading]
return self.boundingRectWithSize(maxSize, options: options, attributes: arrt, context: nil).size
}
func qb_stringSizeWithFont(font: UIFont) -> CGSize{
return qb_stringSize(font, maxWidth: 0, maxHeight: 0)
}
}