Skip to content

Commit

Permalink
Fix brave#1266: Truncate image title when it's too long on long press…
Browse files Browse the repository at this point in the history
… action.
  • Loading branch information
iccub committed Jul 17, 2019
1 parent 4fae8c9 commit 84b86dc
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Client/Frontend/Browser/BrowserViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2523,7 +2523,8 @@ extension BrowserViewController: ContextMenuHelperDelegate {
let imageText = imageTitle.map { "\n\n\($0)" } ?? ""
// If the image is a link, show the link's URL. Otherwise, show the image's source URL.
let urlText = elements.link?.absoluteString ?? url.absoluteString
dialogTitle = "\(urlText)\(imageText)"
// Truncate text above certain amount of characters
dialogTitle = "\(urlText)\(imageText)".truncate(length: 200)

let openInNewTabAction = UIAlertAction(title: Strings.OpenImageInNewTabActionTitle, style: .default) { _ in
let isPrivate = PrivateBrowsingManager.shared.isPrivateBrowsing
Expand Down
11 changes: 11 additions & 0 deletions Shared/Extensions/StringExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,15 @@ extension String {
addWordsDescriptionBolded.setAttributes(attributes, range: nsRangeOfBoldedText)
return addWordsDescriptionBolded
}

/*
Truncates the string to the specified length number of characters and appends an optional trailing string if longer.
- Parameter length: Desired maximum lengths of a string
- Parameter trailing: A 'String' that will be appended after the truncation.

- Returns: 'String' object.
*/
public func truncate(length: Int, trailing: String = "") -> String {
return (self.count > length) ? self.prefix(length) + trailing : self
}
}

0 comments on commit 84b86dc

Please sign in to comment.