Commit 5c01bece authored by edchin's avatar edchin Committed by Commit Bot

[ios] Remove FixOrphanWord

UIKit has fixed orphaned word in UILabel since iOS 11.
Our custom implementation of fixing orphaned words
is no longer necessary.
Furthermore, adding an arbitrary new line in a UILabel
has unintended consequences, such as failing EG tests
that would otherwise pass.

Change-Id: Ifc5ca16d8fca83aa67306d94ceac9db132ded45a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2129241Reviewed-by: default avatarKurt Horimoto <kkhorimoto@chromium.org>
Reviewed-by: default avataredchin <edchin@chromium.org>
Commit-Queue: edchin <edchin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#755082}
parent 950810de
...@@ -71,10 +71,7 @@ source_set("constants") { ...@@ -71,10 +71,7 @@ source_set("constants") {
source_set("unit_tests") { source_set("unit_tests") {
configs += [ "//build/config/compiler:enable_arc" ] configs += [ "//build/config/compiler:enable_arc" ]
testonly = true testonly = true
sources = [ sources = [ "welcome_to_chrome_view_controller_unittest.mm" ]
"first_run_util_unittest.mm",
"welcome_to_chrome_view_controller_unittest.mm",
]
deps = [ deps = [
":first_run", ":first_run",
"//base", "//base",
......
...@@ -27,12 +27,6 @@ extern NSString* const kChromeFirstRunUIWillFinishNotification; ...@@ -27,12 +27,6 @@ extern NSString* const kChromeFirstRunUIWillFinishNotification;
// of Service modal view. // of Service modal view.
extern NSString* const kChromeFirstRunUIDidFinishNotification; extern NSString* const kChromeFirstRunUIDidFinishNotification;
// Checks if the last line of the label only contains one word and if so, insert
// a newline character before the second to last word so that there are two
// words on the last line. Should only be called on labels that span multiple
// lines. Returns YES if a newline was added.
BOOL FixOrphanWord(UILabel* label);
// Creates the First Run sentinel file so that the user will not be shown First // Creates the First Run sentinel file so that the user will not be shown First
// Run on subsequent cold starts. The user is considered done with First Run // Run on subsequent cold starts. The user is considered done with First Run
// only after a successful sign-in or explicitly skipping signing in. First Run // only after a successful sign-in or explicitly skipping signing in. First Run
......
...@@ -37,45 +37,6 @@ NSString* const kChromeFirstRunUIDidFinishNotification = ...@@ -37,45 +37,6 @@ NSString* const kChromeFirstRunUIDidFinishNotification =
namespace { namespace {
NSString* RemoveLastWord(NSString* text) {
__block NSRange range = NSMakeRange(0, [text length]);
NSStringEnumerationOptions options = NSStringEnumerationByWords |
NSStringEnumerationReverse |
NSStringEnumerationSubstringNotRequired;
// Enumerate backwards through the words in |text| to get the range of the
// last word.
[text
enumerateSubstringsInRange:range
options:options
usingBlock:^(NSString* substring, NSRange substringRange,
NSRange enclosingRange, BOOL* stop) {
range = substringRange;
*stop = YES;
}];
return [text substringToIndex:range.location];
}
NSString* InsertNewlineBeforeNthToLastWord(NSString* text, int index) {
__block NSRange range = NSMakeRange(0, [text length]);
__block int count = 0;
NSStringEnumerationOptions options = NSStringEnumerationByWords |
NSStringEnumerationReverse |
NSStringEnumerationSubstringNotRequired;
[text
enumerateSubstringsInRange:range
options:options
usingBlock:^(NSString* substring, NSRange substringRange,
NSRange enclosingRange, BOOL* stop) {
range = substringRange;
count++;
*stop = count == index;
}];
NSMutableString* textWithNewline = [text mutableCopy];
[textWithNewline insertString:@"\n" atIndex:range.location];
return textWithNewline;
}
// Trampoline method for Bind to create the sentinel file. // Trampoline method for Bind to create the sentinel file.
void CreateSentinel() { void CreateSentinel() {
base::File::Error file_error; base::File::Error file_error;
...@@ -116,30 +77,6 @@ void RecordFirstRunMetricsInternal(ChromeBrowserState* browserState, ...@@ -116,30 +77,6 @@ void RecordFirstRunMetricsInternal(ChromeBrowserState* browserState,
} // namespace } // namespace
BOOL FixOrphanWord(UILabel* label) {
// Calculate the height of the label's text.
NSString* text = label.text;
CGSize textSize =
[text cr_boundingSizeWithSize:label.frame.size font:label.font];
CGFloat textHeight = AlignValueToPixel(textSize.height);
// Remove the last word and calculate the height of the new text.
NSString* textMinusLastWord = RemoveLastWord(text);
CGSize minusLastWordSize =
[textMinusLastWord cr_boundingSizeWithSize:label.frame.size
font:label.font];
CGFloat minusLastWordHeight = AlignValueToPixel(minusLastWordSize.height);
// Check if removing the last word results in a smaller height.
if (minusLastWordHeight < textHeight) {
// The last word was the only word on its line. Add a newline before the
// second to last word.
label.text = InsertNewlineBeforeNthToLastWord(text, 2);
return true;
}
return false;
}
void WriteFirstRunSentinelAndRecordMetrics(ChromeBrowserState* browserState, void WriteFirstRunSentinelAndRecordMetrics(ChromeBrowserState* browserState,
BOOL sign_in_attempted, BOOL sign_in_attempted,
BOOL has_sso_account) { BOOL has_sso_account) {
......
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import <UIKit/UIKit.h>
#include "ios/chrome/browser/ui/first_run/first_run_util.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/platform_test.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
namespace {
using UICommonTest = PlatformTest;
TEST_F(UICommonTest, TestFixOrphanWord) {
NSString* englishString =
@"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec tempus"
" dignissim congue. Morbi pulvinar vitae purus at mollis. Sed laoreet "
"euismod neque, eget laoreet nisi porttitor sed.";
NSString* englishStringWithOrphan =
@"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec tempus"
" dignissim congue. Morbi pulvinar vitae purus at mollis. Sed laoreet "
"euismod neque, eget laoreet nisi.";
// TODO(crbug.com/675342): clang_format does a poor job here. Remove when
// fixed in clang_format.
// clang-format off
NSString* chineseString =
@"那只敏捷的棕色狐狸跃过那只懒狗。那只敏捷的棕色狐狸跃过那只懒狗。"
"那只敏捷的棕色狐狸跃过那只懒狗。那只敏捷的棕色狐狸跃过那只懒狗。"
"那只敏捷的棕色狐狸跃过那只懒狗。那只敏捷的棕色狐狸跃过那只懒狗。";
NSString* chineseStringWithOrphan =
@"那只敏捷的棕色狐狸跃过那只懒狗。那只敏捷的棕色狐狸跃过那只懒狗。"
"那只敏捷的棕色狐狸跃过那只懒狗。快速狐狸";
// clang-format on
UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 500)];
[label setText:englishString];
FixOrphanWord(label);
NSRange range = [[label text] rangeOfString:@"\n"];
// Check that the label's text does not contain a newline.
EXPECT_EQ(NSNotFound, static_cast<NSInteger>(range.location));
[label setText:englishStringWithOrphan];
FixOrphanWord(label);
range = [[label text] rangeOfString:@"\n"];
// Check that the label's text contains a newline.
EXPECT_NE(NSNotFound, static_cast<NSInteger>(range.location));
// Check the words after the newline.
NSString* wordsAfterNewline =
[[label text] substringFromIndex:(range.location + range.length)];
EXPECT_TRUE([@"laoreet nisi." isEqualToString:wordsAfterNewline]);
[label setText:chineseString];
FixOrphanWord(label);
range = [[label text] rangeOfString:@"\n"];
// Check that the label's text does not contain a newline.
EXPECT_EQ(NSNotFound, static_cast<NSInteger>(range.location));
[label setText:chineseStringWithOrphan];
FixOrphanWord(label);
range = [[label text] rangeOfString:@"\n"];
// Check that the label's text contains a newline.
ASSERT_NE(NSNotFound, static_cast<NSInteger>(range.location));
// Check the words after the newline.
wordsAfterNewline =
[[label text] substringFromIndex:(range.location + range.length)];
EXPECT_TRUE([@"快速狐狸" isEqualToString:wordsAfterNewline]);
}
}
...@@ -399,22 +399,6 @@ const char kPrivacyNoticeUrl[] = "internal://privacy-notice"; ...@@ -399,22 +399,6 @@ const char kPrivacyNoticeUrl[] = "internal://privacy-notice";
DCHECK_NE(0u, privacyLinkTextRange.length); DCHECK_NE(0u, privacyLinkTextRange.length);
self.TOSLabel.text = TOSText; self.TOSLabel.text = TOSText;
if (FixOrphanWord(self.TOSLabel)) {
// If a newline is inserted, check whether it was added mid-link and adjust
// |tosLinkTextRange| and |privacyLinkTextRange| accordingly.
NSRange newlineRange = [self.TOSLabel.text rangeOfString:@"\n"
options:0
range:tosLinkTextRange];
if (newlineRange.length) {
tosLinkTextRange.location++;
privacyLinkTextRange.location++;
}
newlineRange = [self.TOSLabel.text rangeOfString:@"\n"
options:0
range:privacyLinkTextRange];
if (newlineRange.length)
privacyLinkTextRange.location++;
}
__weak WelcomeToChromeView* weakSelf = self; __weak WelcomeToChromeView* weakSelf = self;
ProceduralBlockWithURL action = ^(const GURL& url) { ProceduralBlockWithURL action = ^(const GURL& url) {
...@@ -465,7 +449,6 @@ const char kPrivacyNoticeUrl[] = "internal://privacy-notice"; ...@@ -465,7 +449,6 @@ const char kPrivacyNoticeUrl[] = "internal://privacy-notice";
CGRectMake(optInLabelOriginX, CGRectMake(optInLabelOriginX,
CGRectGetMaxY(self.TOSLabel.frame) + optInLabelTopPadding, CGRectGetMaxY(self.TOSLabel.frame) + optInLabelTopPadding,
optInLabelSize.width, optInLabelSize.height)); optInLabelSize.width, optInLabelSize.height));
FixOrphanWord(self.optInLabel);
} }
- (void)layoutCheckBoxButton { - (void)layoutCheckBoxButton {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment