├── DPImageView-Letters-Header └── DPImageView+LettersExtension.swift ├── Examples ├── DPImageView+Letters.xcodeproj │ ├── project.pbxproj │ ├── project.xcworkspace │ │ ├── contents.xcworkspacedata │ │ ├── xcshareddata │ │ │ └── IDEWorkspaceChecks.plist │ │ └── xcuserdata │ │ │ └── dpak.xcuserdatad │ │ │ └── UserInterfaceState.xcuserstate │ └── xcuserdata │ │ └── dpak.xcuserdatad │ │ ├── xcdebugger │ │ └── Breakpoints_v2.xcbkptlist │ │ └── xcschemes │ │ └── xcschememanagement.plist └── DPImageView+Letters │ ├── AppDelegate.swift │ ├── Assets.xcassets │ └── AppIcon.appiconset │ │ └── Contents.json │ ├── Base.lproj │ ├── LaunchScreen.storyboard │ └── Main.storyboard │ ├── DPImageView+LettersExtension.swift │ ├── Info.plist │ └── InitialsVC.swift ├── README.md └── ScreenShots ├── circular.png └── nonCircular.png /DPImageView-Letters-Header/DPImageView+LettersExtension.swift: -------------------------------------------------------------------------------- 1 | // 2 | // UIImageView+Letters.swift 3 | // UIImageViewLettersDemo 4 | // 5 | // Created by Deepakraj Murugesan on 23/02/18. 6 | // Copyright © 2018 Deepak. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | import UIKit 11 | 12 | extension UIImageView { 13 | 14 | /// Sets the image property of the view based on initial text, a specified background color, custom text attributes, and a circular clipping 15 | /// 16 | /// - Parameters: 17 | /// - string: The string used to generate the initials. This should be a user's full name if available. 18 | /// - color: This optional paramter sets the background of the image. By default, a random color will be generated. 19 | /// - circular: This boolean will determine if the image view will be clipped to a circular shape. 20 | /// - textAttributes: This dictionary allows you to specify font, text color, shadow properties, etc. 21 | open func setImage(string: String?, 22 | color: UIColor? = nil, 23 | circular: Bool = false, 24 | stroke: Bool = false, 25 | textAttributes: [NSAttributedStringKey: Any]? = nil) { 26 | 27 | let image = imageSnap(text: string != nil ? string?.initials : "", 28 | color: color ?? .random, 29 | circular: circular, 30 | stroke: stroke, 31 | textAttributes:textAttributes) 32 | 33 | if let newImage = image { 34 | self.image = newImage 35 | } 36 | } 37 | 38 | private func imageSnap(text: String?, 39 | color: UIColor, 40 | circular: Bool, 41 | stroke: Bool, 42 | textAttributes: [NSAttributedStringKey: Any]?) -> UIImage? { 43 | 44 | let scale = Float(UIScreen.main.scale) 45 | var size = bounds.size 46 | if contentMode == .scaleToFill || contentMode == .scaleAspectFill || contentMode == .scaleAspectFit || contentMode == .redraw { 47 | size.width = CGFloat(floorf((Float(size.width) * scale) / scale)) 48 | size.height = CGFloat(floorf((Float(size.height) * scale) / scale)) 49 | } 50 | 51 | UIGraphicsBeginImageContextWithOptions(size, false, CGFloat(scale)) 52 | let context = UIGraphicsGetCurrentContext() 53 | if circular { 54 | let path = CGPath(ellipseIn: bounds, transform: nil) 55 | context?.addPath(path) 56 | context?.clip() 57 | } 58 | 59 | // Fill 60 | 61 | context?.setFillColor(color.cgColor) 62 | context?.fill(CGRect(x: 0, y: 0, width: size.width, height: size.height)) 63 | 64 | let attributes = textAttributes ?? [NSAttributedStringKey.foregroundColor: UIColor.white, 65 | NSAttributedStringKey.font: UIFont.systemFont(ofSize: 55.0)] 66 | 67 | 68 | //stroke color 69 | if stroke { 70 | 71 | //outer circle 72 | context?.setStrokeColor((attributes[NSAttributedStringKey.foregroundColor] as! UIColor).cgColor) 73 | context?.setLineWidth(4) 74 | var rectangle : CGRect = CGRect(x: 0, y: 0, width: size.width, height: size.height) 75 | context?.addEllipse(in: rectangle) 76 | context?.drawPath(using: .fillStroke) 77 | 78 | //inner circle 79 | context?.setLineWidth(1) 80 | rectangle = CGRect(x: 4, y: 4, width: size.width - 8, height: size.height - 8) 81 | context?.addEllipse(in: rectangle) 82 | context?.drawPath(using: .fillStroke) 83 | } 84 | 85 | // Text 86 | if let text = text { 87 | let textSize = text.size(withAttributes: attributes) 88 | let bounds = self.bounds 89 | let rect = CGRect(x: bounds.size.width/2 - textSize.width/2, y: bounds.size.height/2 - textSize.height/2, width: textSize.width, height: textSize.height) 90 | 91 | text.draw(in: rect, withAttributes: attributes) 92 | } 93 | 94 | let image = UIGraphicsGetImageFromCurrentImageContext() 95 | UIGraphicsEndImageContext() 96 | 97 | return image 98 | } 99 | } 100 | 101 | // MARK: UIColor Helper 102 | extension UIColor { 103 | 104 | /// Returns random generated color. 105 | open static var random: UIColor { 106 | srandom(arc4random()) 107 | var red: Double = 0 108 | 109 | while (red < 0.1 || red > 0.84) { 110 | red = drand48() 111 | } 112 | 113 | var green: Double = 0 114 | while (green < 0.1 || green > 0.84) { 115 | green = drand48() 116 | } 117 | 118 | var blue: Double = 0 119 | while (blue < 0.1 || blue > 0.84) { 120 | blue = drand48() 121 | } 122 | 123 | return .init(red: CGFloat(red), green: CGFloat(green), blue: CGFloat(blue), alpha: 1.0) 124 | } 125 | 126 | open static func colorHash(name: String?) -> UIColor { 127 | guard let name = name else { 128 | return .red 129 | } 130 | 131 | var nameValue = 0 132 | for character in name { 133 | let characterString = String(character) 134 | let scalars = characterString.unicodeScalars 135 | nameValue += Int(scalars[scalars.startIndex].value) 136 | } 137 | 138 | var r = Float((nameValue * 123) % 51) / 51 139 | var g = Float((nameValue * 321) % 73) / 73 140 | var b = Float((nameValue * 213) % 91) / 91 141 | 142 | let defaultValue: Float = 0.84 143 | r = min(max(r, 0.1), defaultValue) 144 | g = min(max(g, 0.1), defaultValue) 145 | b = min(max(b, 0.1), defaultValue) 146 | 147 | return .init(red: CGFloat(r), green: CGFloat(g), blue: CGFloat(b), alpha: 1.0) 148 | } 149 | } 150 | 151 | // MARK: String Helper 152 | // Example = Ex 153 | // For Example = FE 154 | // for example = fe 155 | // "" = DP 156 | 157 | extension String { 158 | 159 | public var initials: String { 160 | 161 | let words = components(separatedBy: .whitespacesAndNewlines) 162 | 163 | //to identify letters 164 | let letters = CharacterSet.letters 165 | var firstChar : String = "" 166 | var secondChar : String = "" 167 | var firstCharFoundIndex : Int = -1 168 | var firstCharFound : Bool = false 169 | var secondCharFound : Bool = false 170 | 171 | for (index, item) in words.enumerated() { 172 | 173 | if item.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty { 174 | continue 175 | } 176 | 177 | //browse through the rest of the word 178 | for (_, char) in item.unicodeScalars.enumerated() { 179 | 180 | //check if its a aplha 181 | if letters.contains(char) { 182 | 183 | if !firstCharFound { 184 | firstChar = String(char) 185 | firstCharFound = true 186 | firstCharFoundIndex = index 187 | 188 | } else if !secondCharFound { 189 | 190 | secondChar = String(char) 191 | if firstCharFoundIndex != index { 192 | secondCharFound = true 193 | } 194 | 195 | break 196 | } else { 197 | break 198 | } 199 | } 200 | } 201 | } 202 | 203 | if firstChar.isEmpty && secondChar.isEmpty { 204 | firstChar = "D" 205 | secondChar = "P" 206 | } 207 | 208 | return firstChar + secondChar 209 | } 210 | } 211 | 212 | -------------------------------------------------------------------------------- /Examples/DPImageView+Letters.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 48; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | E717B87620408B8400E9A528 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = E717B87520408B8400E9A528 /* AppDelegate.swift */; }; 11 | E717B87B20408B8400E9A528 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = E717B87920408B8400E9A528 /* Main.storyboard */; }; 12 | E717B87D20408B8400E9A528 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = E717B87C20408B8400E9A528 /* Assets.xcassets */; }; 13 | E717B88020408B8400E9A528 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = E717B87E20408B8400E9A528 /* LaunchScreen.storyboard */; }; 14 | E717B88820408B9D00E9A528 /* DPImageView+LettersExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = E717B88720408B9D00E9A528 /* DPImageView+LettersExtension.swift */; }; 15 | E717B88A20408FB300E9A528 /* InitialsVC.swift in Sources */ = {isa = PBXBuildFile; fileRef = E717B88920408FB300E9A528 /* InitialsVC.swift */; }; 16 | /* End PBXBuildFile section */ 17 | 18 | /* Begin PBXFileReference section */ 19 | E717B87220408B8400E9A528 /* DPImageView+Letters.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "DPImageView+Letters.app"; sourceTree = BUILT_PRODUCTS_DIR; }; 20 | E717B87520408B8400E9A528 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 21 | E717B87A20408B8400E9A528 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 22 | E717B87C20408B8400E9A528 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 23 | E717B87F20408B8400E9A528 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 24 | E717B88120408B8400E9A528 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 25 | E717B88720408B9D00E9A528 /* DPImageView+LettersExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "DPImageView+LettersExtension.swift"; sourceTree = ""; }; 26 | E717B88920408FB300E9A528 /* InitialsVC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InitialsVC.swift; sourceTree = ""; }; 27 | /* End PBXFileReference section */ 28 | 29 | /* Begin PBXFrameworksBuildPhase section */ 30 | E717B86F20408B8400E9A528 /* Frameworks */ = { 31 | isa = PBXFrameworksBuildPhase; 32 | buildActionMask = 2147483647; 33 | files = ( 34 | ); 35 | runOnlyForDeploymentPostprocessing = 0; 36 | }; 37 | /* End PBXFrameworksBuildPhase section */ 38 | 39 | /* Begin PBXGroup section */ 40 | E717B86920408B8400E9A528 = { 41 | isa = PBXGroup; 42 | children = ( 43 | E717B87420408B8400E9A528 /* DPImageView+Letters */, 44 | E717B87320408B8400E9A528 /* Products */, 45 | ); 46 | sourceTree = ""; 47 | }; 48 | E717B87320408B8400E9A528 /* Products */ = { 49 | isa = PBXGroup; 50 | children = ( 51 | E717B87220408B8400E9A528 /* DPImageView+Letters.app */, 52 | ); 53 | name = Products; 54 | sourceTree = ""; 55 | }; 56 | E717B87420408B8400E9A528 /* DPImageView+Letters */ = { 57 | isa = PBXGroup; 58 | children = ( 59 | E717B87520408B8400E9A528 /* AppDelegate.swift */, 60 | E717B88720408B9D00E9A528 /* DPImageView+LettersExtension.swift */, 61 | E717B87920408B8400E9A528 /* Main.storyboard */, 62 | E717B88920408FB300E9A528 /* InitialsVC.swift */, 63 | E717B87C20408B8400E9A528 /* Assets.xcassets */, 64 | E717B87E20408B8400E9A528 /* LaunchScreen.storyboard */, 65 | E717B88120408B8400E9A528 /* Info.plist */, 66 | ); 67 | path = "DPImageView+Letters"; 68 | sourceTree = ""; 69 | }; 70 | /* End PBXGroup section */ 71 | 72 | /* Begin PBXNativeTarget section */ 73 | E717B87120408B8400E9A528 /* DPImageView+Letters */ = { 74 | isa = PBXNativeTarget; 75 | buildConfigurationList = E717B88420408B8400E9A528 /* Build configuration list for PBXNativeTarget "DPImageView+Letters" */; 76 | buildPhases = ( 77 | E717B86E20408B8400E9A528 /* Sources */, 78 | E717B86F20408B8400E9A528 /* Frameworks */, 79 | E717B87020408B8400E9A528 /* Resources */, 80 | ); 81 | buildRules = ( 82 | ); 83 | dependencies = ( 84 | ); 85 | name = "DPImageView+Letters"; 86 | productName = "DPImageView+Letters"; 87 | productReference = E717B87220408B8400E9A528 /* DPImageView+Letters.app */; 88 | productType = "com.apple.product-type.application"; 89 | }; 90 | /* End PBXNativeTarget section */ 91 | 92 | /* Begin PBXProject section */ 93 | E717B86A20408B8400E9A528 /* Project object */ = { 94 | isa = PBXProject; 95 | attributes = { 96 | LastSwiftUpdateCheck = 0920; 97 | LastUpgradeCheck = 0920; 98 | ORGANIZATIONNAME = Noticeboard; 99 | TargetAttributes = { 100 | E717B87120408B8400E9A528 = { 101 | CreatedOnToolsVersion = 9.2; 102 | ProvisioningStyle = Automatic; 103 | }; 104 | }; 105 | }; 106 | buildConfigurationList = E717B86D20408B8400E9A528 /* Build configuration list for PBXProject "DPImageView+Letters" */; 107 | compatibilityVersion = "Xcode 8.0"; 108 | developmentRegion = en; 109 | hasScannedForEncodings = 0; 110 | knownRegions = ( 111 | en, 112 | Base, 113 | ); 114 | mainGroup = E717B86920408B8400E9A528; 115 | productRefGroup = E717B87320408B8400E9A528 /* Products */; 116 | projectDirPath = ""; 117 | projectRoot = ""; 118 | targets = ( 119 | E717B87120408B8400E9A528 /* DPImageView+Letters */, 120 | ); 121 | }; 122 | /* End PBXProject section */ 123 | 124 | /* Begin PBXResourcesBuildPhase section */ 125 | E717B87020408B8400E9A528 /* Resources */ = { 126 | isa = PBXResourcesBuildPhase; 127 | buildActionMask = 2147483647; 128 | files = ( 129 | E717B88020408B8400E9A528 /* LaunchScreen.storyboard in Resources */, 130 | E717B87D20408B8400E9A528 /* Assets.xcassets in Resources */, 131 | E717B87B20408B8400E9A528 /* Main.storyboard in Resources */, 132 | ); 133 | runOnlyForDeploymentPostprocessing = 0; 134 | }; 135 | /* End PBXResourcesBuildPhase section */ 136 | 137 | /* Begin PBXSourcesBuildPhase section */ 138 | E717B86E20408B8400E9A528 /* Sources */ = { 139 | isa = PBXSourcesBuildPhase; 140 | buildActionMask = 2147483647; 141 | files = ( 142 | E717B87620408B8400E9A528 /* AppDelegate.swift in Sources */, 143 | E717B88820408B9D00E9A528 /* DPImageView+LettersExtension.swift in Sources */, 144 | E717B88A20408FB300E9A528 /* InitialsVC.swift in Sources */, 145 | ); 146 | runOnlyForDeploymentPostprocessing = 0; 147 | }; 148 | /* End PBXSourcesBuildPhase section */ 149 | 150 | /* Begin PBXVariantGroup section */ 151 | E717B87920408B8400E9A528 /* Main.storyboard */ = { 152 | isa = PBXVariantGroup; 153 | children = ( 154 | E717B87A20408B8400E9A528 /* Base */, 155 | ); 156 | name = Main.storyboard; 157 | sourceTree = ""; 158 | }; 159 | E717B87E20408B8400E9A528 /* LaunchScreen.storyboard */ = { 160 | isa = PBXVariantGroup; 161 | children = ( 162 | E717B87F20408B8400E9A528 /* Base */, 163 | ); 164 | name = LaunchScreen.storyboard; 165 | sourceTree = ""; 166 | }; 167 | /* End PBXVariantGroup section */ 168 | 169 | /* Begin XCBuildConfiguration section */ 170 | E717B88220408B8400E9A528 /* Debug */ = { 171 | isa = XCBuildConfiguration; 172 | buildSettings = { 173 | ALWAYS_SEARCH_USER_PATHS = NO; 174 | CLANG_ANALYZER_NONNULL = YES; 175 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 176 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 177 | CLANG_CXX_LIBRARY = "libc++"; 178 | CLANG_ENABLE_MODULES = YES; 179 | CLANG_ENABLE_OBJC_ARC = YES; 180 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 181 | CLANG_WARN_BOOL_CONVERSION = YES; 182 | CLANG_WARN_COMMA = YES; 183 | CLANG_WARN_CONSTANT_CONVERSION = YES; 184 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 185 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 186 | CLANG_WARN_EMPTY_BODY = YES; 187 | CLANG_WARN_ENUM_CONVERSION = YES; 188 | CLANG_WARN_INFINITE_RECURSION = YES; 189 | CLANG_WARN_INT_CONVERSION = YES; 190 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 191 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 192 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 193 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 194 | CLANG_WARN_STRICT_PROTOTYPES = YES; 195 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 196 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 197 | CLANG_WARN_UNREACHABLE_CODE = YES; 198 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 199 | CODE_SIGN_IDENTITY = "iPhone Developer"; 200 | COPY_PHASE_STRIP = NO; 201 | DEBUG_INFORMATION_FORMAT = dwarf; 202 | ENABLE_STRICT_OBJC_MSGSEND = YES; 203 | ENABLE_TESTABILITY = YES; 204 | GCC_C_LANGUAGE_STANDARD = gnu11; 205 | GCC_DYNAMIC_NO_PIC = NO; 206 | GCC_NO_COMMON_BLOCKS = YES; 207 | GCC_OPTIMIZATION_LEVEL = 0; 208 | GCC_PREPROCESSOR_DEFINITIONS = ( 209 | "DEBUG=1", 210 | "$(inherited)", 211 | ); 212 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 213 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 214 | GCC_WARN_UNDECLARED_SELECTOR = YES; 215 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 216 | GCC_WARN_UNUSED_FUNCTION = YES; 217 | GCC_WARN_UNUSED_VARIABLE = YES; 218 | IPHONEOS_DEPLOYMENT_TARGET = 11.2; 219 | MTL_ENABLE_DEBUG_INFO = YES; 220 | ONLY_ACTIVE_ARCH = YES; 221 | SDKROOT = iphoneos; 222 | SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; 223 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 224 | }; 225 | name = Debug; 226 | }; 227 | E717B88320408B8400E9A528 /* Release */ = { 228 | isa = XCBuildConfiguration; 229 | buildSettings = { 230 | ALWAYS_SEARCH_USER_PATHS = NO; 231 | CLANG_ANALYZER_NONNULL = YES; 232 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 233 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 234 | CLANG_CXX_LIBRARY = "libc++"; 235 | CLANG_ENABLE_MODULES = YES; 236 | CLANG_ENABLE_OBJC_ARC = YES; 237 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 238 | CLANG_WARN_BOOL_CONVERSION = YES; 239 | CLANG_WARN_COMMA = YES; 240 | CLANG_WARN_CONSTANT_CONVERSION = YES; 241 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 242 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 243 | CLANG_WARN_EMPTY_BODY = YES; 244 | CLANG_WARN_ENUM_CONVERSION = YES; 245 | CLANG_WARN_INFINITE_RECURSION = YES; 246 | CLANG_WARN_INT_CONVERSION = YES; 247 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 248 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 249 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 250 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 251 | CLANG_WARN_STRICT_PROTOTYPES = YES; 252 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 253 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 254 | CLANG_WARN_UNREACHABLE_CODE = YES; 255 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 256 | CODE_SIGN_IDENTITY = "iPhone Developer"; 257 | COPY_PHASE_STRIP = NO; 258 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 259 | ENABLE_NS_ASSERTIONS = NO; 260 | ENABLE_STRICT_OBJC_MSGSEND = YES; 261 | GCC_C_LANGUAGE_STANDARD = gnu11; 262 | GCC_NO_COMMON_BLOCKS = YES; 263 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 264 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 265 | GCC_WARN_UNDECLARED_SELECTOR = YES; 266 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 267 | GCC_WARN_UNUSED_FUNCTION = YES; 268 | GCC_WARN_UNUSED_VARIABLE = YES; 269 | IPHONEOS_DEPLOYMENT_TARGET = 11.2; 270 | MTL_ENABLE_DEBUG_INFO = NO; 271 | SDKROOT = iphoneos; 272 | SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; 273 | VALIDATE_PRODUCT = YES; 274 | }; 275 | name = Release; 276 | }; 277 | E717B88520408B8400E9A528 /* Debug */ = { 278 | isa = XCBuildConfiguration; 279 | buildSettings = { 280 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 281 | CODE_SIGN_STYLE = Automatic; 282 | DEVELOPMENT_TEAM = J494G3U7D8; 283 | INFOPLIST_FILE = "DPImageView+Letters/Info.plist"; 284 | IPHONEOS_DEPLOYMENT_TARGET = 9.3; 285 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 286 | PRODUCT_BUNDLE_IDENTIFIER = "com.deepak.DPImageView-Letters"; 287 | PRODUCT_NAME = "$(TARGET_NAME)"; 288 | SWIFT_VERSION = 4.0; 289 | TARGETED_DEVICE_FAMILY = "1,2"; 290 | }; 291 | name = Debug; 292 | }; 293 | E717B88620408B8400E9A528 /* Release */ = { 294 | isa = XCBuildConfiguration; 295 | buildSettings = { 296 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 297 | CODE_SIGN_STYLE = Automatic; 298 | DEVELOPMENT_TEAM = J494G3U7D8; 299 | INFOPLIST_FILE = "DPImageView+Letters/Info.plist"; 300 | IPHONEOS_DEPLOYMENT_TARGET = 9.3; 301 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 302 | PRODUCT_BUNDLE_IDENTIFIER = "com.deepak.DPImageView-Letters"; 303 | PRODUCT_NAME = "$(TARGET_NAME)"; 304 | SWIFT_VERSION = 4.0; 305 | TARGETED_DEVICE_FAMILY = "1,2"; 306 | }; 307 | name = Release; 308 | }; 309 | /* End XCBuildConfiguration section */ 310 | 311 | /* Begin XCConfigurationList section */ 312 | E717B86D20408B8400E9A528 /* Build configuration list for PBXProject "DPImageView+Letters" */ = { 313 | isa = XCConfigurationList; 314 | buildConfigurations = ( 315 | E717B88220408B8400E9A528 /* Debug */, 316 | E717B88320408B8400E9A528 /* Release */, 317 | ); 318 | defaultConfigurationIsVisible = 0; 319 | defaultConfigurationName = Release; 320 | }; 321 | E717B88420408B8400E9A528 /* Build configuration list for PBXNativeTarget "DPImageView+Letters" */ = { 322 | isa = XCConfigurationList; 323 | buildConfigurations = ( 324 | E717B88520408B8400E9A528 /* Debug */, 325 | E717B88620408B8400E9A528 /* Release */, 326 | ); 327 | defaultConfigurationIsVisible = 0; 328 | defaultConfigurationName = Release; 329 | }; 330 | /* End XCConfigurationList section */ 331 | }; 332 | rootObject = E717B86A20408B8400E9A528 /* Project object */; 333 | } 334 | -------------------------------------------------------------------------------- /Examples/DPImageView+Letters.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Examples/DPImageView+Letters.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /Examples/DPImageView+Letters.xcodeproj/project.xcworkspace/xcuserdata/dpak.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deepakraj27/DPImageView-Letters/5e0632b6dab5fbdbc280383f05846d7ea91aaaf5/Examples/DPImageView+Letters.xcodeproj/project.xcworkspace/xcuserdata/dpak.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /Examples/DPImageView+Letters.xcodeproj/xcuserdata/dpak.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | -------------------------------------------------------------------------------- /Examples/DPImageView+Letters.xcodeproj/xcuserdata/dpak.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | DPImageView+Letters.xcscheme 8 | 9 | orderHint 10 | 0 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /Examples/DPImageView+Letters/AppDelegate.swift: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.swift 3 | // DPImageView+Letters 4 | // 5 | // Created by Deepakraj Murugesan on 23/02/18. 6 | // Copyright © 2018 Deepak. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | @UIApplicationMain 12 | class AppDelegate: UIResponder, UIApplicationDelegate { 13 | 14 | var window: UIWindow? 15 | 16 | 17 | func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 18 | // Override point for customization after application launch. 19 | return true 20 | } 21 | 22 | func applicationWillResignActive(_ application: UIApplication) { 23 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 24 | // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game. 25 | } 26 | 27 | func applicationDidEnterBackground(_ application: UIApplication) { 28 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 29 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 30 | } 31 | 32 | func applicationWillEnterForeground(_ application: UIApplication) { 33 | // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. 34 | } 35 | 36 | func applicationDidBecomeActive(_ application: UIApplication) { 37 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 38 | } 39 | 40 | func applicationWillTerminate(_ application: UIApplication) { 41 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 42 | } 43 | 44 | 45 | } 46 | 47 | -------------------------------------------------------------------------------- /Examples/DPImageView+Letters/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "20x20", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "20x20", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "29x29", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "29x29", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "40x40", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "40x40", 31 | "scale" : "3x" 32 | }, 33 | { 34 | "idiom" : "iphone", 35 | "size" : "60x60", 36 | "scale" : "2x" 37 | }, 38 | { 39 | "idiom" : "iphone", 40 | "size" : "60x60", 41 | "scale" : "3x" 42 | }, 43 | { 44 | "idiom" : "ipad", 45 | "size" : "20x20", 46 | "scale" : "1x" 47 | }, 48 | { 49 | "idiom" : "ipad", 50 | "size" : "20x20", 51 | "scale" : "2x" 52 | }, 53 | { 54 | "idiom" : "ipad", 55 | "size" : "29x29", 56 | "scale" : "1x" 57 | }, 58 | { 59 | "idiom" : "ipad", 60 | "size" : "29x29", 61 | "scale" : "2x" 62 | }, 63 | { 64 | "idiom" : "ipad", 65 | "size" : "40x40", 66 | "scale" : "1x" 67 | }, 68 | { 69 | "idiom" : "ipad", 70 | "size" : "40x40", 71 | "scale" : "2x" 72 | }, 73 | { 74 | "idiom" : "ipad", 75 | "size" : "76x76", 76 | "scale" : "1x" 77 | }, 78 | { 79 | "idiom" : "ipad", 80 | "size" : "76x76", 81 | "scale" : "2x" 82 | }, 83 | { 84 | "idiom" : "ipad", 85 | "size" : "83.5x83.5", 86 | "scale" : "2x" 87 | }, 88 | { 89 | "idiom" : "ios-marketing", 90 | "size" : "1024x1024", 91 | "scale" : "1x" 92 | } 93 | ], 94 | "info" : { 95 | "version" : 1, 96 | "author" : "xcode" 97 | } 98 | } -------------------------------------------------------------------------------- /Examples/DPImageView+Letters/Base.lproj/LaunchScreen.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 29 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | -------------------------------------------------------------------------------- /Examples/DPImageView+Letters/Base.lproj/Main.storyboard: -------------------------------------------------------------------------------- 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 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 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 | -------------------------------------------------------------------------------- /Examples/DPImageView+Letters/DPImageView+LettersExtension.swift: -------------------------------------------------------------------------------- 1 | // 2 | // DPImageView+LettersExtension.swift 3 | // DPUIImageViewLettersDemo 4 | // 5 | // Created by Deepakraj Murugesan on 23/02/18. 6 | // Copyright © 2018 Deepak. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | import UIKit 11 | 12 | extension UIImageView { 13 | 14 | /// Sets the image property of the view based on initial text, a specified background color, custom text attributes, and a circular clipping 15 | /// 16 | /// - Parameters: 17 | /// - string: The string used to generate the initials. This should be a user's full name if available. 18 | /// - color: This optional paramter sets the background of the image. By default, a random color will be generated. 19 | /// - circular: This boolean will determine if the image view will be clipped to a circular shape. 20 | /// - textAttributes: This dictionary allows you to specify font, text color, shadow properties, etc. 21 | open func setImage(string: String?, 22 | color: UIColor? = nil, 23 | circular: Bool = false, 24 | stroke: Bool = false, 25 | textAttributes: [NSAttributedStringKey: Any]? = nil) { 26 | 27 | let image = imageSnap(text: string != nil ? string?.initials : "", 28 | color: color ?? .random, 29 | circular: circular, 30 | stroke: stroke, 31 | textAttributes:textAttributes) 32 | 33 | if let newImage = image { 34 | self.image = newImage 35 | } 36 | } 37 | 38 | private func imageSnap(text: String?, 39 | color: UIColor, 40 | circular: Bool, 41 | stroke: Bool, 42 | textAttributes: [NSAttributedStringKey: Any]?) -> UIImage? { 43 | 44 | let scale = Float(UIScreen.main.scale) 45 | var size = bounds.size 46 | if contentMode == .scaleToFill || contentMode == .scaleAspectFill || contentMode == .scaleAspectFit || contentMode == .redraw { 47 | size.width = CGFloat(floorf((Float(size.width) * scale) / scale)) 48 | size.height = CGFloat(floorf((Float(size.height) * scale) / scale)) 49 | } 50 | 51 | UIGraphicsBeginImageContextWithOptions(size, false, CGFloat(scale)) 52 | let context = UIGraphicsGetCurrentContext() 53 | if circular { 54 | let path = CGPath(ellipseIn: bounds, transform: nil) 55 | context?.addPath(path) 56 | context?.clip() 57 | } 58 | 59 | // Fill 60 | 61 | context?.setFillColor(color.cgColor) 62 | context?.fill(CGRect(x: 0, y: 0, width: size.width, height: size.height)) 63 | 64 | let attributes = textAttributes ?? [NSAttributedStringKey.foregroundColor: UIColor.white, 65 | NSAttributedStringKey.font: UIFont.systemFont(ofSize: 55.0)] 66 | 67 | 68 | //stroke color 69 | if stroke { 70 | 71 | //outer circle 72 | context?.setStrokeColor((attributes[NSAttributedStringKey.foregroundColor] as! UIColor).cgColor) 73 | context?.setLineWidth(4) 74 | var rectangle : CGRect = CGRect(x: 0, y: 0, width: size.width, height: size.height) 75 | context?.addEllipse(in: rectangle) 76 | context?.drawPath(using: .fillStroke) 77 | 78 | //inner circle 79 | context?.setLineWidth(1) 80 | rectangle = CGRect(x: 4, y: 4, width: size.width - 8, height: size.height - 8) 81 | context?.addEllipse(in: rectangle) 82 | context?.drawPath(using: .fillStroke) 83 | } 84 | 85 | // Text 86 | if let text = text { 87 | let textSize = text.size(withAttributes: attributes) 88 | let bounds = self.bounds 89 | let rect = CGRect(x: bounds.size.width/2 - textSize.width/2, y: bounds.size.height/2 - textSize.height/2, width: textSize.width, height: textSize.height) 90 | 91 | text.draw(in: rect, withAttributes: attributes) 92 | } 93 | 94 | let image = UIGraphicsGetImageFromCurrentImageContext() 95 | UIGraphicsEndImageContext() 96 | 97 | return image 98 | } 99 | } 100 | 101 | // MARK: UIColor Helper 102 | extension UIColor { 103 | 104 | /// Returns random generated color. 105 | open static var random: UIColor { 106 | srandom(arc4random()) 107 | var red: Double = 0 108 | 109 | while (red < 0.1 || red > 0.84) { 110 | red = drand48() 111 | } 112 | 113 | var green: Double = 0 114 | while (green < 0.1 || green > 0.84) { 115 | green = drand48() 116 | } 117 | 118 | var blue: Double = 0 119 | while (blue < 0.1 || blue > 0.84) { 120 | blue = drand48() 121 | } 122 | 123 | return .init(red: CGFloat(red), green: CGFloat(green), blue: CGFloat(blue), alpha: 1.0) 124 | } 125 | 126 | open static func colorHash(name: String?) -> UIColor { 127 | guard let name = name else { 128 | return .red 129 | } 130 | 131 | var nameValue = 0 132 | for character in name { 133 | let characterString = String(character) 134 | let scalars = characterString.unicodeScalars 135 | nameValue += Int(scalars[scalars.startIndex].value) 136 | } 137 | 138 | var r = Float((nameValue * 123) % 51) / 51 139 | var g = Float((nameValue * 321) % 73) / 73 140 | var b = Float((nameValue * 213) % 91) / 91 141 | 142 | let defaultValue: Float = 0.84 143 | r = min(max(r, 0.1), defaultValue) 144 | g = min(max(g, 0.1), defaultValue) 145 | b = min(max(b, 0.1), defaultValue) 146 | 147 | return .init(red: CGFloat(r), green: CGFloat(g), blue: CGFloat(b), alpha: 1.0) 148 | } 149 | } 150 | 151 | // MARK: String Helper 152 | // Example = Ex 153 | // For Example = FE 154 | // for example = fe 155 | // "" = DP 156 | 157 | extension String { 158 | 159 | public var initials: String { 160 | 161 | let words = components(separatedBy: .whitespacesAndNewlines) 162 | 163 | //to identify letters 164 | let letters = CharacterSet.letters 165 | var firstChar : String = "" 166 | var secondChar : String = "" 167 | var firstCharFoundIndex : Int = -1 168 | var firstCharFound : Bool = false 169 | var secondCharFound : Bool = false 170 | 171 | for (index, item) in words.enumerated() { 172 | 173 | if item.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty { 174 | continue 175 | } 176 | 177 | //browse through the rest of the word 178 | for (_, char) in item.unicodeScalars.enumerated() { 179 | 180 | //check if its a aplha 181 | if letters.contains(char) { 182 | 183 | if !firstCharFound { 184 | firstChar = String(char) 185 | firstCharFound = true 186 | firstCharFoundIndex = index 187 | 188 | } else if !secondCharFound { 189 | 190 | secondChar = String(char) 191 | if firstCharFoundIndex != index { 192 | secondCharFound = true 193 | } 194 | 195 | break 196 | } else { 197 | break 198 | } 199 | } 200 | } 201 | } 202 | 203 | if firstChar.isEmpty && secondChar.isEmpty { 204 | firstChar = "D" 205 | secondChar = "P" 206 | } 207 | 208 | return firstChar + secondChar 209 | } 210 | } 211 | 212 | -------------------------------------------------------------------------------- /Examples/DPImageView+Letters/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | $(DEVELOPMENT_LANGUAGE) 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | APPL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleVersion 20 | 1 21 | LSRequiresIPhoneOS 22 | 23 | UILaunchStoryboardName 24 | LaunchScreen 25 | UIMainStoryboardFile 26 | Main 27 | UIRequiredDeviceCapabilities 28 | 29 | armv7 30 | 31 | UISupportedInterfaceOrientations 32 | 33 | UIInterfaceOrientationPortrait 34 | UIInterfaceOrientationLandscapeLeft 35 | UIInterfaceOrientationLandscapeRight 36 | 37 | UISupportedInterfaceOrientations~ipad 38 | 39 | UIInterfaceOrientationPortrait 40 | UIInterfaceOrientationPortraitUpsideDown 41 | UIInterfaceOrientationLandscapeLeft 42 | UIInterfaceOrientationLandscapeRight 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /Examples/DPImageView+Letters/InitialsVC.swift: -------------------------------------------------------------------------------- 1 | // 2 | // InitialsVC.swift 3 | // DPImageView+Letters 4 | // 5 | // Created by Deepakraj Murugesan on 23/02/18. 6 | // Copyright © 2018 Deepak. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | class InitialsVC: UIViewController { 12 | 13 | @IBOutlet weak var initialsImageView: UIImageView! 14 | @IBOutlet weak var initialsTextField: UITextField! 15 | @IBOutlet weak var formInitialsButtonOutlet: UIButton! 16 | var isCircularWithStroke: Bool = false 17 | 18 | @IBOutlet weak var circularSwitch: UISwitch! 19 | //MARK: - VIEW CONTROLLER LIFE CYCLE 20 | override func viewDidLoad() { 21 | super.viewDidLoad() 22 | formInitialsButtonOutlet.layer.cornerRadius = 5 23 | formInitialsButtonOutlet.layer.masksToBounds = true 24 | // Do any additional setup after loading the view. 25 | } 26 | 27 | //MARK: - FORM INITIAL BUTTON... 28 | @IBAction func formInitialAction(_ sender: Any) { 29 | getCustomImage(imageDisplayName: initialsTextField.text, imageView: initialsImageView) 30 | } 31 | 32 | //MARK: - BackGround Tap 33 | @IBAction func backGroupTap(_ sender: Any) { 34 | self.view.endEditing(true) 35 | } 36 | 37 | //MARK: - Get Custom Image 38 | func getCustomImage(imageDisplayName: String?, imageView: UIImageView!){ 39 | if let name = imageDisplayName, !name.isEmpty { 40 | imageView.setImage(string:name, color: UIColor.colorHash(name: name), circular: isCircularWithStroke, stroke: isCircularWithStroke) 41 | }else{ 42 | imageView.setImage(string:"Display Picture", color: UIColor.colorHash(name: "Display Picture"), circular: isCircularWithStroke, stroke: isCircularWithStroke) 43 | } 44 | } 45 | 46 | //MARK: - Switch OnClick 47 | @IBAction func switchAction(_ sender: UISwitch) { 48 | if sender.isOn{ 49 | isCircularWithStroke = true 50 | }else{ 51 | isCircularWithStroke = false 52 | } 53 | } 54 | 55 | } 56 | extension InitialsVC: UITextFieldDelegate{ 57 | func textFieldShouldReturn(_ textField: UITextField) -> Bool { 58 | textField.resignFirstResponder() 59 | return true 60 | } 61 | 62 | fileprivate func resetEntireSubview() { 63 | initialsImageView.image = nil 64 | circularSwitch.isOn = false 65 | isCircularWithStroke = false 66 | formInitialsButtonOutlet.backgroundColor = UIColor(red:0.46, green:0.51, blue:0.55, alpha:0.3) 67 | formInitialsButtonOutlet.isUserInteractionEnabled = false 68 | } 69 | 70 | func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { 71 | let currentCharacterCount = textField.text?.characters.count ?? 0 72 | if (range.length + range.location > currentCharacterCount){ 73 | return false 74 | } 75 | let newLength = currentCharacterCount + string.characters.count - range.length 76 | 77 | //change the value of the label 78 | if newLength > 0{ 79 | formInitialsButtonOutlet.backgroundColor = UIColor(red:0.18, green:0.49, blue:0.82, alpha:1.0) 80 | formInitialsButtonOutlet.isUserInteractionEnabled = true 81 | }else{ 82 | resetEntireSubview() 83 | } 84 | return newLength <= 25 // To just allow up to 25 characters 85 | } 86 | 87 | 88 | } 89 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # DPImageView-Letters 2 | 3 | An easy, helpful UIImageView extension that generates letter initials as a placeholder for user images, with a custom background color and double border with custom stroke width 4 | 5 | Completely supports SWIFT 4 and SWIFT 3, written excusively for you. 6 | 7 | Inspired by https://github.com/bachonk/UIImageView-Letters 8 | 9 | ### ScreenShots 10 | 11 | Without Circular View and Border | With Circular View and Border 12 | :-------------------------:|:-------------------------: 13 | ![](https://github.com/deepakraj27/DPImageView-Letters/blob/master/ScreenShots/nonCircular.png) | ![](https://github.com/deepakraj27/DPImageView-Letters/blob/master/ScreenShots/circular.png) 14 | 15 | ### Manual Installation 16 | 17 | 1. Drag and Drop the `DPImageView-letters.swift` file from `DPImageView-Letters-Header` Folder to your project. 18 | 2. Thats it! Enjoy! 19 | 20 | ### Usage 21 | 22 | Since `DPImageView-letters.swift` is created as extension to the `ImageView` you don't need to import this in each class seperately. Simply add it to your project after that it is done. 23 | 24 | ### Methods 25 | Call this method to any `ImageView` inside the project. This method will find the initials of the provided text and it will select a random color as background color and it will provide you a way to make your image view as circular or not and it can also give a dual border to the imageview with text attributes changes 26 | 27 | + `func setImage(string: String?, color: UIColor? = nil, circular: Bool = false, stroke: Bool = false, textAttributes: [NSAttributedStringKey: Any]? = nil)` 28 | 29 | `string` is the string used to generate the initials. This should be a user's full name if available. 30 | 31 | `color` is an optional parameter that sets the background color of the image. Pass in `nil` to have a color automatically generated for you. 32 | 33 | `circular` is a boolean parameter that will automatically clip the image to a circle if enabled. 34 | 35 | `stroke` is a boolean parameter that will automatically draw a border to a circle if enabled. (it also provides you dual border) 36 | 37 | `textAttributes` is an optional dictionary of predefined character attributes for text. You can find the list of available keys in NSAttributedString 38 | 39 | If you want to keep the same color for a given name, you can use the following method from `UIColor` extension: 40 | `static func colorHash(name: String?) -> UIColor` 41 | 42 | You can get random color using the method `static var random: UIColor`. 43 | 44 | 45 | ##### Example 46 | 47 | ``` 48 | imageView.setImage(string:name, color: UIColor.colorHash(name: name), circular: false, stroke: false) // Check the first example screenshot for the output 49 | 50 | imageView.setImage(string:name, color: UIColor.colorHash(name: name), circular: true, stroke: false) // Check the second example screenshot for the output 51 | ``` 52 | 53 | Good luck. Try adding more improvements to the code 54 | -------------------------------------------------------------------------------- /ScreenShots/circular.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deepakraj27/DPImageView-Letters/5e0632b6dab5fbdbc280383f05846d7ea91aaaf5/ScreenShots/circular.png -------------------------------------------------------------------------------- /ScreenShots/nonCircular.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deepakraj27/DPImageView-Letters/5e0632b6dab5fbdbc280383f05846d7ea91aaaf5/ScreenShots/nonCircular.png --------------------------------------------------------------------------------