From 91247b7f1628f70e4520a07e4a08133bc87387a1 Mon Sep 17 00:00:00 2001 From: Flowinho Date: Thu, 12 Mar 2020 21:23:57 +0100 Subject: [PATCH 1/2] Adding dynamic width algorithm to the titleLabel MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit adds multiple functionalities at once: - The background of the image title now features rounded corners with a radius of 6 - which is consistent to iOS appearace. - The background of the image title now is enlarged to make sure the letters do not touch the borders of the view. (This was a finding during implementation). - The background of the image title is now removed when no title is present (memory optimization / prevention of unwanted optical glitches). The title label now resizes itself depending on the devices it’s displazed on. - On iPhone it will take 92% percent of available screen width. - On iPhone it will 80% of available screen width. - This works for all device orientations. The numbers are derived from my personal preference when implementing it. --- iOS/Article/ImageViewController.swift | 17 ++++++++++++++ iOS/Base.lproj/Main.storyboard | 32 +++++++++++++++------------ iOS/Settings/Settings.storyboard | 4 ++-- 3 files changed, 37 insertions(+), 16 deletions(-) diff --git a/iOS/Article/ImageViewController.swift b/iOS/Article/ImageViewController.swift index acd572a13..2a1b5cce3 100644 --- a/iOS/Article/ImageViewController.swift +++ b/iOS/Article/ImageViewController.swift @@ -13,6 +13,9 @@ class ImageViewController: UIViewController { @IBOutlet weak var shareButton: UIButton! @IBOutlet weak var imageScrollView: ImageScrollView! @IBOutlet weak var titleLabel: UILabel! + @IBOutlet weak var titleBackground: UIVisualEffectView! + @IBOutlet weak var titleLeading: NSLayoutConstraint! + @IBOutlet weak var titleTrailing: NSLayoutConstraint! var image: UIImage! var imageTitle: String? @@ -30,6 +33,13 @@ class ImageViewController: UIViewController { imageScrollView.display(image: image) titleLabel.text = imageTitle ?? "" + layoutTitleLabel() + + guard imageTitle != nil else { + titleBackground.removeFromSuperview() + return + } + titleBackground.layer.cornerRadius = 6 } override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) { @@ -51,6 +61,13 @@ class ImageViewController: UIViewController { dismiss(animated: true) } + private func layoutTitleLabel(){ + let width = view.frame.width + let multiplier = UIDevice.current.userInterfaceIdiom == .pad ? CGFloat(0.1) : CGFloat(0.04) + titleLeading.constant += width * multiplier + titleTrailing.constant -= width * multiplier + titleLabel.layoutIfNeeded() + } } // MARK: ImageScrollViewDelegate diff --git a/iOS/Base.lproj/Main.storyboard b/iOS/Base.lproj/Main.storyboard index 02ad94cf9..7880d0ddd 100644 --- a/iOS/Base.lproj/Main.storyboard +++ b/iOS/Base.lproj/Main.storyboard @@ -1,8 +1,8 @@ - + - + @@ -239,16 +239,16 @@ - - + + - + -