1 // Copyright 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef IOS_CHROME_BROWSER_UI_UIKIT_UI_UTIL_H_
6 #define IOS_CHROME_BROWSER_UI_UIKIT_UI_UTIL_H_
8 #include <CoreGraphics/CoreGraphics.h>
9 #import <Foundation/Foundation.h>
10 #import <UIKit/UIKit.h>
12 #import "ios/chrome/browser/ui/ui_util.h"
14 // UI Util containing functions that require UIKit.
16 enum { FONT_HELVETICA
, FONT_HELVETICA_NEUE
, FONT_HELVETICA_NEUE_LIGHT
};
18 // Utility function to set the |element|'s accessibility label to the localized
19 // message corresponding to |idsAccessibilityLabel| and its accessibility
20 // identifier to |englishUiAutomationName|.
21 // Call SetA11yLabelAndUiAutomationName() if |element| is accessible and its
22 // a11y label should be localized.
23 // By convention |englishUiAutomationName| must be equal to the English
24 // localized string corresponding to |idsAccessibilityLabel|.
25 // |englishUiAutomationName| is the name used in JavaScript UI Automation test
26 // scripts to identify the |element|.
27 void SetA11yLabelAndUiAutomationName(UIView
* element
,
28 int idsAccessibilityLabel
,
29 NSString
* englishUiAutomationName
);
31 // Sets the given |button|'s width to exactly fit its image and text. Does not
32 // modify the button's height.
33 void GetSizeButtonWidthToFit(UIButton
* button
);
35 // Translates the given |view|'s frame. Sets a new frame instead of applying a
36 // transform to the existing frame.
37 void TranslateFrame(UIView
* view
, UIOffset offset
);
39 // Returns a UIFont. |fontFace| is one of the defined enumerated values
40 // to avoid spelling mistakes.
41 UIFont
* GetUIFont(int fontFace
, bool isBold
, CGFloat fontSize
);
43 // Adds a border shadow around |view|.
44 void AddBorderShadow(UIView
* view
, CGFloat offset
, UIColor
* color
);
46 // Adds a rounded-rectangle border shadow around a view.
47 void AddRoundedBorderShadow(UIView
* view
, CGFloat radius
, UIColor
* color
);
49 // Captures and returns an autoreleased rendering of the |view|.
50 // The |view| is assumed to be opaque and the returned image does
51 // not have an alpha channel. The scale parameter is used as a scale factor
52 // for the rendering context. Using 0.0 as scale will result in the device's
53 // main screen scale to be used.
54 UIImage
* CaptureView(UIView
* view
, CGFloat scale
);
56 // Converts input image and returns a grey scaled version.
57 UIImage
* GreyImage(UIImage
* image
);
59 // Returns the color that should be used for the background of all Settings
61 UIColor
* GetSettingsBackgroundColor();
63 // Returns the color used as the main color for primary action buttons.
64 UIColor
* GetPrimaryActionButtonColor();
66 // Returns an UIColor with |rgb| and |alpha|. The caller should pass the RGB
67 // value in hexadecimal as this is the typical way they are provided by UX.
68 // For example a call to |UIColorFromRGB(0xFF7D40, 1.0)| returns an orange
70 inline UIColor
* UIColorFromRGB(int rgb
, CGFloat alpha
= 1.0) {
71 return [UIColor colorWithRed
:((CGFloat
)((rgb
& 0xFF0000) >> 16)) / 255.0
72 green
:((CGFloat
)((rgb
& 0x00FF00) >> 8)) / 255.0
73 blue
:((CGFloat
)(rgb
& 0x0000FF)) / 255.0
77 // Returns an image resized to |targetSize|. It first calculate the projection
78 // by calling CalculateProjection() and then create a new image of the desired
79 // size and project the correct subset of the originla image onto it.
81 // Image interpolation level for resizing is set to kCGInterpolationDefault.
83 // The resize always preserves the scale of the original image.
84 UIImage
* ResizeImage(UIImage
* image
,
86 ProjectionMode projectionMode
);
88 // Returns a slightly blurred image darkened enough to provide contrast for
89 // white text to be readable.
90 UIImage
* DarkenImage(UIImage
* image
);
92 // Applies various effects to an image. This method can apply a blur over a
93 // |radius|, superimpose a |tintColor| (an alpha of 0.6 on the color is a good
94 // approximation to look like iOS tint colors) or saturate the image colors by
95 // applying a |saturationDeltaFactor| (negative to desaturate, positive to
96 // saturate). The optional |maskImage| is used to limit the effect of the blur
97 // and/or saturation to a portion of the image.
98 UIImage
* BlurImage(UIImage
* image
,
101 CGFloat saturationDeltaFactor
,
104 // Returns a cropped image using |cropRect| on |image|.
105 UIImage
* CropImage(UIImage
* image
, const CGRect
& cropRect
);
107 // Returns the interface orientation of the app.
108 UIInterfaceOrientation
GetInterfaceOrientation();
110 // Returns the height of the keyboard in the current orientation.
111 CGFloat
CurrentKeyboardHeight(NSValue
* keyboardFrameValue
);
113 // Create 1x1px image from |color|.
114 UIImage
* ImageWithColor(UIColor
* color
);
116 // Returns a circular image of width |width| based on |image| scaled up or
117 // down. If the source image is not square, the image is first cropped.
118 UIImage
* CircularImageFromImage(UIImage
* image
, CGFloat width
);
120 // Returns the linear interpolated color from |firstColor| to |secondColor| by
121 // the given |fraction|. Requires that both colors are in RGB or monochrome
122 // color space. |fraction| is a decimal value between 0.0 and 1.0.
123 UIColor
* InterpolateFromColorToColor(UIColor
* firstColor
,
124 UIColor
* secondColor
,
127 // Applies all |constraints| to all views in |subviewsDictionary| in the
128 // superview |parentView|.
129 void ApplyVisualConstraints(NSArray
* constraints
,
130 NSDictionary
* subviewsDictionary
,
133 // Applies all |constraints| with |metrics| to all views in |subviewsDictionary|
134 // in the superview |parentView|
135 void ApplyVisualConstraintsWithMetrics(NSArray
* constraints
,
136 NSDictionary
* subviewsDictionary
,
137 NSDictionary
* metrics
,
140 // Adds a constraint that |subview| is center aligned horizontally in
142 // |subview| must be a subview of |parentView|.
143 void AddSameCenterXConstraint(UIView
* parentView
, UIView
* subview
);
145 // Adds a constraint that |subview1| and |subview2| are center aligned
146 // horizontally on |parentView|.
147 // |subview1| and |subview2| must be subview of |parentView|.
148 void AddSameCenterXConstraint(UIView
*parentView
, UIView
*subview1
,
151 // Adds a constraint that |subview| is center aligned vertically in
153 // |subview| must be a subview of |parentView|.
154 void AddSameCenterYConstraint(UIView
* parentView
, UIView
* subview
);
156 // Adds a constraint that |subview1| and |subview2| are center aligned
157 // vertically on |parentView|.
158 // |subview1| and |subview2| must be subview of |parentView|.
159 void AddSameCenterYConstraint(UIView
* parentView
,
162 // Whether the UI is configured for right to left layout.
163 // The implementation will use the local in order to get the UI layout direction
164 // for version of iOS under 9.
165 // TODO(jbbegue): Use base::i18n::IsRTL() instead when it will support RTL
166 // pseudo language. Remove that method once base::i18n::IsRTL() is fixed.
168 BOOL
IsRTLUILayout();
170 // Returns true if the main application window's rootViewController is a compact
171 // iPad horizontal size class.
172 bool IsCompactTablet();
174 // Returns true if the horizontal |sizeClass| is compact and device is an iPad.
175 bool IsCompactTabletSizeClass(UIUserInterfaceSizeClass sizeClass
);
177 #endif // IOS_CHROME_BROWSER_UI_UIKIT_UI_UTIL_H_