Skip to content

Commit

Permalink
Intrinisic Content Override
Browse files Browse the repository at this point in the history
Adjust font to smaller width
  • Loading branch information
agibson73 committed Feb 21, 2017
1 parent 85af3e6 commit 55c544d
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 12 deletions.
Binary file not shown.
55 changes: 51 additions & 4 deletions ICONButton/AGIconButton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,16 @@ import UIKit
}

private func setUpView(){



var usedWidth : CGFloat = self.intrinsicContentSize.width

if bounds.width < usedWidth{
usedWidth = bounds.width
}



if highlightView == nil{
highlightView = UIView(frame: self.bounds)
Expand All @@ -148,8 +158,6 @@ import UIKit
}


iconImageView.frame = CGRect(x: 0, y: 0, width: imageSize, height: imageSize)


if mainSpacer == nil{
mainSpacer = UIView(frame: CGRect(x: 0, y: 0, width: imagePadding, height: self.bounds.height))
Expand All @@ -163,24 +171,35 @@ import UIKit
iconLabel = UILabel()
iconLabel.isUserInteractionEnabled = false
self.addSubview(iconLabel)

}

iconLabel.font = UIFont.systemFont(ofSize: iconTextSize)
iconLabel.text = iconText
iconLabel.textColor = iconTextColor
iconLabel.sizeToFit()



let maxImageHeight = min(self.bounds.height - padding, imageSize)

if maxImageHeight + imagePadding + iconLabel.bounds.width + padding * 2 > usedWidth{
iconLabel.frame = CGRect(x: 0, y: 0, width: self.bounds.width - iconImageView.bounds.width - imagePadding - padding * 2, height: iconLabel.bounds.height)
iconLabel.fitFontForSize()

}


let maxWidth = (self.bounds.width - iconLabel.intrinsicContentSize.width - maxImageHeight - imagePadding) / 2

let maxWidth = (self.bounds.width - iconLabel.bounds.width - maxImageHeight - imagePadding) / 2

switch alignment {
case 0:
//intrinsic left
iconImageView.frame = CGRect(x:padding, y: self.bounds.midY - maxImageHeight/2,width:maxImageHeight, height: maxImageHeight)
mainSpacer.frame = CGRect(x: maxImageHeight + padding, y: 0, width: imagePadding, height: self.bounds.height)
iconLabel.frame = CGRect(x: maxImageHeight + imagePadding + padding, y: 0, width: iconLabel.frame.width + padding, height: self.bounds.height)
iconLabel.frame = CGRect(x: maxImageHeight + imagePadding + padding, y: 0, width: iconLabel.frame.width, height: bounds.height)

break
case 1:
//intrinsic center
Expand Down Expand Up @@ -210,6 +229,8 @@ import UIKit
mainSpacer.frame = CGRect(x: maxWidth + maxImageHeight, y: 0, width: imagePadding, height: self.bounds.height)
iconLabel.frame = CGRect(x: maxWidth + maxImageHeight + imagePadding, y: 0, width: iconLabel.frame.width, height: self.bounds.height)
}




}
Expand Down Expand Up @@ -273,4 +294,30 @@ import UIKit
}
}

extension UILabel {

func fitFontForSize( minFontSize : CGFloat = 2.0, maxFontSize : CGFloat = 300.0, accuracy : CGFloat = 1.0) {
var maxFontSize = maxFontSize
var minFontSize = minFontSize
assert(maxFontSize > minFontSize)
layoutIfNeeded() // Can be removed at your own discretion
let constrainedSize = bounds.size
while maxFontSize - minFontSize > accuracy {
let midFontSize : CGFloat = ((minFontSize + maxFontSize) / 2)
font = font.withSize(midFontSize)
sizeToFit()
let checkSize : CGSize = bounds.size
if checkSize.height < constrainedSize.height && checkSize.width < constrainedSize.width {
minFontSize = midFontSize
} else {
maxFontSize = midFontSize
}
}
font = font.withSize(minFontSize)
sizeToFit()
layoutIfNeeded() // Can be removed at your own discretion
}

}


