Component
public protocol Component
A component represents a small reusable piece of code of element to be rendered.
This behaves as all elements of UIKit’s UITableView and UICollectionView, and can
be easily support SwiftUI by used in conjunction with the View protocol.
Example for the simple component:
struct Label: Component {
var text: String
init(_ text: String) {
self.text = text
}
func renderContent() -> UILabel {
return UILabel()
}
func render(in content: UILabel) {
content.text = text
}
}
Example for use with SwiftUI:
extension Label: View {}
struct ContentView: View {
var body: some View {
VStack {
Text("This is SwiftUI view")
Label("This is Carbon component")
}
}
}
-
A type that represents a content to be render on the element of list UI.
Declaration
Swift
associatedtype Content
-
Render properties into the content.
Declaration
Swift
func render(in content: Content)
-
reuseIdentifierDefault implementationA string used to identify a element that is reusable. Default is the type name of
self.Default Implementation
A string used to identify a element that is reusable. Default is the type name of
self.Declaration
Swift
var reuseIdentifier: String { get } -
referenceSize(in:)Default implementationReturns the referencing size of content to render on the list UI.
Note
Only
CGSize.heightis used to determine the size of element inUITableView.Default Implementation
Returns the referencing size of content to render on the list UI. Returns nil by default.
Note
Only
CGSize.heightis used to determine the size of element inUITableView.Declaration
Swift
func referenceSize(in bounds: CGRect) -> CGSize?Return Value
The referencing size of content to render on the list UI. If returns nil, the element of list UI falls back to its default size like
UITableView.rowHeightorUICollectionViewFlowLayout.itemSize. -
shouldContentUpdate(with:)Default implementationReturns a
Boolvalue indicating whether the content should be reloaded.Note
Unlike
Equatable, this doesn’t compare whether the two values exactly equal. It’s can be ignore property comparisons, if not expect to reload content.Default Implementation
Returns a
Boolvalue indicating whether the content should be reloaded. Default is false.Note
Unlike
Equatable, this doesn’t compare whether the two values exactly equal. It’s can be ignore property comparisons, if not expect to reload content.Declaration
Swift
func shouldContentUpdate(with next: Self) -> BoolReturn Value
A
Boolvalue indicating whether the content should be reloaded. -
shouldRender(next:in:)Default implementationReturns a
Boolvalue indicating whether component should be render again.Default Implementation
Returns a
Boolvalue indicating whether component should be render again.Declaration
Swift
func shouldRender(next: Self, in content: Content) -> BoolParameters
nextThe next value to be compared to the receiver.
contentAn instance of content laid out on the element.
Return Value
A
Boolvalue indicating whether the component should be render again. -
layout(content:in:)Default implementationLayout the content on top of element of the list UI.
Note
UIViewandUIViewControllerare laid out with edge constraints by default.Default Implementation
Layout the content on top of element of the list UI.
Declaration
Swift
func layout(content: Content, in container: UIView)Parameters
contentAn instance of content to be laid out on top of element.
containerA container view to layout content.
-
intrinsicContentSize(for:)Default implementationThe natural size for the passed content.
Default Implementation
The natural size for the passed content.
Declaration
Swift
func intrinsicContentSize(for content: Content) -> CGSizeReturn Value
A
CGSizevalue represents a natural size of the passed content.
-
contentWillDisplay(_:)Default implementationInvoked every time of before a component got into visible area.
Default Implementation
Invoked every time of before a component got into visible area.
Declaration
Swift
func contentWillDisplay(_ content: Content) -
contentDidEndDisplay(_:)Default implementationInvoked every time of after a component went out from visible area.
Default Implementation
Invoked every time of after a component went out from visible area.
Declaration
Swift
func contentDidEndDisplay(_ content: Content)
-
identified(by:)Extension methodReturns an identified component wrapping
selfand givenid.Declaration
Swift
@inlinable func identified<ID>(by id: ID) -> IdentifiedComponentWrapper<ID, Self> where ID : HashableReturn Value
An identified component wrapping
selfand givenid. -
identified(by:)Extension methodReturns an identified component wrapping
selfand theidthat accessed by given key path.Declaration
Swift
@inlinable func identified<ID>(by keyPath: KeyPath<Self, ID>) -> IdentifiedComponentWrapper<ID, Self> where ID : HashableReturn Value
An identified component wrapping
selfand theidthat accessed by given key path.
View on GitHub
Component Protocol Reference