From d931f4e4ec368dd7c20803a8e12fc9920d97f87a Mon Sep 17 00:00:00 2001 From: suzuki_keishi Date: Fri, 5 Aug 2016 11:50:03 +0900 Subject: [PATCH] [update]clean caption View. --- SKPhotoBrowser/SKCaptionView.swift | 67 +++++++++++++++++------------- 1 file changed, 37 insertions(+), 30 deletions(-) diff --git a/SKPhotoBrowser/SKCaptionView.swift b/SKPhotoBrowser/SKCaptionView.swift index 8510cd8..fc4ebba 100644 --- a/SKPhotoBrowser/SKCaptionView.swift +++ b/SKPhotoBrowser/SKCaptionView.swift @@ -33,36 +33,6 @@ public class SKCaptionView: UIView { setup() } - func setup() { - opaque = false - autoresizingMask = [.FlexibleWidth, .FlexibleTopMargin, .FlexibleRightMargin, .FlexibleLeftMargin] - - // setup background first - fadeView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight] - addSubview(fadeView) - - // add layer at fadeView - gradientLayer.colors = [UIColor(white: 0.0, alpha: 0.0).CGColor, UIColor(white: 0.0, alpha: 0.8).CGColor] - fadeView.layer.insertSublayer(gradientLayer, atIndex: 0) - - photoLabel = UILabel(frame: CGRect(x: photoLabelPadding, y: 0, - width: bounds.size.width - (photoLabelPadding * 2), height: bounds.size.height)) - photoLabel.autoresizingMask = [.FlexibleWidth, .FlexibleHeight] - photoLabel.opaque = false - photoLabel.backgroundColor = .clearColor() - photoLabel.textColor = .whiteColor() - photoLabel.textAlignment = .Center - photoLabel.lineBreakMode = .ByTruncatingTail - photoLabel.numberOfLines = 3 - photoLabel.shadowColor = UIColor(white: 0.0, alpha: 0.5) - photoLabel.shadowOffset = CGSize(width: 0.0, height: 1.0) - photoLabel.font = UIFont.systemFontOfSize(17.0) - if let cap = photo.caption { - photoLabel.text = cap - } - addSubview(photoLabel) - } - public override func sizeThatFits(size: CGSize) -> CGSize { guard let text = photoLabel.text else { return CGSize.zero @@ -88,3 +58,40 @@ public class SKCaptionView: UIView { } } +private extension SKCaptionView { + func setup() { + opaque = false + autoresizingMask = [.FlexibleWidth, .FlexibleTopMargin, .FlexibleRightMargin, .FlexibleLeftMargin] + + // setup background first + setupFadeView() + + // setup photoLabel + setupPhotoLabel() + } + + func setupFadeView() { + fadeView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight] + addSubview(fadeView) + + gradientLayer.colors = [UIColor(white: 0, alpha: 0).CGColor, UIColor(white: 0, alpha: 0.8).CGColor] + fadeView.layer.insertSublayer(gradientLayer, atIndex: 0) + } + + func setupPhotoLabel() { + photoLabel = UILabel(frame: CGRect(x: photoLabelPadding, y: 0, width: bounds.size.width - (photoLabelPadding * 2), height: bounds.size.height)) + photoLabel.autoresizingMask = [.FlexibleWidth, .FlexibleHeight] + photoLabel.opaque = false + photoLabel.backgroundColor = .clearColor() + photoLabel.textColor = .whiteColor() + photoLabel.textAlignment = .Center + photoLabel.lineBreakMode = .ByTruncatingTail + photoLabel.numberOfLines = 3 + photoLabel.shadowColor = UIColor(white: 0.0, alpha: 0.5) + photoLabel.shadowOffset = CGSize(width: 0.0, height: 1.0) + photoLabel.font = UIFont.systemFontOfSize(17.0) + photoLabel.text = photo.caption + addSubview(photoLabel) + } +} +