Update layout for text only previews.

This commit is contained in:
Doug
2021-09-02 12:41:55 +01:00
parent 0491195401
commit 3db5d7f605
2 changed files with 38 additions and 26 deletions
@@ -51,6 +51,15 @@ class URLPreviewView: UIView, NibLoadable, Themable {
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var descriptionLabel: UILabel!
// Matches the label's height with the close button.
// Use a strong reference to keep it around when deactivating.
@IBOutlet var siteNameLabelHeightConstraint: NSLayoutConstraint!
private var hasTitle: Bool {
guard let title = titleLabel.text else { return false }
return !title.isEmpty
}
// MARK: - Setup
static func instantiate() -> Self {
@@ -118,29 +127,28 @@ class URLPreviewView: UIView, NibLoadable, Themable {
}
private func renderLoaded(_ preview: URLPreviewData) {
if let image = preview.image {
imageView.image = image
showImageContainer()
} else {
imageView.image = nil
hideImageContainer()
}
imageView.image = preview.image
siteNameLabel.text = preview.siteName ?? preview.url.host
titleLabel.text = preview.title
descriptionLabel.text = preview.text
updateLayout()
}
private func showImageContainer() {
imageView.isHidden = false
// TODO: Adjust spacing of site name label
}
private func hideImageContainer() {
imageView.isHidden = true
// TODO: Adjust spacing of site name label
private func updateLayout() {
if imageView.image == nil {
imageView.isHidden = true
// tweak the layout of labels
siteNameLabelHeightConstraint.isActive = true
descriptionLabel.numberOfLines = hasTitle ? 3 : 5
} else {
imageView.isHidden = false
// tweak the layout of labels
siteNameLabelHeightConstraint.isActive = false
descriptionLabel.numberOfLines = 2
}
}
// MARK: - Action
@@ -10,11 +10,11 @@
<objects>
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner"/>
<view contentMode="scaleToFill" id="dCz-KI-m5q" customClass="URLPreviewView" customModule="Riot" customModuleProvider="target">
<rect key="frame" x="0.0" y="0.0" width="267" height="230"/>
<rect key="frame" x="0.0" y="0.0" width="267" height="222"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<subviews>
<stackView opaque="NO" contentMode="scaleToFill" axis="vertical" translatesAutoresizingMaskIntoConstraints="NO" id="Rqc-iY-nm0">
<rect key="frame" x="0.0" y="0.0" width="267" height="230"/>
<rect key="frame" x="0.0" y="0.0" width="267" height="222"/>
<subviews>
<imageView clipsSubviews="YES" userInteractionEnabled="NO" contentMode="scaleAspectFit" horizontalHuggingPriority="251" verticalHuggingPriority="251" translatesAutoresizingMaskIntoConstraints="NO" id="4zc-0W-jb8">
<rect key="frame" x="0.0" y="0.0" width="267" height="140"/>
@@ -23,25 +23,28 @@
</constraints>
</imageView>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="n9x-Yn-0qQ">
<rect key="frame" x="0.0" y="140" width="267" height="90"/>
<rect key="frame" x="0.0" y="140" width="267" height="82"/>
<subviews>
<stackView opaque="NO" contentMode="scaleToFill" axis="vertical" alignment="top" spacing="2" translatesAutoresizingMaskIntoConstraints="NO" id="3Wa-hg-AAN">
<rect key="frame" x="8" y="8" width="251" height="74"/>
<rect key="frame" x="8" y="8" width="251" height="66"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Site Name" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="ay6-fT-tTb">
<rect key="frame" x="0.0" y="0.0" width="56" height="21.5"/>
<rect key="frame" x="0.0" y="0.0" width="56" height="25"/>
<constraints>
<constraint firstAttribute="height" constant="25" id="vhD-hz-f58"/>
</constraints>
<fontDescription key="fontDescription" type="system" weight="semibold" pointSize="11"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="252" text="Title" textAlignment="natural" lineBreakMode="tailTruncation" numberOfLines="2" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="IVX-5S-0kr">
<rect key="frame" x="0.0" y="23.5" width="33.5" height="19.5"/>
<rect key="frame" x="0.0" y="27" width="33.5" height="19.5"/>
<fontDescription key="fontDescription" type="system" weight="semibold" pointSize="16"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="252" verticalCompressionResistancePriority="250" textAlignment="natural" lineBreakMode="tailTruncation" numberOfLines="2" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="Z30-YF-eQk">
<rect key="frame" x="0.0" y="45" width="250.5" height="29"/>
<rect key="frame" x="0.0" y="48.5" width="250.5" height="17.5"/>
<string key="text">Description with two lines of text for testing purposes, going bigger so we can see what happens. That's not enought text, more text keep gooing!</string>
<fontDescription key="fontDescription" type="system" pointSize="12"/>
<nil key="textColor"/>
@@ -90,10 +93,11 @@
<outlet property="descriptionLabel" destination="Z30-YF-eQk" id="DJ4-Bg-MHW"/>
<outlet property="imageView" destination="4zc-0W-jb8" id="QRh-IX-XxR"/>
<outlet property="siteNameLabel" destination="ay6-fT-tTb" id="2wA-1z-lcs"/>
<outlet property="siteNameLabelHeightConstraint" destination="vhD-hz-f58" id="Bz9-ub-9UA"/>
<outlet property="titleLabel" destination="IVX-5S-0kr" id="PRN-5g-HiO"/>
<outletCollection property="gestureRecognizers" destination="rSB-1V-Kev" appends="YES" id="OOJ-ft-VIj"/>
</connections>
<point key="canvasLocation" x="1860.144927536232" y="1.3392857142857142"/>
<point key="canvasLocation" x="1860.144927536232" y="-1.3392857142857142"/>
</view>
<placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
<tapGestureRecognizer id="rSB-1V-Kev">