├── .gitignore ├── BadgesGenerator.playground ├── Contents.swift ├── Resources │ ├── Calibri-Bold.ttf │ ├── Calibri.ttf │ ├── attendees.json │ └── badge-background.png ├── Sources │ ├── Attendee.swift │ ├── Rendering.swift │ └── Utils.swift └── contents.xcplayground ├── LICENSE ├── README.md └── screenshot.png /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | # 3 | # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore 4 | 5 | ## Build generated 6 | build/ 7 | DerivedData/ 8 | 9 | ## Various settings 10 | *.pbxuser 11 | !default.pbxuser 12 | *.mode1v3 13 | !default.mode1v3 14 | *.mode2v3 15 | !default.mode2v3 16 | *.perspectivev3 17 | !default.perspectivev3 18 | xcuserdata/ 19 | 20 | ## Other 21 | *.moved-aside 22 | *.xcuserstate 23 | 24 | ## Obj-C/Swift specific 25 | *.hmap 26 | *.ipa 27 | *.dSYM.zip 28 | *.dSYM 29 | 30 | ## Playgrounds 31 | timeline.xctimeline 32 | playground.xcworkspace 33 | 34 | # Swift Package Manager 35 | # 36 | # Add this line if you want to avoid checking in source code from Swift Package Manager dependencies. 37 | # Packages/ 38 | .build/ 39 | 40 | # CocoaPods 41 | # 42 | # We recommend against adding the Pods directory to your .gitignore. However 43 | # you should judge for yourself, the pros and cons are mentioned at: 44 | # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control 45 | # 46 | # Pods/ 47 | 48 | # Carthage 49 | # 50 | # Add this line if you want to avoid checking in source code from Carthage dependencies. 51 | # Carthage/Checkouts 52 | 53 | Carthage/Build 54 | 55 | # fastlane 56 | # 57 | # It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the 58 | # screenshots whenever they are needed. 59 | # For more information about the recommended setup visit: 60 | # https://github.com/fastlane/fastlane/blob/master/fastlane/docs/Gitignore.md 61 | 62 | fastlane/report.xml 63 | fastlane/Preview.html 64 | fastlane/screenshots 65 | fastlane/test_output 66 | -------------------------------------------------------------------------------- /BadgesGenerator.playground/Contents.swift: -------------------------------------------------------------------------------- 1 | import UIKit 2 | 3 | CTFontManagerRegisterFontsForURL(fontURL as CFURL, CTFontManagerScope.process, nil) 4 | CTFontManagerRegisterFontsForURL(fontBoldURL as CFURL, CTFontManagerScope.process, nil) 5 | 6 | // Start rendering all attendees from JSON 7 | print("👉🏻 Output directory: " + documentsDirectory.path) 8 | Attendee.all().forEach { 9 | print("✅ Badge generated for \($0.firstName) \($0.lastName)") 10 | write(render($0), name: "\($0.firstName)-\($0.lastName)") 11 | } 12 | -------------------------------------------------------------------------------- /BadgesGenerator.playground/Resources/Calibri-Bold.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BalestraPatrick/BadgesGenerator/72405f5c8e371f6ec22b1f4dccc92a2eafbf1683/BadgesGenerator.playground/Resources/Calibri-Bold.ttf -------------------------------------------------------------------------------- /BadgesGenerator.playground/Resources/Calibri.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BalestraPatrick/BadgesGenerator/72405f5c8e371f6ec22b1f4dccc92a2eafbf1683/BadgesGenerator.playground/Resources/Calibri.ttf -------------------------------------------------------------------------------- /BadgesGenerator.playground/Resources/attendees.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "first_name": "Patrick", 4 | "last_name": "Balestra", 5 | "company": "Scandit", 6 | "twitter": "@BalestraPatrick", 7 | "country": "CH" 8 | } 9 | ] 10 | 11 | -------------------------------------------------------------------------------- /BadgesGenerator.playground/Resources/badge-background.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BalestraPatrick/BadgesGenerator/72405f5c8e371f6ec22b1f4dccc92a2eafbf1683/BadgesGenerator.playground/Resources/badge-background.png -------------------------------------------------------------------------------- /BadgesGenerator.playground/Sources/Attendee.swift: -------------------------------------------------------------------------------- 1 | import Foundation 2 | 3 | public struct Attendee { 4 | public let firstName: String 5 | public let lastName: String 6 | public let company: String 7 | public let twitter: String 8 | public let country: String 9 | } 10 | 11 | extension Attendee { 12 | 13 | public init(dictionary: [String: AnyObject]) { 14 | self.firstName = dictionary["first_name"] as! String 15 | self.lastName = dictionary["last_name"] as! String 16 | self.company = dictionary["company"] as! String 17 | self.twitter = dictionary["twitter"] as! String 18 | self.country = dictionary["country"] as! String 19 | } 20 | 21 | public static func all() -> [Attendee] { 22 | var attendees = [Attendee]() 23 | if let filePath = Bundle.main.path(forResource: "attendees", ofType: "json") { 24 | do { 25 | let data = try Data(contentsOf: URL(fileURLWithPath: filePath), options: []) 26 | let attendeesJSON = try? JSONSerialization.jsonObject(with: data, options: .allowFragments) 27 | if let attendeesArray = attendeesJSON as? [[String: AnyObject]] { 28 | attendeesArray.forEach { 29 | let attendee = Attendee(dictionary: $0) 30 | attendees.append(attendee) 31 | } 32 | } 33 | } catch { 34 | print("❌ Error parsing JSON file.") 35 | } 36 | } else { 37 | print("❌ No JSON file found with the given name.") 38 | } 39 | return attendees 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /BadgesGenerator.playground/Sources/Rendering.swift: -------------------------------------------------------------------------------- 1 | import UIKit 2 | 3 | public func render(_ attendee: Attendee) -> Data { 4 | let renderer = UIGraphicsImageRenderer(bounds: CGRect(origin: .zero, size: CGSize(width: 1250, height: 1965))) 5 | let image = renderer.image { context in 6 | let rect = context.format.bounds 7 | UIColor.blue.setFill() 8 | context.fill(rect) 9 | #imageLiteral(resourceName: "badge-background.png").draw(in: rect) 10 | 11 | do { 12 | let fontSize = CGFloat(185) 13 | let attributes = [NSAttributedStringKey.font: UIFont(name: calibriBold, size: fontSize)!, NSAttributedStringKey.paragraphStyle: paragraphStyle, NSAttributedStringKey.foregroundColor: #colorLiteral(red: 0.2470588235, green: 0.2470588235, blue: 0.2470588235, alpha: 1)] 14 | let string = "\(attendee.firstName)\n\(attendee.lastName)" 15 | let attributedString = NSAttributedString(string: string, attributes: attributes) 16 | let height = attributedString.boundingRect(with: CGSize(width: rect.width, height: 750), options: [.usesLineFragmentOrigin], context: nil).height 17 | let lines = Int(height / fontSize) 18 | let yPosition: CGFloat 19 | if lines == 1 { 20 | yPosition = CGFloat(950) 21 | } else if lines == 2 { 22 | yPosition = CGFloat(875) 23 | } else { 24 | yPosition = CGFloat(800) 25 | } 26 | attributedString.draw(with: CGRect(x: 0, y: yPosition, width: rect.width, height: rect.height), options: .usesLineFragmentOrigin, context: nil) 27 | } 28 | 29 | do { 30 | let attributes = [NSAttributedStringKey.font: UIFont(name: calibri, size: 125)!, NSAttributedStringKey.paragraphStyle: paragraphStyle, NSAttributedStringKey.foregroundColor: #colorLiteral(red: 0.1843137255, green: 0.1843137255, blue: 0.1843137255, alpha: 0.7)] 31 | let string = attendee.company 32 | let attributedString = NSAttributedString(string: string, attributes: attributes) 33 | attributedString.draw(with: CGRect(x: 0, y: 1325, width: rect.width, height: rect.height), options: .usesLineFragmentOrigin, context: nil) 34 | } 35 | 36 | do { 37 | let attributes = [NSAttributedStringKey.font: UIFont(name: calibri, size: 150)!, NSAttributedStringKey.paragraphStyle: paragraphStyle, NSAttributedStringKey.foregroundColor: #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)] 38 | let string = flag(country: attendee.country) 39 | let attributedString = NSAttributedString(string: string, attributes: attributes) 40 | attributedString.draw(with: CGRect(x: 0, y: 1500, width: rect.width, height: rect.height), options: .usesLineFragmentOrigin, context: nil) 41 | } 42 | 43 | do { 44 | let attributes = [NSAttributedStringKey.font: UIFont(name: calibri, size: 125)!, NSAttributedStringKey.paragraphStyle: paragraphStyle, NSAttributedStringKey.foregroundColor: #colorLiteral(red: 0.1843137255, green: 0.1843137255, blue: 0.1843137255, alpha: 0.7)] 45 | let string = attendee.twitter 46 | let attributedString = NSAttributedString(string: string, attributes: attributes) 47 | attributedString.draw(with: CGRect(x: 0, y: 1750, width: rect.width, height: rect.height), options: .usesLineFragmentOrigin, context: nil) 48 | } 49 | } 50 | return UIImageJPEGRepresentation(image, 1)! 51 | } 52 | -------------------------------------------------------------------------------- /BadgesGenerator.playground/Sources/Utils.swift: -------------------------------------------------------------------------------- 1 | import UIKit 2 | 3 | let calibri = "Calibri" 4 | let calibriBold = "Calibri-Bold" 5 | public let fontURL = Bundle.main.url(forResource: calibri, withExtension: "ttf")! 6 | public let fontBoldURL = Bundle.main.url(forResource: calibriBold, withExtension: "ttf")! 7 | 8 | public let paragraphStyle: NSParagraphStyle = { 9 | let paragraph = NSMutableParagraphStyle() 10 | paragraph.alignment = .center 11 | return paragraph 12 | }() 13 | 14 | public let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first! 15 | 16 | let directoryName = "AppBuildersBadges" 17 | let directory = documentsDirectory.appendingPathComponent(directoryName) 18 | 19 | public func write(_ data: Data, name: String) { 20 | // Create directory if it doesn't exist 21 | try? FileManager.default.createDirectory(atPath: directory.path, withIntermediateDirectories: false, attributes: nil) 22 | do { 23 | try data.write(to: directory.appendingPathComponent("\(name).jpg"), options: []) 24 | } 25 | catch { 26 | print(error) 27 | } 28 | } 29 | 30 | 31 | public func flag(country: String) -> String { 32 | let base: UInt32 = 127397 33 | var s = "" 34 | for v in country.unicodeScalars { 35 | s.unicodeScalars.append(UnicodeScalar(base + v.value)!) 36 | } 37 | return String(s) 38 | } 39 | -------------------------------------------------------------------------------- /BadgesGenerator.playground/contents.xcplayground: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Patrick Balestra 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # App Builders Badge Generator 2 | 3 | ![](screenshot.png) 4 | 5 | After manually designing hundreds of personalized badges for [App Builders 2016](http://2016.appbuilders.ch) and [The Swift Alps](http://theswiftalps.com), I knew I had to find a faster way. 6 | 7 | This playground takes care of rendering all personalized badges using the new `UIGraphicsRenderer` API available in iOS 10. Just provide your own JSON file with the parameters that you want to customize such as first name, last name, company name, twitter name and country of birth. 8 | 9 | The playground will generate the JPG images in the directory of the Playground (which is logged in the debbuger for your convenience). 10 | 11 | ## Usage 12 | Feel free to use the code for your own conference bagdes generation. Modify the `attendees.json` with your attendees data. 13 | If you want to add or remove fields from the badge, modify the struct `Attendee`. To modify the design of the badge, check out the `Rendering` class. 14 | You can also import a custom font to use in your drawing context. 15 | 16 | Let me know if you used this project for your own event, I'd love to hear from you! 17 | 18 | ## Author 19 | 20 | I'm [Patrick Balestra](http://www.patrickbalestra.com). 21 | Email: [me@patrickbalestra.com](mailto:me@patrickbalestra.com) 22 | Twitter: [@BalestraPatrick](http://twitter.com/BalestraPatrick). 23 | 24 | ## License 25 | 26 | `BadgesGenerator` is available under the MIT license. See the [LICENSE](LICENSE) file for more info. 27 | -------------------------------------------------------------------------------- /screenshot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BalestraPatrick/BadgesGenerator/72405f5c8e371f6ec22b1f4dccc92a2eafbf1683/screenshot.png --------------------------------------------------------------------------------