Skip to content

Commit 3f84fc9

Browse files
committed
Added LinkPreviewSizing
1 parent 0bacd21 commit 3f84fc9

File tree

6 files changed

+68
-16
lines changed

6 files changed

+68
-16
lines changed

Example/Pods/Pods.xcodeproj/project.pbxproj

+12-9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Example/SMLinkPreview/APIKeys.plist

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
../../../../SyncServerII/Private/SMLinkPreview/APIKeys.plist
1+
/Users/chris/Developer/Private/SMLinkPreview/APIKeys.plist

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ To run the example project, clone the repo, and run `pod install` from the Examp
2828

2929
## How to use
3030

31-
0) Set up plugs that will do the fetching. You (only) have to have at least one of these:
31+
0) Set up plugin's that will do the fetching. You (only) have to have at least one of these:
3232
```
3333
PreviewManager.session.reset()
3434

Sources/SMLinkPreview/LinkPreview.swift

+35-5
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,29 @@
55
// Created by Christopher G Prince on 4/22/19.
66
//
77

8+
// By default: This resizes its height to what it needs to have to present its `LinkData`. It keeps the width you set.
9+
// If you don't want this behavior, use the `LinkPreviewSizing` parameter below.
10+
811
import UIKit
912

1013
public class LinkPreview: UIView {
1114
@IBOutlet weak var topLevelView: UIView!
1215
@IBOutlet weak var contentView: UIView!
16+
1317
@IBOutlet weak var imageHeight: NSLayoutConstraint!
1418
@IBOutlet public weak var image: UIImageView!
19+
1520
@IBOutlet weak var title: UILabel!
1621
@IBOutlet weak var url: UILabel!
1722
@IBOutlet weak var icon: UIImageView!
1823
@IBOutlet weak var iconContainerWidth: NSLayoutConstraint!
24+
1925
@IBOutlet weak var textAndIconContainer: UIView!
26+
2027
public var textAndIconAction:(()->())?
2128
private var haveImage: Bool = false
2229
private var doneSetup = false
30+
var sizing:LinkPreviewSizing?
2331

2432
public enum LoadedImage {
2533
case large(UIImage)
@@ -80,9 +88,9 @@ public class LinkPreview: UIView {
8088
}
8189
}
8290

83-
public static func create(with linkData: LinkData, callback:((_ image: LoadedImage?)->())? = nil) -> LinkPreview {
91+
public static func create(with linkData: LinkData, sizing:LinkPreviewSizing? = nil, callback:((_ image: LoadedImage?)->())? = nil) -> LinkPreview {
8492
let preview = LinkPreview()
85-
preview.setup(with: linkData, callback: callback)
93+
preview.setup(with: linkData, sizing: sizing, callback: callback)
8694
return preview
8795
}
8896

@@ -93,8 +101,9 @@ public class LinkPreview: UIView {
93101

94102
/// The image is passed back in the form of a callback to allow for asynchronous image loading if needed.
95103
// Image data are loaded from the linkData icon/image URL's, if non-nil. Those URL's can refer to either local or remote files.
96-
public func setup(with linkData: LinkData, callback:((_ image: LoadedImage?)->())? = nil) {
97-
title.numberOfLines = Int(PreviewManager.session.config.maxNumberTitleLines)
104+
public func setup(with linkData: LinkData, sizing:LinkPreviewSizing? = nil, callback:((_ image: LoadedImage?)->())? = nil) {
105+
self.sizing = sizing
106+
98107
title.text = linkData.title
99108
url.text = linkData.url.urlWithoutScheme()
100109

@@ -105,6 +114,14 @@ public class LinkPreview: UIView {
105114

106115
var result:LoadedImage?
107116

117+
let heightIsResizable = sizing?.resizingAllowed ?? false
118+
119+
title.numberOfLines = Int(PreviewManager.session.config.maxNumberTitleLines)
120+
121+
if let sizingNumberTitleLines = sizing?.titleLabelNumberOfLines {
122+
title.numberOfLines = sizingNumberTitleLines
123+
}
124+
108125
if let imageURL = linkData.image,
109126
let data = try? Data(contentsOf: imageURL.attemptForceScheme(forceScheme)) {
110127
haveImage = true
@@ -113,9 +130,19 @@ public class LinkPreview: UIView {
113130
result = .large(image)
114131
}
115132
applyCornerRounding(view: contentView)
133+
134+
// Not showing the icon-- because we have the large image
116135
iconContainerWidth.constant = 0
136+
117137
layoutIfNeeded()
118-
frame.size.height = textAndIconContainer.frame.height + image.frame.height
138+
139+
if heightIsResizable {
140+
frame.size.height = textAndIconContainer.frame.height + image.frame.height
141+
}
142+
else {
143+
// We can't change the height of the `LinkPreview`. Change the imageHeight to the most it can have.
144+
imageHeight.constant = max(frame.size.height - textAndIconContainer.frame.height, 0)
145+
}
119146
}
120147
else {
121148
haveImage = false
@@ -132,10 +159,13 @@ public class LinkPreview: UIView {
132159
}
133160
}
134161
else {
162+
// No image, no icon. Hide the icon.
135163
iconContainerWidth.constant = 0
136164
}
137165

166+
// No image.
138167
imageHeight.constant = 0
168+
139169
layoutIfNeeded()
140170
frame.size.height = textAndIconContainer.frame.height
141171
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
import Foundation
3+
4+
public struct LinkPreviewSizing {
5+
// Can the height of the `LinkPreview` be adapted to fit the `LinkData`?
6+
public var resizingAllowed: Bool = true
7+
8+
// The value for `title.numberOfLines`-- 0 means no limit. If you set `resizingAllowed` - false, it's best to set this to 1 or 2.
9+
// This overrides, if LinkPreviewSizing is non-nil, the `maxNumberTitleLines` value given in `PreviewConfiguration`.
10+
public var titleLabelNumberOfLines:Int = 0
11+
12+
public init(resizingAllowed: Bool = true, titleLabelNumberOfLines:Int = 0) {
13+
self.resizingAllowed = resizingAllowed
14+
self.titleLabelNumberOfLines = titleLabelNumberOfLines
15+
}
16+
}

VERSIONS.txt

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
SMLinkPreview
22

3+
Version 0.3.0 (12/28/20)
4+
* Made resizing capabilities of LinkPreview more general-- see LinkPreviewSizing.
5+
36
Version 0.2.0 (12/28/20)
47
* Enable Swift Package Manager (as well as Cocoapods)
58

0 commit comments

Comments
 (0)