Last active
November 12, 2018 19:37
-
-
Save mayoff/b3ebe6b647d76e458732 to your computer and use it in GitHub Desktop.
Playground for drawing a UILabel with attributed text containing icons from a custom font
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "appfont", | |
"css_prefix_text": "icon-", | |
"css_use_suffix": false, | |
"hinting": true, | |
"units_per_em": 1000, | |
"ascent": 850, | |
"glyphs": [ | |
{ | |
"uid": "324e9013e79ecf9e0e30a07feffa9d6b", | |
"css": "champagne-glass", | |
"code": 59393, | |
"src": "custom_icons", | |
"selected": true, | |
"svg": { | |
"path": "M644.4 140.9L475.3 93.1C426.7 249.1 365.5 366.3 423.3 545.8 417 687 371.7 826.7 341.7 827.1 302 827.7 255.2 816.4 257.2 835.2L256.2 849.5C275.6 864.9 305.1 879.4 363.1 899.7 430.1 913.3 452.9 911.4 482.4 911.8L487.9 896C495.1 874.9 470.2 871.6 417.1 846.1 382.9 829.6 424 667.1 482 552.8 608.9 435.5 613.9 258.5 644.4 140.9L644.4 140.9 644.4 140.9ZM829.2 52.7L647.8 98.6C686 268.3 691.9 408.9 840.8 543.6 910.2 677.2 942.8 830.1 915.4 846.5 879 868.2 829.9 882.7 841.7 898.9L848.5 912.7C874.5 916.6 909.4 914.2 973.7 902 1042.7 878.9 1062.8 865 1090.2 849.7L1086.8 832.2C1082.3 808.9 1057.6 819.1 994.9 823.8 954.7 826.9 906.1 655.1 898.7 518.9 953.3 343.2 863.7 177.3 829.2 52.7L829.2 52.7 829.2 52.7 829.2 52.7 829.2 52.7Z", | |
"width": 1333 | |
}, | |
"search": [ | |
"champagne-glass" | |
] | |
}, | |
{ | |
"uid": "dc323f7b48116cca695188f44df21ef0", | |
"css": "record3", | |
"code": 59394, | |
"src": "custom_icons", | |
"selected": true, | |
"svg": { | |
"path": "M399.6 553.5C370 553.5 346.1 529.5 346.1 500 346.1 470.4 370 446.5 399.6 446.5 429.1 446.5 453.1 470.5 453.1 500 453.1 529.5 429.1 553.5 399.6 553.5ZM1000 856.4H580.1C525.9 884 464.6 899.6 399.6 899.6 334.6 899.6 273.3 884 219.1 856.4H92V755C34.5 685.8 0 597 0 500S34.5 314.2 92 245V144.4H217.5C272.1 116.3 334 100.4 399.6 100.4 465.2 100.4 527.1 116.3 581.7 144.4H1000L1000 856.4 1000 856.4ZM120.7 173.1V213.9C136 199 152.4 185.4 169.9 173.1H120.7ZM120.7 446.5C133.7 392.7 159.1 343.6 193.8 302.7 189.9 298.8 185.9 294.9 182 290.9 156.4 320.9 135.7 355.1 120.7 392.3L120.7 446.5 120.7 446.5ZM152.5 528H169.1C169.1 458.1 193.8 394 234.9 343.8L223.1 332C179 385.3 152.5 453.6 152.5 528ZM399.6 658.5C487.1 658.5 558.1 587.5 558.1 500 558.1 412.5 487.1 341.5 399.6 341.5 312.1 341.5 241.1 412.5 241.1 500 241.1 587.5 312.1 658.5 399.6 658.5ZM623.1 723.6C677.6 659.7 710.6 577 710.6 486.6H694C694 572.4 662.8 651 611.3 711.9 615.3 715.8 619.2 719.7 623.1 723.6ZM652.6 486.6H636C636 556.4 611.3 620.5 570.2 670.7L582 682.5C626.1 629.3 652.6 561 652.6 486.6ZM171.1 827.7C153.2 815.2 136.3 801.3 120.7 786.1V827.7H171.1ZM971.3 173.1H629.2C732 245.4 799.2 364.8 799.2 500 799.2 635.6 731.5 755.5 628.1 827.7H971.3V173.1H971.3ZM830.6 700C838.6 700 845.9 702.2 852.4 705.8L851.4 704.8 898.8 651.4V331.2H878.1V225.8H948.3V331.2H927.6V662.3L872.8 723.9 868.4 720C873.5 727.3 876.5 736.3 876.5 745.8 876.5 771.1 855.9 791.7 830.6 791.7 805.3 791.7 784.8 771.1 784.8 745.8S805.4 700 830.6 700ZM830.6 728.7C821.2 728.7 813.5 736.4 813.5 745.8 813.5 755.2 821.2 762.9 830.6 762.9 840.1 762.9 847.8 755.2 847.8 745.8 847.8 736.4 840.1 728.7 830.6 728.7Z", | |
"width": 1000 | |
}, | |
"search": [ | |
"record3" | |
] | |
}, | |
{ | |
"uid": "501f33ed61da407feb057a7699ef93f7", | |
"css": "clock", | |
"code": 59392, | |
"src": "websymbols" | |
} | |
] | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import XCPlayground | |
import Foundation | |
import UIKit | |
import CoreImage | |
import CoreData | |
import MobileCoreServices | |
// Record player icon by Freepik from www.flaticon.com is licensed under CC BY 3.0 http://creativecommons.org/licenses/by/3.0/ | |
// Champagne icon by Pilar, from clker.com: http://www.clker.com/clipart-champagne-glass-.html | |
// Clock icon by Студия Just Be Nice: http://www.justbenice.ru/studio/websymbols/ | |
// appfont.ttf assembled by http://fontello.com/ | |
let appFontURL = NSBundle.mainBundle().URLForResource("appfont", withExtension: "ttf")! | |
let appFontData = NSData(contentsOfURL: appFontURL)! | |
let appFontDescriptor = CTFontManagerCreateFontDescriptorFromData(appFontData)! | |
let baseFontDescriptor = CTFontDescriptorCreateWithNameAndSize("Papyrus", 18) | |
let fontAttributes: [NSString:AnyObject] = [ | |
kCTFontCascadeListAttribute : [ baseFontDescriptor ] | |
] | |
let fontDescriptor = CTFontDescriptorCreateCopyWithAttributes(appFontDescriptor, fontAttributes) | |
let font = CTFontCreateWithFontDescriptor(fontDescriptor, 18, nil) as UIFont | |
let recordPlayer = "\u{E802}" | |
let champagneGlasses = "\u{E801}" | |
let clock = "\u{E800}" | |
let nonBreakingSpace = "\u{00A0}" | |
let music1 = "Electro, Disco, 80's" | |
let music2 = "Jazz, Latino, Rock and roll, Electro, Beat Music" | |
let occasion = "After Office" | |
let time = "9:00 PM - 2:00 AM" | |
let text1 = "\(recordPlayer) \(music1) \(champagneGlasses)\(nonBreakingSpace)\(occasion)\n\(clock) \(time)" | |
let text2 = "\(recordPlayer) \(music2) \(champagneGlasses)\(nonBreakingSpace)\(occasion)\n\(clock) \(time)" | |
let text = text1 + "\n\n\n" + text2 | |
let paragraphStyle = NSMutableParagraphStyle() | |
paragraphStyle.headIndent = 25 | |
let richText = NSAttributedString(string: text, attributes: [ | |
NSFontAttributeName: font, | |
NSParagraphStyleAttributeName: paragraphStyle | |
]) | |
let label = UILabel(frame: CGRectMake(0, 0, 320, 0)) | |
label.backgroundColor = UIColor.whiteColor() | |
label.attributedText = richText | |
label.numberOfLines = 0 | |
label.bounds.size.height = label.sizeThatFits(CGSizeMake(label.bounds.size.width, CGFloat.infinity)).height | |
UIGraphicsBeginImageContextWithOptions(label.bounds.size, true, 1) | |
label.drawViewHierarchyInRect(label.bounds, afterScreenUpdates: true) | |
let image = UIGraphicsGetImageFromCurrentImageContext() | |
UIGraphicsEndImageContext() | |
image |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See http://stackoverflow.com/a/34242689/77567 for an explanation of this code, and to see the result.