Skip to content

Commit

Permalink
Customize the hint text with paragraph style
Browse files Browse the repository at this point in the history
  • Loading branch information
bcylin committed Jun 14, 2017
1 parent 0561013 commit 1b92249
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 3 deletions.
43 changes: 43 additions & 0 deletions Example/Pickle/CarousellImagePickerController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,45 @@ internal class CarousellImagePickerController: ImagePickerController {
configuration: CarousellTheme(),
camera: UIImagePickerController.init
)

hint = {
let titleStyle = NSMutableParagraphStyle()
titleStyle.maximumLineHeight = 42
titleStyle.minimumLineHeight = 42
titleStyle.paragraphSpacing = 4
titleStyle.firstLineHeadIndent = 12
titleStyle.alignment = .left

let subtitleStyle = NSMutableParagraphStyle()
subtitleStyle.maximumLineHeight = 12
subtitleStyle.minimumLineHeight = 12
subtitleStyle.paragraphSpacing = 10
subtitleStyle.firstLineHeadIndent = 12
subtitleStyle.alignment = .left

let title = NSMutableAttributedString(
string: "What are you listing?\n",
attributes: [
NSFontAttributeName: UIFont.boldSystemFont(ofSize: 22),
NSForegroundColorAttributeName: UIColor.black,
NSBackgroundColorAttributeName: UIColor.white,
NSParagraphStyleAttributeName: titleStyle
]
)

let subtitle = NSAttributedString(
string: "You can choose up to 4 photos for your listing.\n",
attributes: [
NSFontAttributeName: UIFont.systemFont(ofSize: 12),
NSForegroundColorAttributeName: UIColor.darkGray,
NSBackgroundColorAttributeName: UIColor.white,
NSParagraphStyleAttributeName: subtitleStyle
]
)

title.append(subtitle)
return title
}()
}

required init?(coder aDecoder: NSCoder) {
Expand Down Expand Up @@ -56,4 +95,8 @@ private struct CarousellTheme: ImagePickerConfigurable {
var selectedImageOverlayColor: UIColor? = nil
let allowedSelections: ImagePickerSelection? = .limit(to: 4)

// MARK: -

let hintTextMargin: UIEdgeInsets? = .zero

}
5 changes: 5 additions & 0 deletions Pickle/Classes/ImagePickerConfigurable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ public protocol ImagePickerConfigurable {

/// Specifies the number of photo selections is allowed in ImagePickerController.
var allowedSelections: ImagePickerSelection? { get }

// MARK: -

/// The margin for the text of the hint label.
var hintTextMargin: UIEdgeInsets? { get }
}


Expand Down
7 changes: 5 additions & 2 deletions Pickle/Classes/PhotoGalleryHintLabel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ internal final class PhotoGalleryHintLabel: UILabel {
setUpAppearance()
}

// MARK: - Properties

internal var textMargin = UIEdgeInsets(top: 6, left: 0, bottom: 6, right: 0)

// MARK: - UIView

override var bounds: CGRect {
Expand Down Expand Up @@ -54,8 +58,7 @@ internal final class PhotoGalleryHintLabel: UILabel {
// MARK: - Private

private var contentEdgeInsets: UIEdgeInsets {
let padding: CGFloat = (text?.isEmpty ?? true) ? 0 : 6
return UIEdgeInsets(top: padding, left: 0, bottom: padding, right: 0)
return (text?.isEmpty ?? true) ? .zero : textMargin
}

private lazy var borderLayer: CALayer = {
Expand Down
5 changes: 4 additions & 1 deletion Pickle/Classes/PhotoGalleryViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ internal final class PhotoGalleryViewController: UIViewController,
}
set {
hintLabel.attributedText = newValue
if let margin = configuration?.hintTextMargin {
hintLabel.textMargin = margin
}
if let color = newValue?.attributes?[NSBackgroundColorAttributeName] as? UIColor {
hintLabel.backgroundColor = color
}
Expand Down Expand Up @@ -81,7 +84,7 @@ internal final class PhotoGalleryViewController: UIViewController,
return PHAsset.fetchAssets(in: self.album, options: options)
}()

private lazy var hintLabel: UILabel = PhotoGalleryHintLabel()
private lazy var hintLabel: PhotoGalleryHintLabel = PhotoGalleryHintLabel()

internal private(set) lazy var collectionView: UICollectionView = {
let layout = self.photoGalleryLayout(for: UIScreen.main.bounds.size)
Expand Down

0 comments on commit 1b92249

Please sign in to comment.