-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathImageFromText.swift
30 lines (27 loc) · 929 Bytes
/
ImageFromText.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
//
// ImageFromText.swift
// SwipeableCell
//
// Created by Daniel Vebman on 1/14/17.
// Copyright © 2017 Daniel Vebman. All rights reserved.
//
import Foundation
import UIKit
/// Returns an image with the given text and font (`pointSize` does not matter)
/**
- parameters:
- string: The text to be made into an image
- font: The font with which the text should be displayed. If `font` is `nil`, the system font will be used.
*/
func image(from string: String, with font: UIFont?) -> UIImage {
let label = UILabel()
label.text = string
label.font = font ?? UIFont.systemFont(ofSize: 1000)
label.font = UIFont(name: label.font!.fontName, size: 1000)
label.sizeToFit()
UIGraphicsBeginImageContext(label.frame.size)
label.layer.render(in: UIGraphicsGetCurrentContext()!)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image!
}