Skip to content

Commit

Permalink
Add an opaque flag
Browse files Browse the repository at this point in the history
  • Loading branch information
vpeschenkov committed Mar 17, 2020
1 parent 78cd8a2 commit 218b48b
Show file tree
Hide file tree
Showing 39 changed files with 377 additions and 295 deletions.
38 changes: 1 addition & 37 deletions ISSUE_TEMPLATE.md
@@ -1,39 +1,3 @@
### Description

Describe your issue here.

### Requirements (place an `x` in each of the `[ ]`)

* [ ] I've read and agree to the [Code of Conduct](CODE_OF_CONDUCT.md).
* [ ] I've read and understood the [Contributing guidelines](CONTRIBUTING.md) and have done my best effort to follow them.
* [ ] I've searched for any related issues and avoided creating a duplicate issue.

---

### Bug Report

Filling out the following details about bugs will help us solve your issue sooner.

#### Reproducible in:

LetterAvatarKit version:

iOS version:

#### Steps to reproduce:

1.
2.
3.

#### Expected result:

What you expected to happen

#### Actual result:

What actually happened

#### Attachments:

Logs, screenshots, screencast, sample project, funny gif, etc.
Describe your issue here.
2 changes: 1 addition & 1 deletion LetterAvatarKit.podspec
@@ -1,7 +1,7 @@
Pod::Spec.new do |spec|
spec.name = 'LetterAvatarKit'
spec.platform = :ios, '8.0'
spec.version = '1.2.1'
spec.version = '1.2.2'
spec.license = { :type => 'MIT' }
spec.homepage = 'https://github.com/vpeschenkov/LetterAvatarKit'
spec.authors = { 'Viktor Peschenkov' => 'v.peschenkov@gmail.com' }
Expand Down
30 changes: 26 additions & 4 deletions LetterAvatarKit.xcodeproj/project.pbxproj
Expand Up @@ -213,6 +213,7 @@
EB8EC4051F85929E000B7641 /* Frameworks */,
EB8EC4061F85929E000B7641 /* Headers */,
EB8EC4071F85929E000B7641 /* Resources */,
3505688D24215D6500FB2E54 /* SwiftLint */,
);
buildRules = (
);
Expand Down Expand Up @@ -307,6 +308,27 @@
};
/* End PBXResourcesBuildPhase section */

/* Begin PBXShellScriptBuildPhase section */
3505688D24215D6500FB2E54 /* SwiftLint */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
);
inputFileListPaths = (
);
inputPaths = (
);
name = SwiftLint;
outputFileListPaths = (
);
outputPaths = (
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "if which swiftlint >/dev/null; then\n swiftlint\nelse\n echo \"warning: SwiftLint not installed, download from https://github.com/realm/SwiftLint\"\nfi\n";
};
/* End PBXShellScriptBuildPhase section */

/* Begin PBXSourcesBuildPhase section */
EB8EC4041F85929E000B7641 /* Sources */ = {
isa = PBXSourcesBuildPhase;
Expand Down Expand Up @@ -485,8 +507,8 @@
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
MARKETING_VERSION = 1.2.1;
PRODUCT_BUNDLE_IDENTIFIER = org.peschenkov.LetterAvatarKit;
MARKETING_VERSION = 1.2.2;
PRODUCT_BUNDLE_IDENTIFIER = com.github.vpeschenkov.LetterAvatarKit;
PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)";
SKIP_INSTALL = YES;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
Expand All @@ -510,8 +532,8 @@
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
MARKETING_VERSION = 1.2.1;
PRODUCT_BUNDLE_IDENTIFIER = org.peschenkov.LetterAvatarKit;
MARKETING_VERSION = 1.2.2;
PRODUCT_BUNDLE_IDENTIFIER = com.github.vpeschenkov.LetterAvatarKit;
PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)";
SKIP_INSTALL = YES;
SWIFT_VERSION = 5.0;
Expand Down
1 change: 1 addition & 0 deletions LetterAvatarKit/Extensions/Character+LetterAvatarKit.swift
Expand Up @@ -26,6 +26,7 @@
import Foundation