38 changes: 30 additions & 8 deletions ICONButton/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -22,46 +22,62 @@
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="NZK-1X-Vq4" customClass="AGIconButton" customModule="ICONButton" customModuleProvider="target">
<rect key="frame" x="111" y="48" width="154.5" height="31.5"/>
<rect key="frame" x="138" y="48" width="100" height="31.5"/>
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="calibratedWhite"/>
<constraints>
<constraint firstAttribute="width" constant="100" id="HWJ-Gd-zSE"/>
<constraint firstAttribute="height" constant="31.5" id="eOB-Pl-hz6"/>
</constraints>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="image" keyPath="iconImage" value="ProfileTabbar"/>
<userDefinedRuntimeAttribute type="string" keyPath="iconText" value="Tap Me Please"/>
<userDefinedRuntimeAttribute type="number" keyPath="imageSize">
<real key="value" value="17"/>
<real key="value" value="14"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="number" keyPath="iconTextSize">
<real key="value" value="18"/>
<real key="value" value="17"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="number" keyPath="alignment">
<integer key="value" value="1"/>
</userDefinedRuntimeAttribute>
</userDefinedRuntimeAttributes>
<connections>
<action selector="buttonDidTouchUpInside:" destination="BYZ-38-t0r" eventType="touchUpInside" id="abJ-jL-onQ"/>
</connections>
</view>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="wTC-ec-D5h" customClass="AGIconButton" customModule="ICONButton" customModuleProvider="target">
<rect key="frame" x="67" y="99.5" width="242" height="50"/>
<rect key="frame" x="73" y="99.5" width="230" height="38"/>
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="calibratedWhite"/>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="image" keyPath="iconImage" value="Audio"/>
<userDefinedRuntimeAttribute type="number" keyPath="alignment">
<integer key="value" value="2"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="string" keyPath="iconText" value="Intrinsic Centered Inverted"/>
<userDefinedRuntimeAttribute type="number" keyPath="imageSize">
<real key="value" value="28"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="number" keyPath="imagePadding">
<real key="value" value="10"/>
</userDefinedRuntimeAttribute>
</userDefinedRuntimeAttributes>
</view>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="Fm7-19-DhQ" customClass="AGIconButton" customModule="ICONButton" customModuleProvider="target">
<rect key="frame" x="67" y="172.5" width="242" height="50"/>
<rect key="frame" x="73" y="160.5" width="230" height="38"/>
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="calibratedWhite"/>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="image" keyPath="iconImage" value="Audio"/>
<userDefinedRuntimeAttribute type="number" keyPath="alignment">
<integer key="value" value="2"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="string" keyPath="iconText" value="Intrinsic Centered Inverted"/>
<userDefinedRuntimeAttribute type="number" keyPath="imageSize">
<real key="value" value="28"/>
</userDefinedRuntimeAttribute>
</userDefinedRuntimeAttributes>
</view>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="V6m-yU-tGz" customClass="AGIconButton" customModule="ICONButton" customModuleProvider="target">
<rect key="frame" x="39" y="250.5" width="299.5" height="50"/>
<rect key="frame" x="49" y="226.5" width="279.5" height="30"/>
<color key="backgroundColor" red="0.0" green="0.57171165940000002" blue="0.8294764161" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="image" keyPath="iconImage" value="Audio"/>
Expand All @@ -75,10 +91,13 @@
<color key="value" white="1" alpha="1" colorSpace="calibratedWhite"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="string" keyPath="iconText" value="Icon Button With Equal Widths"/>
<userDefinedRuntimeAttribute type="number" keyPath="imageSize">
<real key="value" value="20"/>
</userDefinedRuntimeAttribute>
</userDefinedRuntimeAttributes>
</view>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="ACY-p5-tM7" customClass="AGIconButton" customModule="ICONButton" customModuleProvider="target">
<rect key="frame" x="39" y="322.5" width="299.5" height="50"/>
<rect key="frame" x="49" y="278.5" width="279.5" height="30"/>
<color key="backgroundColor" red="0.0" green="0.57171165940000002" blue="0.8294764161" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="image" keyPath="iconImage" value="Audio"/>
Expand All @@ -92,10 +111,13 @@
<userDefinedRuntimeAttribute type="color" keyPath="iconTextColor">
<color key="value" white="1" alpha="1" colorSpace="calibratedWhite"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="number" keyPath="imageSize">
<real key="value" value="20"/>
</userDefinedRuntimeAttribute>
</userDefinedRuntimeAttributes>
</view>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="9c8-g3-KWj" customClass="AGIconButton" customModule="ICONButton" customModuleProvider="target">
<rect key="frame" x="115" y="410.5" width="147.5" height="50"/>
<rect key="frame" x="115" y="346.5" width="147.5" height="50"/>
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="calibratedWhite"/>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="image" keyPath="iconImage" value="Audio"/>
Expand Down

0 comments on commit 55c544d

Please sign in to comment.