Skip to content

Commit

Permalink
Create TitleSubtitleView component
Browse files Browse the repository at this point in the history
  • Loading branch information
Emerson Mendes Filho committed May 23, 2021
1 parent 460a8a4 commit 97cac76
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 24 deletions.
4 changes: 4 additions & 0 deletions ViewCodeGuide/ViewCodeGuide.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
C69E437326582D9C004E30A6 /* ViewCodeGuideTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = C69E437226582D9C004E30A6 /* ViewCodeGuideTests.swift */; };
C69E437E26582D9D004E30A6 /* ViewCodeGuideUITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = C69E437D26582D9D004E30A6 /* ViewCodeGuideUITests.swift */; };
C69E43AD26583B52004E30A6 /* MyView.swift in Sources */ = {isa = PBXBuildFile; fileRef = C69E43AC26583B52004E30A6 /* MyView.swift */; };
C69E4406265A9C66004E30A6 /* TitleSubtitleView.swift in Sources */ = {isa = PBXBuildFile; fileRef = C69E4405265A9C66004E30A6 /* TitleSubtitleView.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -49,6 +50,7 @@
C69E437D26582D9D004E30A6 /* ViewCodeGuideUITests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewCodeGuideUITests.swift; sourceTree = "<group>"; };
C69E437F26582D9D004E30A6 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
C69E43AC26583B52004E30A6 /* MyView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MyView.swift; sourceTree = "<group>"; };
C69E4405265A9C66004E30A6 /* TitleSubtitleView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TitleSubtitleView.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -139,6 +141,7 @@
children = (
C69E435F26582D94004E30A6 /* ViewController.swift */,
C69E43AC26583B52004E30A6 /* MyView.swift */,
C69E4405265A9C66004E30A6 /* TitleSubtitleView.swift */,
);
path = Source;
sourceTree = "<group>";
Expand Down Expand Up @@ -284,6 +287,7 @@
C69E43AD26583B52004E30A6 /* MyView.swift in Sources */,
C69E436026582D94004E30A6 /* ViewController.swift in Sources */,
C69E435C26582D94004E30A6 /* AppDelegate.swift in Sources */,
C69E4406265A9C66004E30A6 /* TitleSubtitleView.swift in Sources */,
C69E435E26582D94004E30A6 /* SceneDelegate.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
32 changes: 8 additions & 24 deletions ViewCodeGuide/ViewCodeGuide/Source/MyView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,36 +22,20 @@ final class MyView: UIView {
fatalError("init(coder:) has not been implemented")
}

lazy var titleLabel: UILabel = {
let titleLabel = UILabel()
titleLabel.text = "My Title"
titleLabel.backgroundColor = .gray
titleLabel.translatesAutoresizingMaskIntoConstraints = false
lazy var titleSubtitleView: TitleSubtitleView = {
let view = TitleSubtitleView(title: "MyTitle", subtitle: "MySubtitle")
view.translatesAutoresizingMaskIntoConstraints = false

return titleLabel
}()

lazy var subtitleLabel: UILabel = {
let subtitleLabel = UILabel()
subtitleLabel.text = "My Subtitle"
subtitleLabel.backgroundColor = .gray
subtitleLabel.translatesAutoresizingMaskIntoConstraints = false

return subtitleLabel
return view
}()

func setupHierarchy() {
addSubview(titleLabel)
addSubview(subtitleLabel)
addSubview(titleSubtitleView)
}

func setupConstraints() {
titleLabel.topAnchor.constraint(equalTo: safeAreaLayoutGuide.topAnchor, constant: 24).isActive = true
titleLabel.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 16).isActive = true
titleLabel.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -16).isActive = true

subtitleLabel.topAnchor.constraint(equalTo: titleLabel.bottomAnchor, constant: 16).isActive = true
subtitleLabel.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 16).isActive = true
subtitleLabel.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -16).isActive = true
titleSubtitleView.topAnchor.constraint(equalTo: safeAreaLayoutGuide.topAnchor, constant: 24).isActive = true
titleSubtitleView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 16).isActive = true
titleSubtitleView.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -16).isActive = true
}
}
63 changes: 63 additions & 0 deletions ViewCodeGuide/ViewCodeGuide/Source/TitleSubtitleView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//
// TitleSubtitleView.swift
// ViewCodeGuide
//
// Created by Emerson Mendes Filho on 23/05/21.
//

import UIKit

final class TitleSubtitleView: UIView {

private let title: String
private let subtitle: String

init(title: String,
subtitle: String) {
self.title = title
self.subtitle = subtitle

super.init(frame: .zero)

setupHierarchy()
setupConstraints()
}

@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

private lazy var titleLabel: UILabel = {
let titleLabel = UILabel()
titleLabel.text = title
titleLabel.backgroundColor = .gray
titleLabel.translatesAutoresizingMaskIntoConstraints = false

return titleLabel
}()

private lazy var subtitleLabel: UILabel = {
let subtitleLabel = UILabel()
subtitleLabel.text = subtitle
subtitleLabel.backgroundColor = .gray
subtitleLabel.translatesAutoresizingMaskIntoConstraints = false

return subtitleLabel
}()

func setupHierarchy() {
addSubview(titleLabel)
addSubview(subtitleLabel)
}

func setupConstraints() {
titleLabel.topAnchor.constraint(equalTo: topAnchor).isActive = true
titleLabel.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
titleLabel.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true

subtitleLabel.topAnchor.constraint(equalTo: titleLabel.bottomAnchor, constant: 16).isActive = true
subtitleLabel.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
subtitleLabel.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
}
}

0 comments on commit 97cac76

Please sign in to comment.