{"id":1757,"url":"https://github.com/100mango/SwiftCssParser","last_synced_at":"2025-08-06T16:31:41.498Z","repository":{"id":78198576,"uuid":"92806935","full_name":"100mango/SwiftCssParser","owner":"100mango","description":"A Powerful , Extensible CSS Parser written in pure Swift.","archived":false,"fork":false,"pushed_at":"2019-06-12T08:21:07.000Z","size":2105,"stargazers_count":279,"open_issues_count":2,"forks_count":19,"subscribers_count":5,"default_branch":"master","last_synced_at":"2024-12-09T16:43:43.248Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/100mango.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2017-05-30T07:08:04.000Z","updated_at":"2024-11-19T08:22:55.000Z","dependencies_parsed_at":"2023-02-24T16:00:18.466Z","dependency_job_id":null,"html_url":"https://github.com/100mango/SwiftCssParser","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/100mango/SwiftCssParser","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/100mango%2FSwiftCssParser","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/100mango%2FSwiftCssParser/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/100mango%2FSwiftCssParser/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/100mango%2FSwiftCssParser/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/100mango","download_url":"https://codeload.github.com/100mango/SwiftCssParser/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/100mango%2FSwiftCssParser/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":269112520,"owners_count":24361990,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-06T02:00:09.910Z","response_time":99,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-01-05T20:15:55.085Z","updated_at":"2025-08-06T16:31:40.955Z","avatar_url":"https://github.com/100mango.png","language":"Swift","funding_links":[],"categories":["Parsing","Libs","UI [🔝](#readme)"],"sub_categories":["Other Parsing","UI","Other free courses"],"readme":"\u003cp align=\"center\"\u003e \u003cimg src=\"icon.jpeg\" /\u003e\n\u003cbr\u003e\n\n  ![](http://img.shields.io/badge/Swift-5.0-orange.svg)\n\n\n\nA Powerful , Extensible CSS Parser written in pure Swift.\n\n\n\n## Basic Usage\n\nFrom CSS:\n\n~~~css\n#View {\n \"width\" : 118;\n \"height\" : 120.5;\n \"color1\" : \"#888888\";\n \"color2\" : RGB(200,200,200);\n \"color3\" : RGB(200,200,200,0.5);\n \"font1\" : \"Helvetica-Bold\" 18;\n \"font2\" : \"Cochin\";\n \"size\" : 10 10;\n }\n~~~\n\nTo Cocoa:\n\n~~~swift\nlet width = css.int(selector: \"#View\", key: \"width\") // Int\nlet height = css.double(selector: \"#View\", key: \"height\") //Double\nlet color1 = css.color(selector: \"#View\", key: \"color1\") //UIColor\nlet font1  = css.font(selector: \"#View\", key: \"font1\") //UIFont\nlet font2 = css.font(selector: \"#View\", key: \"font2\", fontSize: 14) //UIFont\nlet size = testSwiftCSS.size(selector: \"#View\", key: \"size\") //CGsize\n~~~\n\n\n\nIt's very easy to setup and parse CSS with `SwiftCssParser`:\n\n~~~~swift\n//1.Get CSS file path\nlet path = Bundle.main.url(forResource: \"cssFileNmae\", withExtension: \"css\")\n//2.Get parsed CSS\nlet css = SwiftCSS(CssFileURL: path)\n//3.Use it\nlet width = css.int(selector: \"#View\", key: \"width\")\n~~~~\n\n\n\n## Extension\n\nIt's very easy to build your own Powerful, Flexiable CSS based solutions base on `SwiftCssParser`.\n\n#### Example1: `SwiftDeviceCss`\n\n​\tIn most cases, `Auto Layout` can help us calculates the size and location of our views. But in some cases, we need to set specifc size and location for our views based on device type (device's screen size) to accomplish the `Pixel Perfect` design.\n\n​\tSo, we can use `SwiftCssParser` to get layout value from CSS file. Different Device has different configuration file.\n\n~~~swift\npublic let SwiftDeviceCss = SwiftCssStyleSheet.deviceCss()\n\nclass SwiftCssStyleSheet {\n    \n    private enum ScreenSize {\n        case _320_480 //iPhone4 etc.\n        case _320_568 //iPhone5 etc.\n        //iPhone6....\n    }\n    \n    static private let screenSize: ScreenSize = {\n        let screen = UIScreen.main\n        let size = UIScreen.main.fixedCoordinateSpace.bounds.size\n        switch (size.width,size.height) {\n        case (320,640):\n        \treturn ._320_480\n        //......\n        }\n    }()\n    \n    static func deviceCss() -\u003e SwiftCSS {\n        switch self.screenSize {\n        case ._320_480:\n            return SwiftCSS(CssFileURL: URL.CssURL(name: \"iPhone4\"))\n        case ._320_568:\n            return SwiftCSS(CssFileURL: URL.CssURL(name: \"iPhone5\"))\n        //......\n        }\n    }\n    \n}\n~~~\n\nThen just layout: \n\n~~~swift\nview.frame.size = SwiftDeviceCss.size(selector: \"#View\", key: \"size\")\n~~~\n\n\n\n#### Exeample2: `SwiftCssTheme`\n\nWe can also create a powerful theme manager base on `SwiftCssParser`.\n\nFor example, we want to create a night \u0026 day theme.\n\n~~~swift\npublic class SwiftCssTheme {\n    \n    public static let updateThemeNotification = Notification.Name(\"SwiftCSSThemeUpdate\")\n    \n    public enum Theme {\n        case day\n        case night\n    }\n    \n    public static var theme: Theme = .day {\n        didSet {\n            switch theme {\n            case .day:\n                self.themeCSS = SwiftCSS(CssFileURL: URL.CssURL(name: \"day\"))\n            case .night:\n                self.themeCSS = SwiftCSS(CssFileURL: URL.CssURL(name: \"night\"))\n            }\n            NotificationCenter.default.post(name: updateThemeNotification, object: nil)\n        }\n    }\n    \n    public static var themeCSS = SwiftCSS(CssFileURL: URL.CssURL(name: \"day\"))\n}\n~~~\n\nIf we want to be able to dynamically modify the background color of UIView:\n\n~~~swift\nextension UIView {\n    \n    private struct AssociatedKeys {\n        static var selector = \"themeColorSelector\"\n        static var key = \"themeColorKey\"\n    }\n    \n    var backgroundColorCSS: (selector: String,key: String) {\n        get {\n        \tlet selector = //Use objc_getAssociatedObject to get value.....\n        \tlet key = //.....\n            return (selector,key)\n        }\n        \n        set {\n            let selector = newValue.selector\n            let key = newValue.key\n            \n            //Use objc_setAssociatedObject to set value......   \n            \n            NotificationCenter.default.addObserver(self, selector: #selector(_cssUpdateBackgroundColor), name: SwiftCssTheme.updateThemeNotification, object: nil)\n            \n            _cssUpdateBackgroundColor()\n        }\n    }\n    \n    private dynamic func _cssUpdateBackgroundColor() {\n        self.backgroundColor = SwiftCssTheme.themeCSS.color(selector: self.backgroundColorCSS.selector, key: self.backgroundColorCSS.key)\n    }\n}\n~~~\n\nThen, we just need to specify the background color's  CSS selector and key:\n\n~~~swift\nself.view.backgroundColorCSS = (\"#View\",\"color\")\n~~~\n\nChanging theme is even easier:\n\n~~~swift\n@IBAction func changeColor(_ sender: UIButton) {\n    if SwiftCssTheme.theme == .day {\n        SwiftCssTheme.theme = .night\n    } else {\n        SwiftCssTheme.theme = .day\n    }\n}\n~~~\n\n\n\n![](theme.gif)\n\n\n\n\n\n\n\nAll the code and demo can be found in the project. Feel free to download and experiment.  Advice and pull requests are welcome.\n\n\n## Installation\n\nCocoaPods:\n\n~~~\npod 'SwiftCssParser'\n~~~\n\n\n## License\n\n`SwiftCssParser` is under the MIT license.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F100mango%2FSwiftCssParser","html_url":"https://awesome.ecosyste.ms/projects/github.com%2F100mango%2FSwiftCssParser","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F100mango%2FSwiftCssParser/lists"}