{"id":2023,"date":"2023-03-24T17:09:17","date_gmt":"2023-03-24T09:09:17","guid":{"rendered":"http:\/\/www.youthtribe.com\/?p=2023"},"modified":"2023-03-24T18:13:36","modified_gmt":"2023-03-24T10:13:36","slug":"ios-swift5-%e8%87%aa%e5%b7%b1%e8%ae%be%e8%ae%a1%e7%9a%84%e4%b8%80%e4%b8%aa%e7%b1%bb%e4%bc%bc%e5%ae%89%e5%8d%93android%e7%9a%84toast%e7%9a%84%e4%bf%a1%e6%81%af%e6%98%be%e7%a4%ba%e6%8f%90%e7%a4%ba","status":"publish","type":"post","link":"http:\/\/www.youthtribe.com\/archives\/2023","title":{"rendered":"IOS-swift5-\u81ea\u5df1\u8bbe\u8ba1\u7684\u4e00\u4e2a\u7c7b\u4f3c\u5b89\u5353Android\u7684TOAST\u7684\u4fe1\u606f\u663e\u793a\u63d0\u793a\u529f\u80fd"},"content":{"rendered":"\n

\u81ea\u5df1\u8bbe\u8ba1\u7684\u82f9\u679cios\u7684\u4e00\u4e2a\u7c7b\u4f3c\u5b89\u5353\u7684TOAST\u7684\u4fe1\u606f\u663e\u793a\u63d0\u793a\u529f\u80fd\uff0c\u57fa\u4e8eswift5\u5f00\u53d1\u3002\u53ef\u4ee5\u6307\u5b9a \u663e\u793a\u65f6\u957f\u3002\u5355\u4f4d \u79d2\uff0c\u53ef\u4ee5\u662f\u5c0f\u6570\uff0c\u6bd4\u59822.5\u79d2\u3002<\/p>\n\n\n\n

\/\/\n\/\/  Tools.swift\n\/\/  Mark\n\/\/\n\/\/  Created by nelson on 2022\/12\/14.\n\/\/\n\nimport Foundation\nimport UIKit\n\/\/ \u81ea\u5df1\u6574\u7684\u4e00\u4e2a\u5de5\u5177\u7c7b\nclass Tools {\n    static func toast(msg:String,seconds:Double,vc:UIViewController){\n        \n        \/\/\u5f97\u5230\u5f53\u524d UIViewController \u7684 \u5b89\u5168\u663e\u793a\u533a\u57df\n        \n        let insets:UIEdgeInsets = vc.view.safeAreaInsets\n        \/\/\u5f00\u59cb\u6d4b\u91cf\u5f53\u524d\u5185\u5bb9\u8981\u663e\u793a\u7684 \u5927\u5c0f\uff0c\u5bbd\u5ea6 \u4e0d\u8d85\u8fc7 \u6700\u5927\u5bbd\u5ea6\n        \n        \n        \/\/\u4e0d\u8981\u7d27\u8d34\u7740\u5e95\u90e8\uff0c\u7559\u4e00\u4e2amarging->to->bottom\n        let marginBottom:CGFloat = 50\n        \n        let width = UIScreen.getScreenWidth() - insets.left - insets.right\n        let height = UIScreen.getScreenHeight() - insets.top - insets.bottom - marginBottom\n        \n        let font:UIFont = UIFont.preferredFont(forTextStyle: .body)\n        \n        let dict = [NSAttributedString.Key.font : font]\n        let msgRectTemp = (msg as NSString).boundingRect(with: CGSize(width: width , height: height), options: [.usesFontLeading,.usesLineFragmentOrigin], attributes: dict as [NSAttributedString.Key : Any], context: nil)\n        \n        \/\/\u56e0\u4e3a\u6709insets\uff0c\u6240\u4ee5\u9700\u8981\u4eba\u4e3a\u6269\u5927 rect\n        let msgRect = CGRect(x: 0, y: 0, width: Int(ceil(msgRectTemp.width)) + Int(MyLabelToast.insetHor * 2), height: Int(ceil(msgRectTemp.height)) + Int(MyLabelToast.insetVer * 2))\n        \n        \n        let y = Int(UIScreen.getScreenHeight() - insets.bottom - msgRect.height - marginBottom)\n        \n        let x = insets.left + (CGFloat(width) - msgRect.width ) \/ 2\n        \n        var label:UILabel?\n        \n        let frame = CGRect(x: Int(x), y: y, width: Int(ceil(msgRect.width))  , height: Int(msgRect.height))\n\n        label = vc.view.window?.windowScene?.windows.last?.viewWithTag(999) as? UILabel\n        if(label == nil){\n            \/\/\u65b0\u5efa\u7acb\n            label = MyLabelToast(frame: frame)\n            \n            label!.layer.masksToBounds = true \/\/ \u4e0d\u52a0\u8fd9\u53e5 \u5706\u89d2\u4f1a\u65e0\u6548\n            label!.layer.cornerRadius = 5\n            \n            label!.backgroundColor = UIColor.black.withAlphaComponent(0.5)\n            label!.textColor = UIColor.white\n            label!.font = font\n            \n            \/\/\n            label!.tag = 999\n        }\n        else{\n            \/\/\u4e0d\u7528\u65b0\u5efa\u7acb\n            \/\/label!.frame = frame\n        }\n        \n        \/\/\n        label!.text = msg\n        vc.view.window?.windowScene?.windows.last?.addSubview(label!)\n        \n        \/\/\u4e0b\u8fb9\u7684\u65b9\u6cd5\u4e0d\u53ef\u4ee5\uff01\u5728pad\u4e0a \uff0c\u4e0d\u662f\u5168\u5c4f\u7684vc\u65f6\uff0c\u4f1a\u663e\u793a\u4e0d\u6b63\u5e38\n        \/\/vc.view.addSubview(label!)\n        \n        \/\/\u5b9a\u65f6\u5668\n        let timer = Timer.scheduledTimer(withTimeInterval: seconds, repeats: false) { Timer in\n            if(label != nil){\n                label!.removeFromSuperview()\n            }            \n        }\n        \n        return\n    }\n}\n<\/pre>\n\n\n\n
extension UIScreen{\n    static func getScreenWidth()->CGFloat{\n        return UIScreen.main.bounds.size.width\n    }\n    static func getScreenHeight()->CGFloat{\n        return UIScreen.main.bounds.size.height\n    }    \n    static func getScreenSize()->CGSize{\n        return UIScreen.main.bounds.size\n    }\n}<\/pre>\n\n\n\n

\u7528\u6765\u5448\u73b0\u6587\u5b57\u7684label<\/p>\n\n\n\n

\/\/\n\/\/  MyLabelToast.swift\n\/\/  Mark\n\/\/\n\/\/  Created by nelson on 2023\/3\/21.\n\/\/\n\/*\n \u4e3b\u8981\u5c31\u662f\u4e3a\u4e86\u589e\u52a0 toast \u4e2d label\u7684 padding\n *\/\nimport UIKit\n\nclass MyLabelToast: UILabel {\n\n    static let insetHor:CGFloat = 20\n    static let insetVer:CGFloat = 10\n    \/\/ Only override draw() if you perform custom drawing.\n    \/\/ An empty implementation adversely affects performance during animation.\n    override func draw(_ rect: CGRect) {\n        \/\/ Drawing code\n        let insets = UIEdgeInsets(top: 10, left: 20, bottom: 10, right: 20)\n        super.drawText(in: rect.inset(by: insets))\n\/\/        super.draw(rect)\n        \n    }\n    override init(frame: CGRect) {\n        super.init(frame: frame)\n        self.numberOfLines = 0\n    }\n    \n    required init?(coder: NSCoder) {\n        fatalError(\"init(coder:) has not been implemented\")\n    }\n    \n}\n<\/pre>\n\n\n\n

\u7528\u6cd5\u5982\u4e0b\uff1a<\/p>\n\n\n\n

Tools.toast(msg: \"\u9752\u6625\u90e8\u843d\uff01GREAT!www.youthtribe.com\uff01\", seconds: 3, vc: self)<\/pre>\n\n\n\n

\u6548\u679c\u5982\u4e0b\uff0c\u7c7b\u4f3candroid\u7684toast\uff1a<\/p>\n\n\n\n

\"\"<\/a><\/figure>\n\n\n\n
\"\"<\/a><\/figure>\n","protected":false},"excerpt":{"rendered":"

\u81ea\u5df1\u8bbe\u8ba1\u7684\u82f9\u679cios\u7684\u4e00\u4e2a\u7c7b\u4f3c\u5b89\u5353\u7684TOAST\u7684\u4fe1\u606f\u663e\u793a\u63d0\u793a\u529f\u80fd\uff0c\u57fa\u4e8eswift5\u5f00\u53d1\u3002\u53ef\u4ee5\u6307\u5b9a \u663e\u793a\u65f6\u957f\u3002\u5355\u4f4d \u79d2\uff0c\u53ef\u4ee5\u662f\u5c0f\u6570\uff0c\u6bd4\u59822.5\u79d2\u3002 \/\/ \/\/ Tools.swift \/\/ Mark \/\/ \/\/ Created by nelson on 2022\/12\/14. \/\/ import Foundation import UIKit \/\/ \u81ea\u5df1\u6574\u7684\u4e00\u4e2a\u5de5\u5177\u7c7b class Tools { static func toast(msg:String,seconds:Double,vc:UIViewController){ \/\/\u5f97\u5230\u5f53\u524d UIViewController \u7684 \u5b89\u5168\u663e\u793a\u533a\u57df le<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[318,319],"tags":[322,320,321,323],"_links":{"self":[{"href":"http:\/\/www.youthtribe.com\/wp-json\/wp\/v2\/posts\/2023"}],"collection":[{"href":"http:\/\/www.youthtribe.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.youthtribe.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.youthtribe.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.youthtribe.com\/wp-json\/wp\/v2\/comments?post=2023"}],"version-history":[{"count":3,"href":"http:\/\/www.youthtribe.com\/wp-json\/wp\/v2\/posts\/2023\/revisions"}],"predecessor-version":[{"id":2030,"href":"http:\/\/www.youthtribe.com\/wp-json\/wp\/v2\/posts\/2023\/revisions\/2030"}],"wp:attachment":[{"href":"http:\/\/www.youthtribe.com\/wp-json\/wp\/v2\/media?parent=2023"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.youthtribe.com\/wp-json\/wp\/v2\/categories?post=2023"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.youthtribe.com\/wp-json\/wp\/v2\/tags?post=2023"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}