Skip to content

Commit 37e762e

Browse files
kostassitephimage
authored andcommitted
[ADD] titleColor, titleShadowColor, backgroundImage to Button.State
1 parent ac9f1e6 commit 37e762e

File tree

3 files changed

+72
-2
lines changed

3 files changed

+72
-2
lines changed

Sources/Models/Views/Controls/Button.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,23 @@ public struct Button: IBDecodable, ControlProtocol, IBIdentifiable {
5050
public struct State: IBDecodable, IBKeyable {
5151
public let key: String?
5252
public let title: String?
53-
public let color: Color?
53+
public let titleColor: Color?
54+
public let titleShadowColor: Color?
5455
public let image: String?
56+
public let backgroundImage: String?
5557
public let catalog: String?
5658

5759
static func decode(_ xml: XMLIndexerType) throws -> Button.State {
5860
let container = xml.container(keys: CodingKeys.self)
61+
let colorsContainer = xml.container(keys: ExternalCodingKeys.self)
62+
.nestedContainerIfPresent(of: .color, keys: ColorsCodingKeys.self)
5963
return State.init(
6064
key: try container.attribute(of: .key),
6165
title: container.attributeIfPresent(of: .title),
62-
color: container.elementIfPresent(of: .color),
66+
titleColor: colorsContainer?.withAttributeElement(.key, CodingKeys.titleColor.stringValue),
67+
titleShadowColor: colorsContainer?.withAttributeElement(.key, CodingKeys.titleShadowColor.stringValue),
6368
image: container.attributeIfPresent(of: .image),
69+
backgroundImage: container.attributeIfPresent(of: .backgroundImage),
6470
catalog: container.attributeIfPresent(of: .catalog)
6571
)
6672
}

Tests/IBDecodableTests/Tests.swift

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,41 @@ class Tests: XCTestCase {
452452
}
453453
}
454454

455+
func testButtonStates() {
456+
let url = self.url(forResource: "Button", withExtension: "xib")
457+
458+
do {
459+
let file = try XibFile(url: url)
460+
461+
let rootView = file.document.views?.first?.view
462+
XCTAssertNotNil(rootView, "There should be a root view")
463+
464+
let button = rootView as? Button
465+
XCTAssertNotNil(button, "There should be a button")
466+
XCTAssertEqual(button?.elementClass, "UIButton")
467+
468+
let states = button?.state
469+
XCTAssertEqual(states?.count, 2, "Should find 2 states")
470+
471+
let normalState = states?.first(where: {$0.key == "normal"})
472+
XCTAssertEqual(normalState?.title, "Normal title")
473+
XCTAssertNil(normalState?.image)
474+
XCTAssertNil(normalState?.backgroundImage)
475+
XCTAssertNotNil(normalState?.titleColor)
476+
XCTAssertNotNil(normalState?.titleShadowColor)
477+
478+
let selectedState = states?.first(where: {$0.key == "selected"})
479+
XCTAssertEqual(selectedState?.title, "selected text")
480+
XCTAssertEqual(selectedState?.image, "testImage.png")
481+
XCTAssertEqual(selectedState?.backgroundImage, "backImage.png")
482+
XCTAssertNil(selectedState?.titleColor)
483+
XCTAssertNil(selectedState?.titleShadowColor)
484+
485+
} catch {
486+
XCTFail("\(error) \(url)")
487+
}
488+
}
489+
455490
func testActivityIndicatorView() {
456491
let url = self.url(forResource: "ViewWithActivityIndicatorView", withExtension: "xib")
457492
do {

Tests/Resources/Button.xib

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="17701" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES">
3+
<device id="retina6_1" orientation="portrait" appearance="light"/>
4+
<dependencies>
5+
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="17703"/>
6+
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
7+
</dependencies>
8+
<objects>
9+
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner"/>
10+
<placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
11+
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" lineBreakMode="middleTruncation" id="NLs-WP-EeK">
12+
<rect key="frame" x="0.0" y="0.0" width="93" height="34"/>
13+
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
14+
<fontDescription key="fontDescription" type="system" pointSize="19"/>
15+
<state key="normal" title="Normal title">
16+
<color key="titleColor" red="0.70451136999999997" green="0.0" blue="0.0" alpha="1" colorSpace="calibratedRGB"/>
17+
<color key="titleShadowColor" red="0.70451136999999997" green="1" blue="1" alpha="1" colorSpace="calibratedRGB"/>
18+
</state>
19+
<state key="selected" title="selected text" image="testImage.png" backgroundImage="backImage.png"/>
20+
<point key="canvasLocation" x="-154" y="-3"/>
21+
</button>
22+
</objects>
23+
<resources>
24+
<image name="backImage.png" width="128" height="128"/>
25+
<image name="square.and.arrow.down" catalog="system" width="121" height="128"/>
26+
<image name="square.and.arrow.up" catalog="system" width="115" height="128"/>
27+
<image name="testImage.png" width="128" height="128"/>
28+
</resources>
29+
</document>

0 commit comments

Comments
 (0)