Skip to content

Commit

Permalink
Bugfix FXIOS-7985 [v122] Display 3 lines of title for ads (#17819)
Browse files Browse the repository at this point in the history
* Restrict lines of title for LinkButton

* Update colors like in other button

* Add comments

* Restrict number of lines for ad link

* Add FakespotAdLinkButton

* Use new button

* Remove new code

* Simplify code

(cherry picked from commit aba3d15)
  • Loading branch information
thatswinnie authored and mergify[bot] committed Dec 19, 2023
1 parent 9e6548f commit 4af4dc4
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 29 deletions.
50 changes: 22 additions & 28 deletions BrowserKit/Sources/ComponentLibrary/Buttons/LinkButton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,23 @@
import Common
import UIKit

public class LinkButton: UIButton, ThemeApplicable {
var foregroundColorForState: ((UIControl.State) -> UIColor)?
open class LinkButton: UIButton, ThemeApplicable {
private var foregroundColorNormal: UIColor = .clear
private var foregroundColorHighlighted: UIColor = .clear
private var backgroundColorNormal: UIColor = .clear

override init(frame: CGRect) {
super.init(frame: frame)

configuration = UIButton.Configuration.plain()
backgroundColor = .clear
titleLabel?.adjustsFontForContentSizeCategory = true
titleLabel?.numberOfLines = 0
titleLabel?.lineBreakMode = .byWordWrapping
}

public func configure(viewModel: LinkButtonViewModel) {
open func configure(viewModel: LinkButtonViewModel) {
guard let config = configuration else {
return
}
var updatedConfiguration = config

accessibilityIdentifier = viewModel.a11yIdentifier

updatedConfiguration.title = viewModel.title
updatedConfiguration.titleTextAttributesTransformer = UIConfigurationTextAttributesTransformer { incoming in
var outgoing = incoming
Expand All @@ -34,42 +30,40 @@ public class LinkButton: UIButton, ThemeApplicable {
return outgoing
}
updatedConfiguration.contentInsets = viewModel.contentInsets
configuration = updatedConfiguration

accessibilityIdentifier = viewModel.a11yIdentifier
contentHorizontalAlignment = viewModel.contentHorizontalAlignment

configuration = updatedConfiguration
layoutIfNeeded()
}

required init?(coder aDecoder: NSCoder) {
public required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

override public func updateConfiguration() {
guard let config = configuration else {
guard var updatedConfiguration = configuration else {
return
}
var updatedConfiguration = config
let foregroundColor = foregroundColorForState?(state)

updatedConfiguration.baseForegroundColor = foregroundColor
switch state {
case [.highlighted]:
updatedConfiguration.baseForegroundColor = foregroundColorHighlighted
default:
updatedConfiguration.baseForegroundColor = foregroundColorNormal
}

updatedConfiguration.background.backgroundColor = backgroundColorNormal
configuration = updatedConfiguration
}

// MARK: ThemeApplicable

public func applyTheme(theme: Theme) {
var updatedConfiguration = configuration

foregroundColorForState = { state in
switch state {
case [.highlighted]:
return theme.colors.actionPrimaryHover
default:
return theme.colors.textAccent
}
}

updatedConfiguration?.baseForegroundColor = foregroundColorForState?(state)
configuration = updatedConfiguration
foregroundColorNormal = theme.colors.textAccent
foregroundColorHighlighted = theme.colors.actionPrimaryHover
backgroundColorNormal = .clear
setNeedsUpdateConfiguration()
}
}
4 changes: 4 additions & 0 deletions Client.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1333,6 +1333,7 @@
E12BD0AE28AC38480029AAF0 /* UIImage+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = E12BD0AD28AC38480029AAF0 /* UIImage+Extension.swift */; };
E12BD0B028AC3A7E0029AAF0 /* UIEdgeInsets+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = E12BD0AF28AC3A7E0029AAF0 /* UIEdgeInsets+Extension.swift */; };
E1312FD129D237EE008DDA85 /* NotificationSurfaceManagerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E1312FD029D237EE008DDA85 /* NotificationSurfaceManagerTests.swift */; };
E134D5802B31FF3100C6B17B /* FakespotAdLinkButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = E134D57F2B31FF3000C6B17B /* FakespotAdLinkButton.swift */; };
E136D41A2B19D35D003D0302 /* EmbeddedNavController.swift in Sources */ = {isa = PBXBuildFile; fileRef = E136D4192B19D35D003D0302 /* EmbeddedNavController.swift */; };
E1380B8D2AEA897C00630AFA /* SidebarEnabledView.swift in Sources */ = {isa = PBXBuildFile; fileRef = E1380B8C2AEA897C00630AFA /* SidebarEnabledView.swift */; };
E1390FB628B040E900C9EF3E /* WallpaperSelectorViewModelTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E1390FB528B040E900C9EF3E /* WallpaperSelectorViewModelTests.swift */; };
Expand Down Expand Up @@ -7003,6 +7004,7 @@
E12BD0AD28AC38480029AAF0 /* UIImage+Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIImage+Extension.swift"; sourceTree = "<group>"; };
E12BD0AF28AC3A7E0029AAF0 /* UIEdgeInsets+Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIEdgeInsets+Extension.swift"; sourceTree = "<group>"; };
E1312FD029D237EE008DDA85 /* NotificationSurfaceManagerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NotificationSurfaceManagerTests.swift; sourceTree = "<group>"; };
E134D57F2B31FF3000C6B17B /* FakespotAdLinkButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FakespotAdLinkButton.swift; sourceTree = "<group>"; };
E136D4192B19D35D003D0302 /* EmbeddedNavController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EmbeddedNavController.swift; sourceTree = "<group>"; };
E1380B8C2AEA897C00630AFA /* SidebarEnabledView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SidebarEnabledView.swift; sourceTree = "<group>"; };
E1390FB528B040E900C9EF3E /* WallpaperSelectorViewModelTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WallpaperSelectorViewModelTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -10623,6 +10625,7 @@
AB0303292AB47AF300DCD8EF /* FakespotOptInCardView.swift */,
DF529EA02AB1B421003C5373 /* FakespotReliabilityScoreView.swift */,
AB6FEA1F2AEA5CA200E7B2F2 /* FakespotAdView.swift */,
E134D57F2B31FF3000C6B17B /* FakespotAdLinkButton.swift */,
);
path = Views;
sourceTree = "<group>";
Expand Down Expand Up @@ -13712,6 +13715,7 @@
D863C8F21F68BFC20058D95F /* GradientProgressBar.swift in Sources */,
C8445A14264428DC00B83F53 /* LibraryPanelViewState.swift in Sources */,
D5D052D92645ABF400759F85 /* ExperimentsSettingsView.swift in Sources */,
E134D5802B31FF3100C6B17B /* FakespotAdLinkButton.swift in Sources */,
8AE0BF4F2819B10E00F33EC4 /* TopSitesSettingsViewController.swift in Sources */,
8A8DDEBF276259A900E7B97A /* RatingPromptManager.swift in Sources */,
1D969C702B21322B004255B1 /* BrowserWindow.swift in Sources */,
Expand Down
64 changes: 64 additions & 0 deletions Client/Frontend/Fakespot/Views/FakespotAdLinkButton.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/

