diff --git a/Client/Frontend/Browser/Toolbars/UrlBar/TabLocationView.swift b/Client/Frontend/Browser/Toolbars/UrlBar/TabLocationView.swift index 98be8c29675..f876aad282a 100644 --- a/Client/Frontend/Browser/Toolbars/UrlBar/TabLocationView.swift +++ b/Client/Frontend/Browser/Toolbars/UrlBar/TabLocationView.swift @@ -134,6 +134,14 @@ class TabLocationView: UIView { urlTextField.clipsToBounds = true urlTextField.textColor = .braveLabel urlTextField.isEnabled = false + urlTextField.defaultTextAttributes[.paragraphStyle] = { + let paragraphStyle = (urlTextField + .defaultTextAttributes[.paragraphStyle, default: NSParagraphStyle.default] as! NSParagraphStyle) + .mutableCopy() as! NSMutableParagraphStyle + paragraphStyle.alignment = .center + paragraphStyle.lineBreakMode = .byTruncatingHead + return paragraphStyle + }() // Remove the default drop interaction from the URL text field so that our // custom drop interaction on the BVC can accept dropped URLs. if let dropInteraction = urlTextField.textDropInteraction { @@ -406,8 +414,7 @@ class TabLocationView: UIView { } fileprivate func updateTextWithURL() { - (urlTextField as? DisplayTextField)?.hostString = url?.withoutWWW.host ?? "" - urlTextField.text = url?.withoutWWW.schemelessAbsoluteString.trim("/") + urlTextField.text = url?.origin.url?.schemelessAbsoluteDisplayString } } @@ -474,8 +481,6 @@ extension TabLocationView { class DisplayTextField: UITextField { weak var accessibilityActionsSource: AccessibilityActionsSource? - var hostString: String = "" - let pathPadding: CGFloat = 5.0 override var accessibilityCustomActions: [UIAccessibilityCustomAction]? { get { @@ -494,21 +499,6 @@ class DisplayTextField: UITextField { override var canBecomeFirstResponder: Bool { return false } - - // This override is done in case the eTLD+1 string overflows the width of textField. - // In that case the textRect is adjusted to show right aligned and truncate left. - // Since this textField changes with WebView domain change, performance implications are low. - override func textRect(forBounds bounds: CGRect) -> CGRect { - var rect: CGRect = super.textRect(forBounds: bounds) - - if let size: CGSize = (self.hostString as NSString?)?.size(withAttributes: [.font: self.font!]) { - if size.width > self.bounds.width { - rect.origin.x = rect.origin.x - (size.width + pathPadding - self.bounds.width) - rect.size.width = size.width + pathPadding - } - } - return rect - } } private class CustomSeparatorView: UIView { diff --git a/Tests/ClientTests/DisplayTextFieldTest.swift b/Tests/ClientTests/DisplayTextFieldTest.swift deleted file mode 100644 index 4fb8ac336c3..00000000000 --- a/Tests/ClientTests/DisplayTextFieldTest.swift +++ /dev/null @@ -1,41 +0,0 @@ -// 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/. - -@testable import Brave -import XCTest - -class DisplayTextFieldTest: XCTestCase { - - func testRightAlignedTLD() { - let textField = DisplayTextField(frame: CGRect(width: 250, height: 44)) - - let urlCases: [URL] = [ - URL(string: "https://www.google.com")!, - URL(string: "http://myaccountsecure.testmycase.co.uk/asd?a=1")!, - URL(string: "http://testmycase.co.uk/something/path/asd?a=1")!, - URL(string: "http://myaccountsecure.secured.testmycase.co.uk/asd?a=1")!, - URL(string: "http://myaccountsecure.testmycase.co.uk")!, - ] - urlCases.forEach({ textField.assertWidth(text: $0.schemelessAbsoluteString, hostString: $0.host ?? "") }) - - } - -} - -extension DisplayTextField { - func assertWidth(text: String, hostString: String) { - self.hostString = hostString - self.text = text - let rect = self.textRect(forBounds: self.bounds) - if rect.width > self.bounds.width, let upperBound = text.range(of: hostString)?.upperBound { - let pathString = text[upperBound...] - let widthPath = (pathString as NSString).size(withAttributes: [.font: self.font!]).width - let widthText = (text as NSString).size(withAttributes: [.font: self.font!]).width - - let test1 = rect.width + widthPath - pathPadding == widthText - let test2 = rect.origin.x == self.bounds.width - widthText + widthPath - pathPadding - XCTAssert(test1 && test2) - } - } -}