Coder Social home page Coder Social logo

drapierlayout's Introduction

DrapierLayout

Helps make layoutSubviews more readable

Examples

A simple view with a UIImageView and two UILabels.

class CustomView: UIView {
	var imageView: UIImageView!
	var titleLabel: UILabel!
	var detailLabel: UILabel!
	
	struct ViewLayout {
		let imageFrame: CGRect
		let titleFrame: CGRect
		let detailFrame: CGRect
	}
	
	func generateLayout(bounds: CGRect) -> ViewLayout {
		// Since a size is not specified, sizeThatFits is called on imageView with a 
		// size of 10,000 x 10,000 this allows the image to be as large as it can.
		var imageFrame = imageView.layout(
			Leading(equalTo: bounds.leading(layoutMargins)),
			Top(equalTo: bounds.top(layoutMargins))
		)
		
		let maximumImageWidth = CGFloat(50.0)
		
		if imageFrame.width > maximumImageWidth {
			// This time a size is specified, this attempts to make sure that the image
			// doesn't push the UILabels off the edge of the parent view.
			imageFrame = imageView.layout(
				Leading(equalTo: bounds.leading(layoutMargins)),
				Top(equalTo: bounds.top(layoutMargins)),
				Width(equalTo: maximumImageWidth),
				Height(equalTo: round(maximumImageWidth * imageFrame.inverseAspectRatio))
			)
		}
		
		// The title fits between the image and the trailing edge of the superview.
		// A capline is the line that marks the top of a capital letter for the 
		// UILabel's current font.
		let titleFrame = titleLabel.layout(
			Leading(equalTo: imageFrame.trailing, constant: 8.0),
			Trailing(equalTo: bounds.trailing(layoutMargins)),
			Capline(equalTo: imageFrame.top)
		)
		
		let detailFrame = detailLabel.layout(
			Leading(equalTo: titleFrame.leading),
			Trailing(equalTo: titleFrame.trailing),
			Capline(equalTo: titleFrame.baseline(font: titleLabel.font), constant: 8.0)
		)
		
		return ViewLayout(
			imageFrame: imageFrame,
			titleFrame: titleFrame,
			detailFrame: detailFrame
		)
	}
	
	override func sizeThatFits(size: CGSize) -> CGSize {
		let layout = generateLayout(size.rect())
		// Find the fitting height from the layout frame that is farthest toward the bottom, then
		// add in the layoutMargin bottom if needed to find the true height.
		let maxBottom = max(layout.imageFrame.bottom, layout.detailFrame.baseline(font: detailLabel.font))
		return CGSize(width: size.width, height: maxBottom + layoutMargins.bottom)
	}
	
	override func layoutSubviews() {
		super.layoutSubviews()
		
		// In most cases the layout code can be shared between sizeThatFits and layoutSubviews.
		let layout = generateLayout(bounds)
		
		imageView.frame = layout.imageFrame
		titleLabel.frame = layout.titleFrame
		detailLabel.frame = layout.detailFrame
	}
}

Contact

Justin Kolb
@nabobnick

License

DrapierLayout is available under the MIT license. See the LICENSE file for more info.

drapierlayout's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

ksloginov

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.