Permalink
Browse files

Better iOS 7 compatibility

  • Loading branch information...
1 parent 573bfec commit 1513676b081ca8a00aa913a985e2b9b761d692f3 @warrenm committed Oct 6, 2013
View
8 AHAlertView/AHAlertView.h
@@ -24,7 +24,7 @@
#import <UIKit/UIKit.h>
-#if __IPHONE_OS_VERSION_MIN_REQUIRED >= 60000
+#ifdef __IPHONE_6_0
#define AHTextAlignmentCenter NSTextAlignmentCenter
#define AHLineBreakModeWordWrap NSLineBreakByWordWrapping
#else
@@ -101,6 +101,12 @@ typedef void (^AHAlertViewButtonBlock)();
// Resets all UIAppearance modifiers back to generic iOS alert styles
+ (void)applySystemAlertAppearance;
+// Builds a text attributes dictionary from the provided text attributes
++ (NSDictionary *)textAttributesWithFont:(UIFont *)font
+ foregroundColor:(UIColor *)foregroundColor
+ shadowColor:(UIColor *)shadowColor
+ shadowOffset:(CGSize)shadowOffset;
+
// Designated initializer
- (id)initWithTitle:(NSString *)title message:(NSString *)message;
View
158 AHAlertView/AHAlertView.m
@@ -110,6 +110,55 @@ + (void)initialize
[self applySystemAlertAppearance];
}
++ (NSDictionary *)textAttributesWithFont:(UIFont *)font
+ foregroundColor:(UIColor *)foregroundColor
+ shadowColor:(UIColor *)shadowColor
+ shadowOffset:(CGSize)shadowOffset
+{
+#if __IPHONE_OS_VERSION_MIN_REQUIRED >= 60000
+ NSShadow *textShadow = [[NSShadow alloc] init];
+ textShadow.shadowColor = shadowColor;
+ textShadow.shadowOffset = shadowOffset;
+
+ return @{ NSFontAttributeName : font,
+ NSForegroundColorAttributeName : foregroundColor,
+ NSShadowAttributeName : textShadow };
+#else
+ return @{ UITextAttributeFont: font,
+ UITextAttributeTextColor : foregroundColor,
+ UITextAttributeTextShadowColor :[UIColor blackColor],
+ UITextAttributeTextShadowOffset : [NSValue valueWithCGSize:CGSizeMake(0, -1)] };
+#endif
+}
+
++ (void)getFont:(UIFont **)font
+foregroundColor:(UIColor **)foregroundColor
+ shadowColor:(UIColor **)shadowColor
+ shadowOffset:(CGSize *)shadowOffset
+fromTextAttributes:(NSDictionary *)attributes
+{
+#if __IPHONE_OS_VERSION_MIN_REQUIRED >= 60000
+ NSShadow *textShadow = attributes[NSShadowAttributeName];
+ if (font)
+ *font = attributes[NSFontAttributeName];
+ if (foregroundColor)
+ *foregroundColor = attributes[NSForegroundColorAttributeName];
+ if (shadowColor)
+ *shadowColor = textShadow.shadowColor;
+ if (shadowOffset)
+ *shadowOffset = textShadow.shadowOffset;
+#else
+ if (font)
+ *font = attributes[UITextAttributeFont];
+ if (foregroundColor)
+ *foregroundColor = attributes[UITextAttributeTextColor];
+ if (shadowColor)
+ *shadowColor = attributes[UITextAttributeTextShadowColor];
+ if (shadowOffset)
+ *shadowOffset = [attributes[UITextAttributeTextShadowOffset] CGSizeValue];
+#endif
+}
+
+ (void)applySystemAlertAppearance {
// Set up default values for all UIAppearance-compatible selectors
@@ -120,26 +169,20 @@ + (void)applySystemAlertAppearance {
[[self appearance] setContentInsets:UIEdgeInsetsMake(16, 8, 8, 8)];
// Configure text properties for title, message, and buttons so they accord with system defaults.
- [[self appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
- [UIFont boldSystemFontOfSize:17], UITextAttributeFont,
- [UIColor whiteColor], UITextAttributeTextColor,
- [UIColor blackColor], UITextAttributeTextShadowColor,
- [NSValue valueWithCGSize:CGSizeMake(0, -1)], UITextAttributeTextShadowOffset,
- nil]];
-
- [[self appearance] setMessageTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
- [UIFont systemFontOfSize:15], UITextAttributeFont,
- [UIColor whiteColor], UITextAttributeTextColor,
- [UIColor blackColor], UITextAttributeTextShadowColor,
- [NSValue valueWithCGSize:CGSizeMake(0, -1)], UITextAttributeTextShadowOffset,
- nil]];
-
- [[self appearance] setButtonTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
- [UIFont boldSystemFontOfSize:17], UITextAttributeFont,
- [UIColor whiteColor], UITextAttributeTextColor,
- [UIColor blackColor], UITextAttributeTextShadowColor,
- [NSValue valueWithCGSize:CGSizeMake(0, -1)], UITextAttributeTextShadowOffset,
- nil]];
+ [[self appearance] setTitleTextAttributes:[self textAttributesWithFont:[UIFont boldSystemFontOfSize:17]
+ foregroundColor:[UIColor whiteColor]
+ shadowColor:[UIColor blackColor]
+ shadowOffset:CGSizeMake(0, -1)]];
+
+ [[self appearance] setMessageTextAttributes:[self textAttributesWithFont:[UIFont systemFontOfSize:15]
+ foregroundColor:[UIColor whiteColor]
+ shadowColor:[UIColor blackColor]
+ shadowOffset:CGSizeMake(0, -1)]];
+
+ [[self appearance] setButtonTitleTextAttributes:[self textAttributesWithFont:[UIFont boldSystemFontOfSize:17]
+ foregroundColor:[UIColor whiteColor]
+ shadowColor:[UIColor blackColor]
+ shadowOffset:CGSizeMake(0, -1)]];
// Set basic button background images.
[[self appearance] setButtonBackgroundImage:[self normalButtonBackgroundImage] forState:UIControlStateNormal];
@@ -369,18 +412,36 @@ - (UIImage *)destructiveButtonBackgroundImageForState:(UIControlState)state
- (void)applyTextAttributes:(NSDictionary *)attributes toLabel:(UILabel *)label
{
- label.font = [attributes objectForKey:UITextAttributeFont];
- label.textColor = [attributes objectForKey:UITextAttributeTextColor];
- label.shadowColor = [attributes objectForKey:UITextAttributeTextShadowColor];
- label.shadowOffset = [[attributes objectForKey:UITextAttributeTextShadowOffset] CGSizeValue];
+ UIFont *font;
+ UIColor *textColor;
+ UIColor *shadowColor;
+ CGSize shadowOffset;
+ [[self class] getFont:&font
+ foregroundColor:&textColor
+ shadowColor:&shadowColor
+ shadowOffset:&shadowOffset
+ fromTextAttributes:attributes];
+ label.font = font;
+ label.textColor = textColor;
+ label.shadowColor = shadowColor;
+ label.shadowOffset = shadowOffset;
}
- (void)applyTextAttributes:(NSDictionary *)attributes toButton:(UIButton *)button
{
- button.titleLabel.font = [attributes objectForKey:UITextAttributeFont];
- [button setTitleColor:[attributes objectForKey:UITextAttributeTextColor] forState:UIControlStateNormal];
- [button setTitleShadowColor:[attributes objectForKey:UITextAttributeTextShadowColor] forState:UIControlStateNormal];
- button.titleLabel.shadowOffset = [[attributes objectForKey:UITextAttributeTextShadowOffset] CGSizeValue];
+ UIFont *font;
+ UIColor *textColor;
+ UIColor *shadowColor;
+ CGSize shadowOffset;
+ [[self class] getFont:&font
+ foregroundColor:&textColor
+ shadowColor:&shadowColor
+ shadowOffset:&shadowOffset
+ fromTextAttributes:attributes];
+ button.titleLabel.font = font;
+ [button setTitleColor:textColor forState:UIControlStateNormal];
+ [button setTitleShadowColor:shadowColor forState:UIControlStateNormal];
+ button.titleLabel.shadowOffset = shadowOffset;
}
- (void)applyBackgroundImages:(NSDictionary *)imagesForStates toButton:(UIButton *)button
@@ -483,14 +544,12 @@ - (void)buttonWasPressed:(UIButton *)sender {
- (void)setCenterAlignToPixel:(CGPoint)center
{
- CGFloat top = ((self.superview.bounds.size.height - self.bounds.size.height) * 0.5);
- center.y += top - floorf(top);
- self.center = center;
- // after assign the center, just make sure the origin is not on an half pixel.
- CGRect viewFrame = self.frame;
- viewFrame.origin.x = floorf(viewFrame.origin.x);
- viewFrame.origin.y = floorf(viewFrame.origin.y);
- self.frame = viewFrame;
+ self.center = center;
+ CGRect frame = self.frame;
+ CGFloat inverseScale = 1.0 / [[UIScreen mainScreen] scale];
+ CGPoint centerCorrection = CGPointMake(fmod(frame.origin.x, inverseScale), fmod(frame.origin.y, inverseScale));
+ CGPoint roundedCenter = CGPointMake(center.x - centerCorrection.x, center.y - centerCorrection.y);
+ self.center = roundedCenter;
}
- (void)performPresentationAnimation
@@ -589,7 +648,7 @@ - (void)performDismissalAnimation
// off the screen while rotating slightly off-kilter. Use sparingly.
[UIView animateWithDuration:0.6
delay:0.0
- options:UIViewAnimationOptionCurveEaseIn
+ options:UIViewAnimationOptionCurveEaseIn | UIViewAnimationOptionBeginFromCurrentState
animations:^
{
CGPoint offset = CGPointMake(0, self.superview.bounds.size.height * 1.5);
@@ -703,6 +762,19 @@ - (void)layoutSubviews {
[self reposition];
}
+- (CGSize)sizeOfString:(NSString *)string withFont:(UIFont *)font constrainedToSize:(CGSize)size
+{
+#if __IPHONE_OS_VERSION_MIN_REQUIRED >= 70000
+ NSDictionary *attributes = @{ NSFontAttributeName : font };
+ return [string boundingRectWithSize:size
+ options:NSStringDrawingUsesLineFragmentOrigin
+ attributes:attributes
+ context:NULL].size;
+#else
+ return [string sizeWithFont:font constrainedToSize:size lineBreakMode:AHLineBreakModeWordWrap];
+#endif
+}
+
- (CGRect)layoutTitleLabelWithinRect:(CGRect)boundingRect
{
// Lazily generate a title label.
@@ -712,9 +784,9 @@ - (CGRect)layoutTitleLabelWithinRect:(CGRect)boundingRect
// Assign appropriate text attributes to this label, then calculate a suitable frame for it.
[self applyTextAttributes:self.titleTextAttributes toLabel:self.titleLabel];
self.titleLabel.text = self.title;
- CGSize titleSize = [self.titleLabel.text sizeWithFont:self.titleLabel.font
- constrainedToSize:boundingRect.size
- lineBreakMode:AHLineBreakModeWordWrap];
+ CGSize titleSize = [self sizeOfString:self.titleLabel.text
+ withFont:self.titleLabel.font
+ constrainedToSize:boundingRect.size];
self.titleLabel.frame = CGRectMake(boundingRect.origin.x, boundingRect.origin.y,
boundingRect.size.width, titleSize.height);
@@ -733,9 +805,9 @@ - (CGRect) layoutMessageLabelWithinRect:(CGRect)boundingRect
// Assign appropriate text attributes to this label, then calculate a suitable frame for it.
[self applyTextAttributes:self.messageTextAttributes toLabel:self.messageLabel];
self.messageLabel.text = self.message;
- CGSize messageSize = [self.messageLabel.text sizeWithFont:self.messageLabel.font
- constrainedToSize:boundingRect.size
- lineBreakMode:AHLineBreakModeWordWrap];
+ CGSize messageSize = [self sizeOfString:self.messageLabel.text
+ withFont:self.messageLabel.font
+ constrainedToSize:boundingRect.size];
self.messageLabel.frame = CGRectMake(boundingRect.origin.x, boundingRect.origin.y,
boundingRect.size.width, messageSize.height);
View
11 AHAlertViewSample.xcodeproj/project.pbxproj
@@ -206,6 +206,11 @@
CLASSPREFIX = AH;
LastUpgradeCheck = 0460;
ORGANIZATIONNAME = "Auerhaus Development, LLC";
+ TargetAttributes = {
+ 80C6F0FE15FE54A900ABF313 = {
+ DevelopmentTeam = 5XU8G72NQS;
+ };
+ };
};
buildConfigurationList = 80C6F0F915FE54A900ABF313 /* Build configuration list for PBXProject "AHAlertViewSample" */;
compatibilityVersion = "Xcode 3.2";
@@ -330,11 +335,14 @@
80C6F11815FE54A900ABF313 /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
+ CODE_SIGN_IDENTITY = "iPhone Developer";
+ "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = "AHAlertViewSample/AHAlertViewSample-Prefix.pch";
INFOPLIST_FILE = "AHAlertViewSample/AHAlertViewSample-Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 5.1;
PRODUCT_NAME = "$(TARGET_NAME)";
+ PROVISIONING_PROFILE = "";
TARGETED_DEVICE_FAMILY = "1,2";
WRAPPER_EXTENSION = app;
};
@@ -343,11 +351,14 @@
80C6F11915FE54A900ABF313 /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
+ CODE_SIGN_IDENTITY = "iPhone Developer";
+ "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = "AHAlertViewSample/AHAlertViewSample-Prefix.pch";
INFOPLIST_FILE = "AHAlertViewSample/AHAlertViewSample-Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 5.1;
PRODUCT_NAME = "$(TARGET_NAME)";
+ PROVISIONING_PROFILE = "";
TARGETED_DEVICE_FAMILY = "1,2";
WRAPPER_EXTENSION = app;
};
View
36 AHAlertViewSample/AHAlertSampleViewController.m
@@ -134,27 +134,21 @@ - (void)applyCustomAlertAppearance
forState:UIControlStateNormal];
[[AHAlertView appearance] setButtonBackgroundImage:normalButtonImage
forState:UIControlStateNormal];
-
- [[AHAlertView appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
- [UIFont boldSystemFontOfSize:18], UITextAttributeFont,
- [UIColor whiteColor], UITextAttributeTextColor,
- [UIColor blackColor], UITextAttributeTextShadowColor,
- [NSValue valueWithCGSize:CGSizeMake(0, -1)], UITextAttributeTextShadowOffset,
- nil]];
-
- [[AHAlertView appearance] setMessageTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
- [UIFont systemFontOfSize:14], UITextAttributeFont,
- [UIColor colorWithWhite:0.8 alpha:1.0], UITextAttributeTextColor,
- [UIColor blackColor], UITextAttributeTextShadowColor,
- [NSValue valueWithCGSize:CGSizeMake(0, -1)], UITextAttributeTextShadowOffset,
- nil]];
-
- [[AHAlertView appearance] setButtonTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
- [UIFont boldSystemFontOfSize:14], UITextAttributeFont,
- [UIColor whiteColor], UITextAttributeTextColor,
- [UIColor blackColor], UITextAttributeTextShadowColor,
- [NSValue valueWithCGSize:CGSizeMake(0, -1)], UITextAttributeTextShadowOffset,
- nil]];
+
+ [[AHAlertView appearance] setTitleTextAttributes:[AHAlertView textAttributesWithFont:[UIFont boldSystemFontOfSize:18]
+ foregroundColor:[UIColor whiteColor]
+ shadowColor:[UIColor blackColor]
+ shadowOffset:CGSizeMake(0, -1)]];
+
+ [[AHAlertView appearance] setMessageTextAttributes:[AHAlertView textAttributesWithFont:[UIFont systemFontOfSize:14]
+ foregroundColor:[UIColor colorWithWhite:0.8 alpha:1.0]
+ shadowColor:[UIColor blackColor]
+ shadowOffset:CGSizeMake(0, -1)]];
+
+ [[AHAlertView appearance] setButtonTitleTextAttributes:[AHAlertView textAttributesWithFont:[UIFont boldSystemFontOfSize:14]
+ foregroundColor:[UIColor whiteColor]
+ shadowColor:[UIColor blackColor]
+ shadowOffset:CGSizeMake(0, -1)]];
}
@end

0 comments on commit 1513676

Please sign in to comment.