import UIKit
import ComponentLibrary

class FakespotAdLinkButton: LinkButton {
private struct UX {
static let numberOfLines: Int = 3
}

private var previousFrame: CGRect = .zero

override public var frame: CGRect {
didSet {
guard previousFrame != frame else { return }

previousFrame = frame
invalidateIntrinsicContentSize()
}
}

override public func configure(viewModel: LinkButtonViewModel) {
super.configure(viewModel: viewModel)

guard let config = configuration else {
return
}

var updatedConfiguration = config
updatedConfiguration.titleLineBreakMode = .byTruncatingTail

configuration = updatedConfiguration
layoutIfNeeded()
}

override public func layoutSubviews() {
super.layoutSubviews()

guard let titleLabel else { return }

// hack to be able to restrict the number of lines displayed
titleLabel.numberOfLines = UX.numberOfLines
sizeToFit()
}

override public var intrinsicContentSize: CGSize {
guard let title = titleLabel,
let configuration
else {
return super.intrinsicContentSize
}

let widthContentInset = configuration.contentInsets.leading + configuration.contentInsets.trailing
let heightContentInset = configuration.contentInsets.top + configuration.contentInsets.bottom

let availableWidth = frame.width - widthContentInset
let size = title.sizeThatFits(CGSize(width: availableWidth, height: .greatestFiniteMagnitude))

return CGSize(width: size.width + widthContentInset,
height: size.height + heightContentInset)
}
}
2 changes: 1 addition & 1 deletion Client/Frontend/Fakespot/Views/FakespotAdView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ class FakespotAdView: UIView, Notifiable, ThemeApplicable, UITextViewDelegate {
weight: .semibold)
label.numberOfLines = 0
}
private lazy var productLinkButton: LinkButton = .build { button in
private lazy var productLinkButton: FakespotAdLinkButton = .build { button in
button.addTarget(self, action: #selector(self.didTapProductLink), for: .touchUpInside)
}

Expand Down

0 comments on commit 4af4dc4

Please sign in to comment.