Skip to content

Commit

Permalink
Added textSize property and left top right bottom tag padding
Browse files Browse the repository at this point in the history
  • Loading branch information
scaraux committed Jul 12, 2018
1 parent 34f0fc3 commit 732ade4
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 37 deletions.
12 changes: 9 additions & 3 deletions Example/Hashtags.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,8 @@
};
607FACE41AFB9204008FA782 = {
CreatedOnToolsVersion = 6.3.1;
DevelopmentTeam = DBEPTECPFM;
LastSwiftMigration = 0900;
ProvisioningStyle = Manual;
TestTargetID = 607FACCF1AFB9204008FA782;
};
};
Expand Down Expand Up @@ -531,6 +531,7 @@
baseConfigurationReference = 917CC9E18418EF1069980A51 /* Pods-Hashtags_Example.debug.xcconfig */;
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CODE_SIGN_IDENTITY = "iPhone Developer";
CODE_SIGN_STYLE = Manual;
DEVELOPMENT_TEAM = "";
INFOPLIST_FILE = Hashtags/Info.plist;
Expand All @@ -550,6 +551,7 @@
baseConfigurationReference = 6C817035909AC2664FE21E52 /* Pods-Hashtags_Example.release.xcconfig */;
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CODE_SIGN_IDENTITY = "iPhone Developer";
CODE_SIGN_STYLE = Manual;
DEVELOPMENT_TEAM = "";
INFOPLIST_FILE = Hashtags/Info.plist;
Expand All @@ -568,7 +570,8 @@
isa = XCBuildConfiguration;
baseConfigurationReference = CD21704F302ED2D69FFEDF25 /* Pods-Hashtags_Tests.debug.xcconfig */;
buildSettings = {
DEVELOPMENT_TEAM = DBEPTECPFM;
CODE_SIGN_STYLE = Manual;
DEVELOPMENT_TEAM = "";
FRAMEWORK_SEARCH_PATHS = (
"$(SDKROOT)/Developer/Library/Frameworks",
"$(inherited)",
Expand All @@ -581,6 +584,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "org.cocoapods.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
SWIFT_SWIFT3_OBJC_INFERENCE = Default;
SWIFT_VERSION = 4.0;
};
Expand All @@ -590,7 +594,8 @@
isa = XCBuildConfiguration;
baseConfigurationReference = 0C873CC375B45B25507BC693 /* Pods-Hashtags_Tests.release.xcconfig */;
buildSettings = {
DEVELOPMENT_TEAM = DBEPTECPFM;
CODE_SIGN_STYLE = Manual;
DEVELOPMENT_TEAM = "";
FRAMEWORK_SEARCH_PATHS = (
"$(SDKROOT)/Developer/Library/Frameworks",
"$(inherited)",
Expand All @@ -599,6 +604,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "org.cocoapods.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
SWIFT_SWIFT3_OBJC_INFERENCE = Default;
SWIFT_VERSION = 4.0;
};
Expand Down
9 changes: 5 additions & 4 deletions Example/Hashtags/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="14113" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="CHW-cI-DMX">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="14269.12" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="CHW-cI-DMX">
<device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14088"/>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14252.5"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
Expand Down Expand Up @@ -37,7 +38,7 @@
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<stackView opaque="NO" contentMode="scaleToFill" spacing="30" translatesAutoresizingMaskIntoConstraints="NO" id="N5r-yW-KWS">
<rect key="frame" x="77" y="318" width="221" height="30"/>
<rect key="frame" x="77" y="318.5" width="221" height="30"/>
<subviews>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="QLS-ho-fYv">
<rect key="frame" x="0.0" y="0.0" width="111" height="30"/>
Expand Down Expand Up @@ -99,7 +100,7 @@
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Hashtags, using Storyboard" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="IHt-4b-UPZ" userLabel="Title">
<rect key="frame" x="48" y="114" width="280" height="30"/>
<rect key="frame" x="47.5" y="114" width="280" height="30"/>
<fontDescription key="fontDescription" type="system" weight="thin" pointSize="25"/>
<color key="textColor" red="0.99999600649999998" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<nil key="highlightedColor"/>
Expand Down
2 changes: 1 addition & 1 deletion Example/Pods/Pods.xcodeproj/project.pbxproj

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

2 changes: 1 addition & 1 deletion Hashtags.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = 'Hashtags'
s.version = '0.1.0'
s.version = '0.1.1'
s.summary = 'A swift library for displaying hashtags'
s.swift_version = '4.0'
s.description = "Display a list of hashtags dynamically."
Expand Down
15 changes: 10 additions & 5 deletions Hashtags/Classes/HashtagCollectionViewCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ open class HashtagCollectionViewCell: UICollectionViewCell {

lazy var wordLabel : UILabel = {
let lbl = UILabel()
lbl.font = UIFont.systemFont(ofSize: 14)
lbl.textColor = UIColor.white
lbl.textAlignment = .center
lbl.translatesAutoresizingMaskIntoConstraints = false
return lbl
}()

Expand All @@ -39,12 +40,9 @@ open class HashtagCollectionViewCell: UICollectionViewCell {

open func setup() {
self.clipsToBounds = true
self.wordLabel.textAlignment = .center

self.addSubview(wordLabel)

self.wordLabel.translatesAutoresizingMaskIntoConstraints = false

// Padding left
self.paddingLeftConstraint = self.wordLabel.leadingAnchor.constraint(equalTo: self.leadingAnchor)
self.paddingLeftConstraint!.isActive = true
Expand All @@ -59,6 +57,11 @@ open class HashtagCollectionViewCell: UICollectionViewCell {
self.paddingRightConstraint!.isActive = true
}

open override func prepareForInterfaceBuilder() {
self.wordLabel.text = ""
super.prepareForInterfaceBuilder()
}

open func configureWithTag(tag: HashTag, configuration: HashtagConfiguration) {
self.hashtag = tag
wordLabel.text = tag.text
Expand All @@ -68,8 +71,10 @@ open class HashtagCollectionViewCell: UICollectionViewCell {
self.paddingBottomConstraint!.constant = -1 * configuration.paddingBottom
self.paddingRightConstraint!.constant = -1 * configuration.paddingRight

self.layer.cornerRadius = configuration.cornerRadius
self.backgroundColor = configuration.backgroundColor

self.wordLabel.textColor = configuration.textColor
self.layer.cornerRadius = configuration.cornerRadius
self.wordLabel.font = UIFont.systemFont(ofSize: configuration.textSize)
}
}
1 change: 1 addition & 0 deletions Hashtags/Classes/HashtagConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ open class HashtagConfiguration {
var removeButtonSize: CGFloat = 0.0
var removeButtonSpacing: CGFloat = 0.0
var cornerRadius: CGFloat = 0.0
var textSize: CGFloat = 0.0
var textColor = UIColor()
var backgroundColor = UIColor()
}
55 changes: 38 additions & 17 deletions Hashtags/Classes/HashtagView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,28 @@ open class HashtagView: UIView {
// MARK: Hashtag cell padding

@IBInspectable
open var tagPadding: CGFloat = 5.0 {
open var tagPaddingLeft: CGFloat = 5.0 {
didSet {
self.collectionView.reloadData()
}
}

@IBInspectable
open var tagPaddingRight: CGFloat = 5.0 {
didSet {
self.collectionView.reloadData()
}
}

@IBInspectable
open var tagPaddingTop: CGFloat = 5.0 {
didSet {
self.collectionView.reloadData()
}
}

@IBInspectable
open var tagPaddingBottom: CGFloat = 5.0 {
didSet {
self.collectionView.reloadData()
}
Expand All @@ -88,6 +109,13 @@ open class HashtagView: UIView {
}
}

@IBInspectable
open var textSize: CGFloat = 14.0 {
didSet {
self.collectionView.reloadData()
}
}

@IBInspectable
open var tagBackgroundColor: UIColor = .lightGray {
didSet {
Expand Down Expand Up @@ -179,14 +207,15 @@ open class HashtagView: UIView {

let configuration = HashtagConfiguration()

configuration.paddingLeft = self.tagPadding
configuration.paddingRight = self.tagPadding
configuration.paddingTop = self.tagPadding
configuration.paddingBottom = self.tagPadding
configuration.paddingLeft = self.tagPaddingLeft
configuration.paddingRight = self.tagPaddingRight
configuration.paddingTop = self.tagPaddingTop
configuration.paddingBottom = self.tagPaddingBottom
configuration.removeButtonSize = self.removeButtonSize
configuration.removeButtonSpacing = self.removeButtonSpacing
configuration.backgroundColor = self.tagBackgroundColor
configuration.cornerRadius = self.tagCornerRadius
configuration.textSize = self.textSize
configuration.textColor = self.tagTextColor

return configuration
Expand Down Expand Up @@ -318,24 +347,16 @@ extension HashtagView: UICollectionViewDelegateFlowLayout {

public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let hashtag: HashTag = self.hashtags[indexPath.item]
let textDimensions = hashtag.text.sizeOfString(usingFont: UIFont.systemFont(ofSize: 14.0))
let wordSize = hashtag.text.sizeOfString(usingFont: UIFont.systemFont(ofSize: self.textSize))

var calculatedHeight = CGFloat()
var calculatedWidth = CGFloat()

let configuration = makeConfiguration()

calculatedHeight = configuration.paddingTop + textDimensions.height + configuration.paddingBottom
calculatedHeight = self.tagPaddingTop + wordSize.height + self.tagPaddingBottom
calculatedWidth = self.tagPaddingLeft + wordSize.width + self.tagPaddingRight + 1

if hashtag.isRemovable {
calculatedWidth =
configuration.paddingLeft
+ textDimensions.width
+ configuration.removeButtonSpacing
+ configuration.removeButtonSize
+ configuration.paddingRight
} else {
calculatedWidth = configuration.paddingLeft + textDimensions.width + configuration.paddingRight
calculatedWidth += self.removeButtonSize + self.removeButtonSpacing
}
return CGSize(width: calculatedWidth, height: calculatedHeight)
}
Expand Down
12 changes: 6 additions & 6 deletions Hashtags/Classes/RemovableHashtagCollectionViewCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,17 @@ open class RemovableHashtagCollectionViewCell: UICollectionViewCell {

lazy var wordLabel : UILabel = {
let lbl = UILabel()
lbl.font = UIFont.systemFont(ofSize: 14)
lbl.textColor = UIColor.white
lbl.textAlignment = .center
lbl.translatesAutoresizingMaskIntoConstraints = false
return lbl
}()

var removeButton : UIButton = {
let btn = UIButton()
let bundle = Bundle(for: RemovableHashtagCollectionViewCell.self)
let removeIcon = UIImage(named: "close", in: bundle, compatibleWith: nil)!
btn.translatesAutoresizingMaskIntoConstraints = false
btn.setImage(removeIcon, for: .normal)
btn.imageView?.contentMode = .scaleAspectFit
btn.imageView?.tintColor = UIColor.black.withAlphaComponent(0.9)
Expand All @@ -57,14 +59,10 @@ open class RemovableHashtagCollectionViewCell: UICollectionViewCell {

private func setup() {
self.clipsToBounds = true
self.wordLabel.textAlignment = .center

self.addSubview(wordLabel)
self.addSubview(removeButton)

self.wordLabel.translatesAutoresizingMaskIntoConstraints = false
self.removeButton.translatesAutoresizingMaskIntoConstraints = false

// Text Width
self.wordLabel.widthAnchor.constraint(greaterThanOrEqualToConstant: 10).isActive = true
// Padding left
Expand Down Expand Up @@ -116,8 +114,10 @@ extension RemovableHashtagCollectionViewCell {
self.removeButtonSpacingConstraint!.constant = configuration.removeButtonSpacing
self.removeButtonWidthConstraint!.constant = configuration.removeButtonSize

self.layer.cornerRadius = configuration.cornerRadius
self.backgroundColor = configuration.backgroundColor

self.wordLabel.textColor = configuration.textColor
self.layer.cornerRadius = configuration.cornerRadius
self.wordLabel.font = UIFont.systemFont(ofSize: configuration.textSize)
}
}
5 changes: 5 additions & 0 deletions Untitled
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function review() {
console.log("toto");
}

module.exports = review;

0 comments on commit 732ade4

Please sign in to comment.