Skip to content

Commit

Permalink
Updaded to Swift 4. (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
bernaferrari authored and bachonk committed Nov 15, 2017
1 parent edaaa45 commit f54157c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 14 deletions.
16 changes: 8 additions & 8 deletions InitialsImageView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,24 @@ let kGradientBotomOffset: HSVOffset = (hue: 0.025, saturation: -0.05, brightness

extension UIImageView {

public func setImageForName(string: String, backgroundColor: UIColor? = nil, circular: Bool, textAttributes: [String: AnyObject]?, gradient: Bool = false) {
public func setImageForName(string: String, backgroundColor: UIColor? = nil, circular: Bool, textAttributes: [NSAttributedStringKey: AnyObject]?, gradient: Bool = false) {

setImageForName(string: string, backgroundColor: backgroundColor, circular: circular, textAttributes: textAttributes, gradient: gradient, gradientColors: nil)
}

public func setImageForName(string: String, gradientColors: GradientColors, circular: Bool, textAttributes: [String: AnyObject]?) {
public func setImageForName(string: String, gradientColors: GradientColors, circular: Bool, textAttributes: [NSAttributedStringKey: AnyObject]?) {

setImageForName(string: string, backgroundColor: nil, circular: circular, textAttributes: textAttributes, gradient: true, gradientColors: gradientColors)
}

private func setImageForName(string: String, backgroundColor: UIColor?, circular: Bool, textAttributes: [String: AnyObject]?, gradient: Bool = false, gradientColors: GradientColors?) {
private func setImageForName(string: String, backgroundColor: UIColor?, circular: Bool, textAttributes: [NSAttributedStringKey: AnyObject]?, gradient: Bool = false, gradientColors: GradientColors?) {

let initials: String = initialsFromString(string: string)
let color: UIColor = (backgroundColor != nil) ? backgroundColor! : randomColor(for: string)
let gradientColors = gradientColors ?? topAndBottomColors(for: color)
let attributes: [String: AnyObject] = (textAttributes != nil) ? textAttributes! : [
NSFontAttributeName: self.fontForFontName(name: nil),
NSForegroundColorAttributeName: UIColor.white
let attributes: [NSAttributedStringKey: AnyObject] = (textAttributes != nil) ? textAttributes! : [
NSAttributedStringKey.font: self.fontForFontName(name: nil),
NSAttributedStringKey.foregroundColor: UIColor.white
]

self.image = imageSnapshot(text: initials, backgroundColor: color, circular: circular, textAttributes: attributes, gradient: gradient, gradientColors: gradientColors)
Expand All @@ -55,7 +55,7 @@ extension UIImageView {

}

private func imageSnapshot(text imageText: String, backgroundColor: UIColor, circular: Bool, textAttributes: [String : AnyObject], gradient: Bool, gradientColors: GradientColors) -> UIImage {
private func imageSnapshot(text imageText: String, backgroundColor: UIColor, circular: Bool, textAttributes: [NSAttributedStringKey : AnyObject], gradient: Bool, gradientColors: GradientColors) -> UIImage {

let scale: CGFloat = UIScreen.main.scale

Expand Down Expand Up @@ -97,7 +97,7 @@ extension UIImageView {
}

// Draw text in the context
let textSize: CGSize = imageText.size(attributes: textAttributes)
let textSize: CGSize = imageText.size(withAttributes: textAttributes)
let bounds: CGRect = self.bounds

imageText.draw(in: CGRect(x: bounds.midX - textSize.width / 2,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
C874E1AF1E871A33003BC36D /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = C874E1A81E871A33003BC36D /* Main.storyboard */; };
C874E1B11E871A33003BC36D /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = C874E1AB1E871A33003BC36D /* ViewController.swift */; };
C874E1B41E871B01003BC36D /* InitialsImageView.swift in Sources */ = {isa = PBXBuildFile; fileRef = C874E1B31E871B01003BC36D /* InitialsImageView.swift */; };
C874E1B51E871D8D003BC36D /* Info.plist in Resources */ = {isa = PBXBuildFile; fileRef = C874E1AA1E871A33003BC36D /* Info.plist */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand Down Expand Up @@ -103,12 +102,13 @@
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 0820;
LastUpgradeCheck = 0820;
LastUpgradeCheck = 0900;
ORGANIZATIONNAME = "Thomas Bachant";
TargetAttributes = {
C84E0BC41E3D1B42008A5CC4 = {
CreatedOnToolsVersion = 8.2.1;
DevelopmentTeam = BRX3VWK6LR;
LastSwiftMigration = 0900;
ProvisioningStyle = Automatic;
};
};
Expand Down Expand Up @@ -136,7 +136,6 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
C874E1B51E871D8D003BC36D /* Info.plist in Resources */,
C874E1AF1E871A33003BC36D /* Main.storyboard in Resources */,
C874E1AD1E871A33003BC36D /* Assets.xcassets in Resources */,
C874E1AE1E871A33003BC36D /* LaunchScreen.storyboard in Resources */,
Expand Down Expand Up @@ -187,15 +186,21 @@
CLANG_CXX_LIBRARY = "libc++";
CLANG_ENABLE_MODULES = YES;
CLANG_ENABLE_OBJC_ARC = YES;
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_COMMA = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
CLANG_WARN_STRICT_PROTOTYPES = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
Expand Down Expand Up @@ -237,15 +242,21 @@
CLANG_CXX_LIBRARY = "libc++";
CLANG_ENABLE_MODULES = YES;
CLANG_ENABLE_OBJC_ARC = YES;
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_COMMA = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
CLANG_WARN_STRICT_PROTOTYPES = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
Expand Down Expand Up @@ -280,7 +291,8 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.coincidentalcode.InitialsImageViewSampleSwift;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
SWIFT_SWIFT3_OBJC_INFERENCE = Off;
SWIFT_VERSION = 4.0;
};
name = Debug;
};
Expand All @@ -293,7 +305,8 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.coincidentalcode.InitialsImageViewSampleSwift;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
SWIFT_SWIFT3_OBJC_INFERENCE = Off;
SWIFT_VERSION = 4.0;
};
name = Release;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class ViewController: UIViewController {
let customImage: UIImageView = UIImageView.init(frame: CGRect(x: self.view.bounds.midX - 40, y: self.view.bounds.midY + 40, width: 80, height: 80))
self.view.addSubview(customImage)

customImage.setImageForName(string: "Custom Font", backgroundColor: .blue, circular: true, textAttributes: [NSFontAttributeName: UIFont(name: "AmericanTypewriter-Bold", size: 30)!, NSForegroundColorAttributeName: UIColor.init(white: 1.0, alpha: 0.5)])
customImage.setImageForName(string: "Custom Font", backgroundColor: .blue, circular: true, textAttributes: [NSAttributedStringKey(rawValue: NSAttributedStringKey.font.rawValue): UIFont(name: "AmericanTypewriter-Bold", size: 30)!, NSAttributedStringKey(rawValue: NSAttributedStringKey.foregroundColor.rawValue): UIColor.init(white: 1.0, alpha: 0.5)])

}

Expand Down

0 comments on commit f54157c

Please sign in to comment.