From abeefc83aee5640ba7e055de8754971ffa87cac7 Mon Sep 17 00:00:00 2001 From: Dion Crannitch Date: Wed, 14 Jun 2017 11:30:34 +1200 Subject: [PATCH 1/3] Add support for CATextLayer. --- Source/SwiftIcons.swift | 106 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) diff --git a/Source/SwiftIcons.swift b/Source/SwiftIcons.swift index c596a1d..6848c1d 100644 --- a/Source/SwiftIcons.swift +++ b/Source/SwiftIcons.swift @@ -721,6 +721,112 @@ public extension UIViewController { } } +public extension CATextLayer { + + /** + This function sets the icon to CATextLayer + + - Parameter icon: The icon for the CATextLayer + - Parameter iconSize: Size of the icon + - Parameter textColor: Color for the icon + - Parameter backgroundColor: Background color for the icon + + */ + public func setIcon(icon: FontType, iconSize: CGFloat, color: UIColor = .black, bgColor: UIColor = .clear) { + FontLoader.loadFontIfNeeded(fontType: icon) + + let iconFont = CTFontCreateWithName(icon.fontName() as CFString?, iconSize, nil) + string = icon.text + font = iconFont + fontSize = iconSize + foregroundColor = color.cgColor + backgroundColor = bgColor.cgColor + alignmentMode = kCAAlignmentCenter + } + + + /** + This function sets the icon to CATextLayer with text around it with different colors + + - Parameter prefixText: The text before the icon + - Parameter prefixTextColor: The color for the text before the icon + - Parameter icon: The icon + - Parameter iconColor: Color for the icon + - Parameter postfixText: The text after the icon + - Parameter postfixTextColor: The color for the text after the icon + - Parameter size: Size of the text + - Parameter iconSize: Size of the icon + + */ + public func setIcon(prefixText: String, prefixTextColor: UIColor = .black, icon: FontType?, iconColor: UIColor = .black, postfixText: String, postfixTextColor: UIColor = .black, size: CGFloat?, iconSize: CGFloat? = nil) { + string = nil + FontLoader.loadFontIfNeeded(fontType: icon!) + + let attrText = string as? NSAttributedString ?? NSAttributedString() + let startFont = attrText.length == 0 ? nil : attrText.attribute(NSFontAttributeName, at: 0, effectiveRange: nil) as? UIFont + let endFont = attrText.length == 0 ? nil : attrText.attribute(NSFontAttributeName, at: attrText.length - 1, effectiveRange: nil) as? UIFont + var textFont = font + if let f = startFont , f.fontName != icon?.fontName() { + textFont = f + } else if let f = endFont , f.fontName != icon?.fontName() { + textFont = f + } + let prefixTextAttributes = [NSFontAttributeName : textFont!, NSForegroundColorAttributeName: prefixTextColor] as [String : Any] + let prefixTextAttribured = NSMutableAttributedString(string: prefixText, attributes: prefixTextAttributes) + + if let iconText = icon?.text { + let iconFont = UIFont(name: (icon?.fontName())!, size: iconSize ?? size ?? fontSize)! + let iconAttributes = [NSFontAttributeName : iconFont, NSForegroundColorAttributeName: iconColor] + + let iconString = NSAttributedString(string: iconText, attributes: iconAttributes) + prefixTextAttribured.append(iconString) + } + let postfixTextAttributes = [NSFontAttributeName : textFont!, NSForegroundColorAttributeName: postfixTextColor] as [String : Any] + let postfixTextAttributed = NSAttributedString(string: postfixText, attributes: postfixTextAttributes) + prefixTextAttribured.append(postfixTextAttributed) + + string = prefixTextAttribured + alignmentMode = kCAAlignmentCenter + } + + /** + This function sets the icon to UILabel with text around it with different fonts & colors + + - Parameter prefixText: The text before the icon + - Parameter prefixTextFont: The font for the text before the icon + - Parameter prefixTextColor: The color for the text before the icon + - Parameter icon: The icon + - Parameter iconColor: Color for the icon + - Parameter postfixText: The text after the icon + - Parameter postfixTextFont: The font for the text after the icon + - Parameter postfixTextColor: The color for the text after the icon + - Parameter iconSize: Size of the icon + + */ + public func setIcon(prefixText: String, prefixTextFont: UIFont, prefixTextColor: UIColor = .black, icon: FontType?, iconColor: UIColor = .black, postfixText: String, postfixTextFont: UIFont, postfixTextColor: UIColor = .black, iconSize: CGFloat? = nil) { + string = nil + FontLoader.loadFontIfNeeded(fontType: icon!) + + let prefixTextAttributes = [NSFontAttributeName : prefixTextFont, NSForegroundColorAttributeName: prefixTextColor] + let prefixTextAttribured = NSMutableAttributedString(string: prefixText, attributes: prefixTextAttributes) + + if let iconText = icon?.text { + let iconFont = UIFont(name: (icon?.fontName())!, size: iconSize ?? fontSize)! + let iconAttributes = [NSFontAttributeName : iconFont, NSForegroundColorAttributeName: iconColor] + + let iconString = NSAttributedString(string: iconText, attributes: iconAttributes) + prefixTextAttribured.append(iconString) + } + + let postfixTextAttributes = [NSFontAttributeName : postfixTextFont, NSForegroundColorAttributeName: postfixTextColor] + let postfixTextAttributed = NSAttributedString(string: postfixText, attributes: postfixTextAttributes) + prefixTextAttribured.append(postfixTextAttributed) + + string = prefixTextAttribured + alignmentMode = kCAAlignmentCenter + } +} + private class FontLoader { /** This utility function helps loading the font if not loaded already From 899fb836743c8ff09d1dda8377713853d2d2e5aa Mon Sep 17 00:00:00 2001 From: Dion Crannitch Date: Wed, 14 Jun 2017 11:35:15 +1200 Subject: [PATCH 2/3] Fixed typos. --- Source/SwiftIcons.swift | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Source/SwiftIcons.swift b/Source/SwiftIcons.swift index 6848c1d..4529673 100644 --- a/Source/SwiftIcons.swift +++ b/Source/SwiftIcons.swift @@ -772,20 +772,20 @@ public extension CATextLayer { textFont = f } let prefixTextAttributes = [NSFontAttributeName : textFont!, NSForegroundColorAttributeName: prefixTextColor] as [String : Any] - let prefixTextAttribured = NSMutableAttributedString(string: prefixText, attributes: prefixTextAttributes) + let prefixTextAttributed = NSMutableAttributedString(string: prefixText, attributes: prefixTextAttributes) if let iconText = icon?.text { let iconFont = UIFont(name: (icon?.fontName())!, size: iconSize ?? size ?? fontSize)! let iconAttributes = [NSFontAttributeName : iconFont, NSForegroundColorAttributeName: iconColor] let iconString = NSAttributedString(string: iconText, attributes: iconAttributes) - prefixTextAttribured.append(iconString) + prefixTextAttributed.append(iconString) } let postfixTextAttributes = [NSFontAttributeName : textFont!, NSForegroundColorAttributeName: postfixTextColor] as [String : Any] let postfixTextAttributed = NSAttributedString(string: postfixText, attributes: postfixTextAttributes) - prefixTextAttribured.append(postfixTextAttributed) + prefixTextAttributed.append(postfixTextAttributed) - string = prefixTextAttribured + string = prefixTextAttributed alignmentMode = kCAAlignmentCenter } From 1a5a23020f7ef95361c8cdabe38a41f2ec2062a1 Mon Sep 17 00:00:00 2001 From: Dion Crannitch Date: Fri, 8 Feb 2019 11:24:22 +1300 Subject: [PATCH 3/3] Fixed build for Swift 4.2. --- Source/SwiftIcons.swift | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/Source/SwiftIcons.swift b/Source/SwiftIcons.swift index 4529673..7845e8a 100644 --- a/Source/SwiftIcons.swift +++ b/Source/SwiftIcons.swift @@ -735,13 +735,13 @@ public extension CATextLayer { public func setIcon(icon: FontType, iconSize: CGFloat, color: UIColor = .black, bgColor: UIColor = .clear) { FontLoader.loadFontIfNeeded(fontType: icon) - let iconFont = CTFontCreateWithName(icon.fontName() as CFString?, iconSize, nil) + let iconFont = CTFontCreateWithName(icon.fontName() as CFString, iconSize, nil) string = icon.text font = iconFont fontSize = iconSize foregroundColor = color.cgColor backgroundColor = bgColor.cgColor - alignmentMode = kCAAlignmentCenter + alignmentMode = .center } @@ -763,30 +763,30 @@ public extension CATextLayer { FontLoader.loadFontIfNeeded(fontType: icon!) let attrText = string as? NSAttributedString ?? NSAttributedString() - let startFont = attrText.length == 0 ? nil : attrText.attribute(NSFontAttributeName, at: 0, effectiveRange: nil) as? UIFont - let endFont = attrText.length == 0 ? nil : attrText.attribute(NSFontAttributeName, at: attrText.length - 1, effectiveRange: nil) as? UIFont + let startFont = attrText.length == 0 ? nil : attrText.attribute(.font, at: 0, effectiveRange: nil) as? UIFont + let endFont = attrText.length == 0 ? nil : attrText.attribute(.font, at: attrText.length - 1, effectiveRange: nil) as? UIFont var textFont = font if let f = startFont , f.fontName != icon?.fontName() { textFont = f } else if let f = endFont , f.fontName != icon?.fontName() { textFont = f } - let prefixTextAttributes = [NSFontAttributeName : textFont!, NSForegroundColorAttributeName: prefixTextColor] as [String : Any] + let prefixTextAttributes: [NSAttributedString.Key : Any] = [.font : textFont!, .foregroundColor: prefixTextColor] let prefixTextAttributed = NSMutableAttributedString(string: prefixText, attributes: prefixTextAttributes) if let iconText = icon?.text { let iconFont = UIFont(name: (icon?.fontName())!, size: iconSize ?? size ?? fontSize)! - let iconAttributes = [NSFontAttributeName : iconFont, NSForegroundColorAttributeName: iconColor] + let iconAttributes = [.font : iconFont, .foregroundColor: iconColor] as [NSAttributedString.Key : Any] let iconString = NSAttributedString(string: iconText, attributes: iconAttributes) prefixTextAttributed.append(iconString) } - let postfixTextAttributes = [NSFontAttributeName : textFont!, NSForegroundColorAttributeName: postfixTextColor] as [String : Any] + let postfixTextAttributes: [NSAttributedString.Key : Any] = [.font : textFont!, .foregroundColor: postfixTextColor] let postfixTextAttributed = NSAttributedString(string: postfixText, attributes: postfixTextAttributes) prefixTextAttributed.append(postfixTextAttributed) string = prefixTextAttributed - alignmentMode = kCAAlignmentCenter + alignmentMode = .center } /** @@ -807,23 +807,23 @@ public extension CATextLayer { string = nil FontLoader.loadFontIfNeeded(fontType: icon!) - let prefixTextAttributes = [NSFontAttributeName : prefixTextFont, NSForegroundColorAttributeName: prefixTextColor] + let prefixTextAttributes: [NSAttributedString.Key : Any] = [.font : prefixTextFont, .foregroundColor: prefixTextColor] let prefixTextAttribured = NSMutableAttributedString(string: prefixText, attributes: prefixTextAttributes) if let iconText = icon?.text { let iconFont = UIFont(name: (icon?.fontName())!, size: iconSize ?? fontSize)! - let iconAttributes = [NSFontAttributeName : iconFont, NSForegroundColorAttributeName: iconColor] + let iconAttributes: [NSAttributedString.Key : Any] = [.font : iconFont, .foregroundColor: iconColor] let iconString = NSAttributedString(string: iconText, attributes: iconAttributes) prefixTextAttribured.append(iconString) } - let postfixTextAttributes = [NSFontAttributeName : postfixTextFont, NSForegroundColorAttributeName: postfixTextColor] + let postfixTextAttributes: [NSAttributedString.Key : Any] = [NSAttributedString.Key.font : postfixTextFont, NSAttributedString.Key.foregroundColor: postfixTextColor] let postfixTextAttributed = NSAttributedString(string: postfixText, attributes: postfixTextAttributes) prefixTextAttribured.append(postfixTextAttributed) string = prefixTextAttribured - alignmentMode = kCAAlignmentCenter + alignmentMode = .center } }