|
| 1 | +<h1 align="center" style="color: #376C9D; font-family: Arial Black, Gadget, sans-serif; font-size: 1.5em">PinLayout Benchmark Source code</h1> |
| 2 | + |
| 3 | +PinLayout's layout code is concise, clean and doesn't contain any computation compared to Manual Layouting source code. |
| 4 | + |
| 5 | + |
| 6 | +#### Manual layouting benchmark source code |
| 7 | + |
| 8 | + |
| 9 | +``` |
| 10 | +override func layoutSubviews() { |
| 11 | + super.layoutSubviews() |
| 12 | + |
| 13 | + optionsLabel.frame = CGRect(x: bounds.width-optionsLabel.frame.width, y: 0, |
| 14 | + width: optionsLabel.frame.width, height: optionsLabel.frame.height) |
| 15 | + actionLabel.frame = CGRect(x: 0, y: 0, width: bounds.width-optionsLabel.frame.width, height: 0) |
| 16 | + actionLabel.sizeToFit() |
| 17 | +
|
| 18 | + posterImageView.frame = CGRect(x: 0, y: actionLabel.frame.bottom, |
| 19 | + width: posterImageView.frame.width, height: 0) |
| 20 | + posterImageView.sizeToFit() |
| 21 | +
|
| 22 | + let contentInsets = UIEdgeInsets(top: 0, left: 1, bottom: 2, right: 3) |
| 23 | + let posterLabelWidth = bounds.width-posterImageView.frame.width - contentInsets.left - |
| 24 | + contentInsets.right |
| 25 | + posterNameLabel.frame = CGRect(x: posterImageView.frame.right + contentInsets.left, |
| 26 | + y: posterImageView.frame.origin.y + contentInsets.top, |
| 27 | + width: posterLabelWidth, height: 0) |
| 28 | + posterNameLabel.sizeToFit() |
| 29 | +
|
| 30 | + let spacing: CGFloat = 1 |
| 31 | + posterHeadlineLabel.frame = CGRect(x: posterImageView.frame.right + contentInsets.left, |
| 32 | + y: posterNameLabel.frame.bottom + spacing, |
| 33 | + width: posterLabelWidth, height: 0) |
| 34 | + posterHeadlineLabel.sizeToFit() |
| 35 | +
|
| 36 | + posterTimeLabel.frame = CGRect(x: posterImageView.frame.right + contentInsets.left, |
| 37 | + y: posterHeadlineLabel.frame.bottom + spacing, width: posterLabelWidth, |
| 38 | + height: 0) |
| 39 | + posterTimeLabel.sizeToFit() |
| 40 | +
|
| 41 | + posterCommentLabel.frame = CGRect(x: 0, y: max(posterImageView.frame.bottom, |
| 42 | + posterTimeLabel.frame.bottom + |
| 43 | + contentInsets.bottom), |
| 44 | + width: frame.width, height: 0) |
| 45 | + posterCommentLabel.sizeToFit() |
| 46 | +
|
| 47 | + contentImageView.frame = CGRect(x: frame.width/2 - contentImageView.frame.width/2, |
| 48 | + y: posterCommentLabel.frame.bottom, width: frame.width, height: 0) |
| 49 | + contentImageView.sizeToFit() |
| 50 | +
|
| 51 | + contentTitleLabel.frame = CGRect(x: 0, y: contentImageView.frame.bottom, width: frame.width, height: 0) |
| 52 | + contentTitleLabel.sizeToFit() |
| 53 | +
|
| 54 | + contentDomainLabel.frame = CGRect(x: 0, y: contentTitleLabel.frame.bottom, width: frame.width, height: 0) |
| 55 | + contentDomainLabel.sizeToFit() |
| 56 | +
|
| 57 | + likeLabel.frame = CGRect(x: 0, y: contentDomainLabel.frame.bottom, width: 0, height: 0) |
| 58 | + likeLabel.sizeToFit() |
| 59 | +
|
| 60 | + commentLabel.sizeToFit() |
| 61 | + commentLabel.frame = CGRect(x: frame.width/2-commentLabel.frame.width/2, |
| 62 | + y: contentDomainLabel.frame.bottom, |
| 63 | + width: commentLabel.frame.width, height: commentLabel.frame.height) |
| 64 | +
|
| 65 | + shareLabel.sizeToFit() |
| 66 | + shareLabel.frame = CGRect(x: frame.width-shareLabel.frame.width, y: contentDomainLabel.frame.bottom, |
| 67 | + width: shareLabel.frame.width, height: shareLabel.frame.height) |
| 68 | +
|
| 69 | + actorImageView.frame = CGRect(x: 0, y: likeLabel.frame.bottom, width: 0, height: 0) |
| 70 | + actorImageView.sizeToFit() |
| 71 | +
|
| 72 | + actorCommentLabel.frame = CGRect(x: actorImageView.frame.right, y: likeLabel.frame.bottom, |
| 73 | + width: frame.width-actorImageView.frame.width, height: 0) |
| 74 | + actorCommentLabel.sizeToFit() |
| 75 | +} |
| 76 | +``` |
| 77 | + |
| 78 | +### PinLayout benchmark source code |
| 79 | + |
| 80 | +``` |
| 81 | +override func layoutSubviews() { |
| 82 | + super.layoutSubviews() |
| 83 | + |
| 84 | + let hMargin: CGFloat = 8 |
| 85 | + let vMargin: CGFloat = 2 |
| 86 | + |
| 87 | + optionsLabel.pin.topRight().margin(hMargin) |
| 88 | + actionLabel.pin.topLeft().margin(hMargin) |
| 89 | + |
| 90 | + posterImageView.pin.below(of: actionLabel, aligned: .left).marginTop(10) |
| 91 | + posterNameLabel.pin.right(of: posterImageView, aligned: .top).margin(-6, 6).right(hMargin).sizeToFit() |
| 92 | + posterHeadlineLabel.pin.below(of: posterNameLabel, aligned: .left).right(hMargin).marginTop(1).sizeToFit() |
| 93 | + posterTimeLabel.pin.below(of: posterHeadlineLabel, aligned: .left).right(hMargin).marginTop(1).sizeToFit() |
| 94 | + |
| 95 | + posterCommentLabel.pin.below(of: posterTimeLabel).left(hMargin).right().right(hMargin) |
| 96 | + .marginTop(vMargin).sizeToFit() |
| 97 | + |
| 98 | + contentImageView.pin.below(of: posterCommentLabel).hCenter().width(100%).sizeToFit() |
| 99 | + contentTitleLabel.pin.below(of: contentImageView).left().right().marginHorizontal(hMargin).sizeToFit() |
| 100 | + contentDomainLabel.pin.below(of: contentTitleLabel, aligned: .left).right().marginRight(hMargin) |
| 101 | + .sizeToFit() |
| 102 | + |
| 103 | + likeLabel.pin.below(of: contentDomainLabel, aligned: .left).marginTop(vMargin) |
| 104 | + commentLabel.pin.top(to: likeLabel.edge.top).hCenter(50%) |
| 105 | + shareLabel.pin.top(to: likeLabel.edge.top).right().marginRight(hMargin) |
| 106 | + |
| 107 | + actorImageView.pin.below(of: likeLabel, aligned: .left).marginTop(vMargin) |
| 108 | + actorCommentLabel.pin.right(of: actorImageView, aligned: .center).marginLeft(4) |
| 109 | +} |
| 110 | +``` |
0 commit comments