internal extension Character {

var ASCIIValue: Int {
let unicode = String(self).unicodeScalars
return Int(unicode[unicode.startIndex].value)
Expand Down
18 changes: 12 additions & 6 deletions LetterAvatarKit/Extensions/UIColor+LetterAvatarKit.swift
Expand Up @@ -51,8 +51,9 @@ func LKUIColorByRGB(red: CGFloat, green: CGFloat, blue: CGFloat) -> UIColor {
}

extension UIColor {

private struct ColorKey {
static var value = "org.peschenkov.LetterAvatarKit.UIColor.colors"
static var propertyReference = "org.peschenkov.LetterAvatarKit.UIColor.colors"
}
/// Colors from http://flatuicolors.com/
public enum HEXColor {
Expand Down Expand Up @@ -95,8 +96,8 @@ extension UIColor {
/// ASBESTOS
static let asbestosColor = 0x7F8C8D
}
static public var colors: [ UIColor ] {
var colors = objc_getAssociatedObject(self, &ColorKey.value)
static public var colors: [UIColor] {
var colors = objc_getAssociatedObject(self, &ColorKey.propertyReference)
if colors == nil {
colors = [
LKUIColorByHEX(HEXColor.turquoiseColor),
Expand All @@ -119,9 +120,14 @@ extension UIColor {
LKUIColorByHEX(HEXColor.silverColor),
LKUIColorByHEX(HEXColor.asbestosColor)
]
objc_setAssociatedObject(self, &ColorKey.value, colors, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
return colors as? [ UIColor ] ?? []
objc_setAssociatedObject(
self,
&ColorKey.propertyReference,
colors,
.OBJC_ASSOCIATION_RETAIN_NONATOMIC
)
return colors as? [UIColor] ?? []
}
return colors as? [ UIColor ] ?? []
return colors as? [UIColor] ?? []
}
}
18 changes: 9 additions & 9 deletions LetterAvatarKit/LetterAvatarBuilder.swift
Expand Up @@ -31,7 +31,7 @@ open class LetterAvatarBuilder: NSObject {
/// Makes a letter-based avatar image by using a given configuration.
///
/// - Parameters:
/// - configuration: The configuration that is used to draw a
/// - configuration: A configuration that is used to draw a
/// letter-based avatar image.
///
/// - Returns: Returns whether an instance of UIImage or nil.
Expand All @@ -47,7 +47,7 @@ open class LetterAvatarBuilder: NSObject {
}
let usernameInfo = UsernameInfo(
username: username,
singleLetter: configuration.isSingleLettered
singleLetter: configuration.useSingleLetter
)
var colorIndex = 0
if colors.count > 1 {
Expand All @@ -69,15 +69,15 @@ open class LetterAvatarBuilder: NSObject {
backgroundColor: CGColor
) -> UIImage? {
let rect = CGRect(x: 0.0, y: 0.0, width: configuration.size.width, height: configuration.size.height)
UIGraphicsBeginImageContextWithOptions(rect.size, false, UIScreen.main.scale)
UIGraphicsBeginImageContextWithOptions(rect.size, configuration.isOpaque, UIScreen.main.scale)
if let context = UIGraphicsGetCurrentContext() {
let borderWidth = configuration.borderWidth
let borderColor = configuration.borderColor.cgColor
let strokeRect = rect.insetBy(dx: borderWidth * 0.5, dy: borderWidth * 0.5)
context.setFillColor(backgroundColor)
context.setStrokeColor(borderColor)
context.setLineWidth(borderWidth)
if (configuration.isCircle) {
if configuration.circle {
context.fillEllipse(in: rect)
context.strokeEllipse(in: strokeRect)
} else {
Expand Down Expand Up @@ -111,7 +111,7 @@ open class LetterAvatarBuilder: NSObject {

private func makeFitFont(withFont font: UIFont?, forSize size: CGSize) -> UIFont {
guard let font = font else {
return UIFont.systemFont(ofSize:min(size.height, size.width) / 2.0)
return UIFont.systemFont(ofSize: min(size.height, size.width) / 2.0)
}
let fitFont = font.withSize(min(size.height, size.width) / 2.0)
return fitFont.pointSize < font.pointSize ? fitFont : font
Expand All @@ -129,7 +129,7 @@ private class UsernameInfo {
}

private let username: String
private let isSingleLettered: Bool
private let useSingleLetter: Bool

private typealias InfoContainer = (letters: String, value: Int)
private lazy var userInfo: InfoContainer = {
Expand All @@ -139,7 +139,7 @@ private class UsernameInfo {
let components = username.components(separatedBy: " ")
// If there are whether two words or more
if components.count > 1 {
if !isSingleLettered {
if !useSingleLetter {
for component in components.prefix(3) {
if let letter = component.first {
letters.append(letter)
Expand All @@ -165,7 +165,7 @@ private class UsernameInfo {
// If single Letter is passed as false but the string is a single char,
// this line fails due to out of bounds exception.
// https://github.com/vpeschenkov/LetterAvatarKit/issues/11
if !isSingleLettered && component.count >= 2 {
if !useSingleLetter && component.count >= 2 {
// Process the second name letter
let startIndex = component.index(after: component.startIndex)
let endIndex = component.index(component.startIndex, offsetBy: 2)
Expand All @@ -183,6 +183,6 @@ private class UsernameInfo {

init(username: String, singleLetter: Bool) {
self.username = username
self.isSingleLettered = singleLetter
self.useSingleLetter = singleLetter
}
}
29 changes: 16 additions & 13 deletions LetterAvatarKit/LetterAvatarBuilderConfiguration.swift
Expand Up @@ -29,35 +29,38 @@ import Foundation
/// Uses for configurating a LetterAvatarBuilder instance.
@objc(LKLetterAvatarBuilderConfiguration)
open class LetterAvatarBuilderConfiguration: NSObject {
/// The size of an avatar image.
/// A size of an avatar image.
@objc(size)
open var size: CGSize = CGSize(width: 80, height: 80)
/// The username.
/// An username.
@objc(username)
open var username: String?
/// The flag that indicates of using single only one letter, otherwise,
/// A flag that indicates of using single only one letter, otherwise,
/// as much as wil be possible to obtain. But no more than 3 letters.
@objc(isSingleLettered)
open var isSingleLettered: Bool = false
/// The letters font.
@objc(useSingleLetter)
open var useSingleLetter: Bool = false
/// Letters font.
@objc(lettersFont)
open var lettersFont: UIFont?
/// The colors are used to draw image.
/// Colors that are used to draw image.
@objc(lettersColor)
open var lettersColor: UIColor = LKUIColorByRGB(red: 236, green: 240, blue: 241)
/// The background colors of the image.
/// Background colors of the image.
@objc(backgroundColors)
open var backgroundColors: [UIColor] = UIColor.colors
/// The letters font attributes.
/// Letters font attributes.
@objc(lettersFontAttributes)
open var lettersFontAttributes: [NSAttributedString.Key: Any]?
/// Indicates whether to generate circle or square image.
@objc(isCircle)
open var isCircle: Bool = false
/// The border width of the image.
@objc(circle)
open var circle: Bool = false
/// A border width of the image.
@objc(borderWidth)
open var borderWidth: CGFloat = 0.0
/// The border color of the image.
/// A border color of the image.
@objc(borderColor)
open var borderColor: UIColor = UIColor.white
/// A Boolean flag indicating whether the avatar is opaque.
@objc(isOpaque)
open var isOpaque: Bool = false
}
14 changes: 10 additions & 4 deletions LetterAvatarKit/LetterAvatarMaker.swift
Expand Up @@ -56,8 +56,8 @@ extension LetterAvatarMaker: LetterAvatarMakerExtendable {
}

@discardableResult
public func setSingleLettered(_ isSingleLettered: Bool) -> LetterAvatarMakerExtendable {
configuration.isSingleLettered = isSingleLettered
public func useSingleLetter(_ useSingleLetter: Bool) -> LetterAvatarMakerExtendable {
configuration.useSingleLetter = useSingleLetter
return self
}

Expand Down Expand Up @@ -88,8 +88,14 @@ extension LetterAvatarMaker: LetterAvatarMakerExtendable {
}

@discardableResult
public func setCircle(_ isCircle: Bool) -> LetterAvatarMakerExtendable {
configuration.isCircle = isCircle
public func setCircle(_ circle: Bool) -> LetterAvatarMakerExtendable {
configuration.circle = circle
return self
}

@discardableResult
public func drawOpaqueBackground(_ isOpaque: Bool) -> LetterAvatarMakerExtendable {
configuration.isOpaque = isOpaque
return self
}

Expand Down
17 changes: 3 additions & 14 deletions LetterAvatarKit/LetterAvatarMakerExtendable.swift
Expand Up @@ -27,30 +27,19 @@ import UIKit
import Foundation

public protocol LetterAvatarMakerExtendable: NSObjectProtocol {
/// Sets the image size which is used to draw the image.
func setSize(_ size: CGSize) -> LetterAvatarMakerExtendable
/// Sets a username size which is used to draw the image.
func setUsername(_ username: String) -> LetterAvatarMakerExtendable
/// Sets the letter font which is used to draw the letters.
func setLettersFont(_ lettersFont: UIFont?) -> LetterAvatarMakerExtendable
/// Sets the letter color which is used to draw the letters.
func setLettersColor(_ lettersColor: UIColor) -> LetterAvatarMakerExtendable
/// Sets whether to generate single-lettered image or not.
func setSingleLettered(_ isSingleLettered: Bool) -> LetterAvatarMakerExtendable
/// Sets the letter background colors which are used to draw the letters.
func useSingleLetter(_ useSingleLetter: Bool) -> LetterAvatarMakerExtendable
func setBackgroundColors(_ backgroundColors: [UIColor]) -> LetterAvatarMakerExtendable
/// Sets the letter font attributes which are used to draw the letters.
func setLettersFontAttributes(
_ lettersFontAttributes: [NSAttributedString.Key: Any]?
) -> LetterAvatarMakerExtendable
// Sets whether to generate circle or square image.
func setCircle(_ isCircle: Bool) -> LetterAvatarMakerExtendable
/// Sets the image border width which is used to draw the image.
func setCircle(_ circle: Bool) -> LetterAvatarMakerExtendable
func setBorderWidth(_ borderWidth: CGFloat) -> LetterAvatarMakerExtendable
/// Sets the image border color which is used to draw the image.
func setBorderColor(_ borderColor: UIColor) -> LetterAvatarMakerExtendable
/// Builds the image using the `maker` values
func drawOpaqueBackground(_ isOpaque: Bool) -> LetterAvatarMakerExtendable
func build(maker: (LetterAvatarBuilderConfiguration) -> Void) -> UIImage?
/// Builds the image using self parameters
func build() -> UIImage?
}
2 changes: 1 addition & 1 deletion LetterAvatarKitObjcExample/Podfile.lock
Expand Up @@ -13,4 +13,4 @@ SPEC CHECKSUMS:

PODFILE CHECKSUM: acab86cc8cc5517bd2b945abae924096efac0c35

COCOAPODS: 1.7.1
COCOAPODS: 1.8.4
2 changes: 1 addition & 1 deletion LetterAvatarKitObjcExample/Pods/Manifest.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 218b48b

Please sign in to comment.