Commit baf439d8 authored by lliabraa's avatar lliabraa Committed by Commit bot

Add a couple UIKit util functions in src/ui/ios/

BUG=None

Review URL: https://codereview.chromium.org/539253002

Cr-Commit-Position: refs/heads/master@{#293933}
parent 7c1f4a98
......@@ -5,25 +5,14 @@
#import "ui/ios/NSString+CrStringDrawing.h"
#include "base/logging.h"
namespace {
// Returns the closest pixel-aligned value higher than |value|, taking the scale
// factor into account. At a scale of 1, equivalent to ceil().
// TODO(lliabraa): Move this method to a common util file (crbug.com/409823).
CGFloat alignValueToUpperPixel(CGFloat value) {
CGFloat scale = [[UIScreen mainScreen] scale];
return ceil(value * scale) / scale;
}
} // namespace
#include "ui/ios/uikit_util.h"
@implementation NSString (CrStringDrawing)
- (CGSize)cr_pixelAlignedSizeWithFont:(UIFont*)font {
DCHECK(font) << "|font| can not be nil; it is used as a NSDictionary value";
NSDictionary* attributes = @{ NSFontAttributeName : font };
CGSize size = [self sizeWithAttributes:attributes];
return CGSizeMake(alignValueToUpperPixel(size.width),
alignValueToUpperPixel(size.height));
return ui::AlignSizeToUpperPixel([self sizeWithAttributes:attributes]);
}
- (CGSize)cr_sizeWithFont:(UIFont*)font {
......
......@@ -21,6 +21,8 @@
'sources': [
'NSString+CrStringDrawing.h',
'NSString+CrStringDrawing.mm',
'uikit_util.h',
'uikit_util.mm',
],
},
],
......
......@@ -19,6 +19,7 @@
],
'sources' : [
'NSString+CrStringDrawing_unittest.mm',
'uikit_util_unittest.mm',
],
},
],
......
// 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.
#ifndef UI_IOS_UIKIT_UTIL_H_
#define UI_IOS_UIKIT_UTIL_H_
#import <UIKit/UIKit.h>
#include "base/compiler_specific.h"
// UI Util containing functions that require UIKit.
namespace ui {
// Returns the closest pixel-aligned value higher than |value|, taking the scale
// factor into account. At a scale of 1, equivalent to ceil().
CGFloat AlignValueToUpperPixel(CGFloat value) WARN_UNUSED_RESULT;
// Returns the size resulting from applying AlignToUpperPixel to both
// components.
CGSize AlignSizeToUpperPixel(CGSize size) WARN_UNUSED_RESULT;
} // namespace ui
#endif // UI_IOS_UIKIT_UTIL_H_
// 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.
#include "ui/ios/uikit_util.h"
#import <UIKit/UIKit.h>
#include <cmath>
namespace ui {
CGFloat AlignValueToUpperPixel(CGFloat value) {
CGFloat scale = [[UIScreen mainScreen] scale];
return std::ceil(value * scale) / scale;
}
CGSize AlignSizeToUpperPixel(CGSize size) {
return CGSizeMake(AlignValueToUpperPixel(size.width),
AlignValueToUpperPixel(size.height));
}
} // namespace ui
// 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 <Foundation/Foundation.h>
#include "base/basictypes.h"
#include "testing/platform_test.h"
#import "ui/ios/uikit_util.h"
namespace {
typedef PlatformTest UIKitUtilTest;
TEST_F(UIKitUtilTest, AlignValueToUpperPixel) {
CGFloat scale = [[UIScreen mainScreen] scale];
// Pick a few interesting values: already aligned, aligned on retina, and
// some unaligned values that would round differently. Ensure that all are
// "integer" values within <1 of the original value in the scaled space.
CGFloat test_values[] = { 10.0, 55.5, 3.1, 2.9 };
const CGFloat kMaxAlignDelta = 0.9999;
size_t value_count = arraysize(test_values);
for (unsigned int i = 0; i < value_count; ++i) {
CGFloat aligned = ui::AlignValueToUpperPixel(test_values[i]);
EXPECT_FLOAT_EQ(aligned * scale, floor(aligned * scale));
EXPECT_NEAR(aligned * scale, test_values[i] * scale, kMaxAlignDelta);
}
}
TEST_F(UIKitUtilTest, AlignSizeToUpperPixel) {
CGFloat scale = [[UIScreen mainScreen] scale];
// Pick a few interesting values: already aligned, aligned on retina, and
// some unaligned values that would round differently. Ensure that all are
// "integer" values within <1 of the original value in the scaled space.
CGFloat test_values[] = { 10.0, 55.5, 3.1, 2.9 };
const CGFloat kMaxAlignDelta = 0.9999;
size_t value_count = arraysize(test_values);
for (unsigned int i = 0; i < value_count; ++i) {
CGFloat width = test_values[i];
CGFloat height = test_values[(i + 1) % value_count];
CGSize alignedSize = ui::AlignSizeToUpperPixel(CGSizeMake(width, height));
EXPECT_FLOAT_EQ(floor(alignedSize.width * scale),
alignedSize.width * scale);
EXPECT_FLOAT_EQ(floor(alignedSize.height * scale),
alignedSize.height * scale);
EXPECT_NEAR(width * scale, alignedSize.width * scale, kMaxAlignDelta);
EXPECT_NEAR(height * scale, alignedSize.height * scale, kMaxAlignDelta);
}
}
} // namespace
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