Files
NetNewsWire/iOS/Article/ImageViewController.swift
Flowinho 91247b7f16 Adding dynamic width algorithm to the titleLabel
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.
2020-03-12 21:23:57 +01:00

87 lines
2.4 KiB
Swift

//
// ImageViewController.swift
// NetNewsWire-iOS
//
// Created by Maurice Parker on 10/12/19.
// Copyright © 2019 Ranchero Software. All rights reserved.
//
import UIKit
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?
var zoomedFrame: CGRect {
return imageScrollView.zoomedFrame
}
override func viewDidLoad() {
super.viewDidLoad()
imageScrollView.setup()
imageScrollView.imageScrollViewDelegate = self
imageScrollView.imageContentMode = .aspectFit
imageScrollView.initialOffset = .center
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) {
super.viewWillTransition(to: size, with: coordinator)
coordinator.animate(alongsideTransition: { [weak self] context in
self?.imageScrollView.resize()
})
}
@IBAction func share(_ sender: Any) {
guard let image = image else { return }
let activityViewController = UIActivityViewController(activityItems: [image], applicationActivities: nil)
activityViewController.popoverPresentationController?.sourceView = shareButton
activityViewController.popoverPresentationController?.sourceRect = shareButton.bounds
present(activityViewController, animated: true)
}
@IBAction func done(_ sender: Any) {
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
extension ImageViewController: ImageScrollViewDelegate {
func imageScrollViewDidGestureSwipeUp(imageScrollView: ImageScrollView) {
dismiss(animated: true)
}
func imageScrollViewDidGestureSwipeDown(imageScrollView: ImageScrollView) {
dismiss(animated: true)
}
}