1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
- import Foundation
- extension String {
-
- func textToImage(size: CGFloat) -> UIImage? {
-
- let nsString = (self as NSString)
- let font = UIFont.systemFont(ofSize: size)
- let stringAttributes = [NSAttributedString.Key.font: font]
- let imageSize = nsString.size(withAttributes: stringAttributes)
- UIGraphicsBeginImageContextWithOptions(imageSize, false, 0)
- UIColor.clear.set()
- UIRectFill(CGRect(origin: CGPoint(), size: imageSize))
- nsString.draw(at: CGPoint.zero, withAttributes: stringAttributes)
- let image = UIGraphicsGetImageFromCurrentImageContext()
- UIGraphicsEndImageContext()
- return image ?? UIImage()
- }
- }
|