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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
| public extension JTIconFontProtocol { // MARK:- For String public func attributedString(size fontSize: CGFloat, attributes: [NSAttributedString.Key: Any]?) -> NSAttributedString? { guard let attributes = attributesWith(size: fontSize, attributes: attributes) else { return nil } return NSAttributedString(string: self.unicode, attributes: attributes) } public func attributedString(size fontSize: CGFloat, foregroundColor: UIColor? = nil, backgroundColor: UIColor? = nil) -> NSAttributedString? { var attributesCombine: [NSAttributedString.Key: Any] = [:] if let foregroundColor = foregroundColor { attributesCombine.updateValue(foregroundColor, forKey: NSAttributedString.Key.foregroundColor) } if let backgroundColor = backgroundColor { attributesCombine.updateValue(backgroundColor, forKey: NSAttributedString.Key.backgroundColor) } return attributedString(size: fontSize, attributes: attributesCombine) } private func attributesWith(size fontSize: CGFloat, attributes: [NSAttributedString.Key: Any]?) -> [NSAttributedString.Key: Any]? { guard let font = font(size: fontSize) else { return nil } var attributesCombine: [NSAttributedString.Key: Any] = self.attributes if let attributes = attributes { for attribute in attributes { attributesCombine.updateValue(attribute.value, forKey: attribute.key) } } attributesCombine.updateValue(font, forKey: NSAttributedString.Key.font) return attributesCombine } // MARK:- For image public func image(size fontSize: CGFloat, attributes: [NSAttributedString.Key: Any]?, isCircle: Bool = false) -> UIImage? { guard let imageString: NSAttributedString = attributedString(size: fontSize, attributes: attributes) else { return nil } let rect = imageString.boundingRect(with: CGSize(width: CGFloat(MAXFLOAT), height: fontSize), options: .usesLineFragmentOrigin, context: nil) let imageSize: CGSize = rect.size let screenScale: CGFloat = UIScreen.main.scale UIGraphicsBeginImageContextWithOptions(imageSize, false, screenScale) if isCircle { let ctx = UIGraphicsGetCurrentContext() let path = UIBezierPath(roundedRect: rect, byRoundingCorners: .allCorners, cornerRadii: CGSize(width: imageSize.width / 2.0, height: imageSize.width / 2.0)) ctx?.addPath(path.cgPath) ctx?.clip() imageString.draw(in: rect) ctx?.drawPath(using: .fillStroke) } else { imageString.draw(in: rect) } let image: UIImage? = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return image } public func image(size fontSize: CGFloat, foregroundColor: UIColor? = nil, backgroundColor: UIColor? = .clear, isCircle: Bool = false) -> UIImage? { var attributesCombine: [NSAttributedString.Key: Any] = [:] if let foregroundColor = foregroundColor { attributesCombine.updateValue(foregroundColor, forKey: NSAttributedString.Key.foregroundColor) } if let backgroundColor = backgroundColor { attributesCombine.updateValue(backgroundColor, forKey: NSAttributedString.Key.backgroundColor) } return image(size: fontSize, attributes: attributesCombine, isCircle: isCircle) } public func image(size imageSize: CGSize, attributes: [NSAttributedString.Key: Any]?, isCircle: Bool = false) -> UIImage? { guard let imageString: NSAttributedString = attributedString(size: 1, attributes: attributes) else { return nil } let rect = imageString.boundingRect(with: CGSize(width: CGFloat(MAXFLOAT), height: 1), options: .usesLineFragmentOrigin, context: nil) let widthScale = imageSize.width / rect.width let heightScale = imageSize.height / rect.height let scale = (widthScale < heightScale) ? widthScale : heightScale let scaledWidth = rect.width * scale let scaledHeight = rect.height * scale var anchorPoint = CGPoint.zero if widthScale < heightScale { anchorPoint.y = (imageSize.height - scaledHeight) / 2 } else if widthScale > heightScale { anchorPoint.x = (imageSize.width - scaledWidth) / 2 } var anchorRect = CGRect.zero anchorRect.origin = anchorPoint anchorRect.size.width = scaledWidth anchorRect.size.height = scaledHeight guard let imageStringScale: NSAttributedString = attributedString(size: scale, attributes: attributes) else { return nil } let screenScale: CGFloat = UIScreen.main.scale UIGraphicsBeginImageContextWithOptions(imageSize, false, screenScale) if isCircle { let ctx = UIGraphicsGetCurrentContext() let path = UIBezierPath(roundedRect: rect, byRoundingCorners: .allCorners, cornerRadii: CGSize(width: imageSize.width / 2.0, height: imageSize.width / 2.0)) ctx?.addPath(path.cgPath) ctx?.clip() imageString.draw(in: rect) ctx?.drawPath(using: .fillStroke) } else { imageString.draw(in: rect) } imageStringScale.draw(in: anchorRect) let image: UIImage? = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return image } public func image(size imageSize: CGSize, foregroundColor: UIColor? = nil, backgroundColor: UIColor? = .clear, isCircle: Bool = false) -> UIImage? { var attributesCombine: [NSAttributedString.Key: Any] = [:] if let foregroundColor = foregroundColor { attributesCombine.updateValue(foregroundColor, forKey: NSAttributedString.Key.foregroundColor) } if let backgroundColor = backgroundColor { attributesCombine.updateValue(backgroundColor, forKey: NSAttributedString.Key.backgroundColor) } return image(size: imageSize, attributes: attributesCombine, isCircle: isCircle) } }
|