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 // UI Util containing functions that require UIKit.
14 enum { FONT_HELVETICA
, FONT_HELVETICA_NEUE
, FONT_HELVETICA_NEUE_LIGHT
};
16 // Utility function to set the |element|'s accessibility label to the localized
17 // message corresponding to |idsAccessibilityLabel| and its accessibility
18 // identifier to |englishUiAutomationName|.
19 // Call SetA11yLabelAndUiAutomationName() if |element| is accessible and its
20 // a11y label should be localized.
21 // By convention |englishUiAutomationName| must be equal to the English
22 // localized string corresponding to |idsAccessibilityLabel|.
23 // |englishUiAutomationName| is the name used in JavaScript UI Automation test
24 // scripts to identify the |element|.
25 void SetA11yLabelAndUiAutomationName(UIView
* element
,
26 int idsAccessibilityLabel
,
27 NSString
* englishUiAutomationName
);
29 // Sets the given |button|'s width to exactly fit its image and text. Does not
30 // modify the button's height.
31 void GetSizeButtonWidthToFit(UIButton
* button
);
33 // Translates the given |view|'s frame. Sets a new frame instead of applying a
34 // transform to the existing frame.
35 void TranslateFrame(UIView
* view
, UIOffset offset
);
37 // Returns a UIFont. |fontFace| is one of the defined enumerated values
38 // to avoid spelling mistakes.
39 UIFont
* GetUIFont(int fontFace
, bool isBold
, CGFloat fontSize
);
41 // Adds a border shadow around |view|.
42 void AddBorderShadow(UIView
* view
, CGFloat offset
, UIColor
* color
);
44 // Adds a rounded-rectangle border shadow around a view.
45 void AddRoundedBorderShadow(UIView
* view
, CGFloat radius
, UIColor
* color
);
47 // Captures and returns an autoreleased rendering of the |view|.
48 // The |view| is assumed to be opaque and the returned image does
49 // not have an alpha channel. The scale parameter is used as a scale factor
50 // for the rendering context. Using 0.0 as scale will result in the device's
51 // main screen scale to be used.
52 UIImage
* CaptureView(UIView
* view
, CGFloat scale
);
54 // Converts input image and returns a grey scaled version.
55 UIImage
* GreyImage(UIImage
* image
);
57 // Returns the color that should be used for the background of all Settings
59 UIColor
* GetSettingsBackgroundColor();
61 // Returns the color used as the main color for primary action buttons.
62 UIColor
* GetPrimaryActionButtonColor();
64 // Returns an UIColor with |rgb| and |alpha|. The caller should pass the RGB
65 // value in hexadecimal as this is the typical way they are provided by UX.
66 // For example a call to |UIColorFromRGB(0xFF7D40, 1.0)| returns an orange
68 inline UIColor
* UIColorFromRGB(int rgb
, CGFloat alpha
= 1.0) {
69 return [UIColor colorWithRed
:((CGFloat
)((rgb
& 0xFF0000) >> 16)) / 255.0
70 green
:((CGFloat
)((rgb
& 0x00FF00) >> 8)) / 255.0
71 blue
:((CGFloat
)(rgb
& 0x0000FF)) / 255.0
75 // Returns an image resized to |targetSize|. It first calculate the projection
76 // by calling CalculateProjection() and then create a new image of the desired
77 // size and project the correct subset of the originla image onto it.
79 // Image interpolation level for resizing is set to kCGInterpolationDefault.
81 // The resize always preserves the scale of the original image.
82 UIImage
* ResizeImage(UIImage
* image
,
84 BOOL preserveAspectRatio
,
87 // Returns a slightly blurred image darkened enough to provide contrast for
88 // white text to be readable.
89 UIImage
* DarkenImage(UIImage
* image
);
91 // Applies various effects to an image. This method can apply a blur over a
92 // |radius|, superimpose a |tintColor| (an alpha of 0.6 on the color is a good
93 // approximation to look like iOS tint colors) or saturate the image colors by
94 // applying a |saturationDeltaFactor| (negative to desaturate, positive to
95 // saturate). The optional |maskImage| is used to limit the effect of the blur
96 // and/or saturation to a portion of the image.
97 UIImage
* BlurImage(UIImage
* image
,
100 CGFloat saturationDeltaFactor
,
103 // Returns the interface orientation of the app.
104 UIInterfaceOrientation
GetInterfaceOrientation();
106 // Returns the height of the keyboard in the current orientation.
107 CGFloat
CurrentKeyboardHeight(NSValue
* keyboardFrameValue
);
109 // Create 1x1px image from |color|.
110 UIImage
* ImageWithColor(UIColor
* color
);
112 // Returns a circular image of width |width| based on |image| scaled up or
113 // down. If the source image is not square, the image is first cropped.
114 UIImage
* CircularImageFromImage(UIImage
* image
, CGFloat width
);
116 // Returns the linear interpolated color from |firstColor| to |secondColor| by
117 // the given |fraction|. Requires that both colors are in RGB or monochrome
118 // color space. |fraction| is a decimal value between 0.0 and 1.0.
119 UIColor
* InterpolateFromColorToColor(UIColor
* firstColor
,
120 UIColor
* secondColor
,
123 // Applies all |constraints| to all views in |subviewsDictionary| in the
124 // superview |parentView|.
125 void ApplyVisualConstraints(NSArray
* constraints
,
126 NSDictionary
* subviewsDictionary
,
129 // Adds a constraint that |subview| is center aligned horizontally in
131 // |subview| must be a subiew of |parentView|.
132 void AddSameCenterXConstraint(UIView
* parentView
, UIView
* subview
);
134 // Adds a constraint that |subview1| and |subview2| are center aligned
135 // vertically on |parentView|.
136 // |subview1| and |subview2| must be subiews of |parentView|.
137 void AddSameCenterYConstraint(UIView
* parentView
,
141 #endif // IOS_CHROME_BROWSER_UI_UIKIT_UI_UTIL_H_