feedless/ios/feedless/Utils.swift

29 lines
1002 B
Swift

//
// Utils.swift
// feedless
//
// Created by Rogerio Chaves on 04/05/20.
// Copyright © 2020 Rogerio Chaves. All rights reserved.
//
import UIKit
import SwiftUI
extension String {
func image() -> UIImage? {
let nsString = (self as NSString)
let font = UIFont.systemFont(ofSize: 16) // you can change your font size here
let stringAttributes = [NSAttributedString.Key.font: font]
let imageSize = nsString.size(withAttributes: stringAttributes)
UIGraphicsBeginImageContextWithOptions(imageSize, false, 0) // begin image context
UIColor.clear.set() // clear background
UIRectFill(CGRect(origin: CGPoint(), size: imageSize)) // set rect size
nsString.draw(at: CGPoint.zero, withAttributes: stringAttributes) // draw text within rect
let image = UIGraphicsGetImageFromCurrentImageContext() // create image from context
UIGraphicsEndImageContext() // end image context
return image ?? UIImage()
}
}