Skip to content

Commit

Permalink
Merge pull request #27 from matej/master
Browse files Browse the repository at this point in the history
Various fixes
  • Loading branch information
matej committed Nov 3, 2011
2 parents 3e3e361 + 645a9de commit e324cba
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 13 deletions.
4 changes: 3 additions & 1 deletion Demo/Classes/HudDemoViewController.m
Expand Up @@ -73,7 +73,8 @@ - (IBAction)showWithDetailsLabel:(id)sender {

HUD.delegate = self;
HUD.labelText = @"Loading";
HUD.detailsLabelText = @"updating data";
HUD.detailsLabelText = @"udating data";
HUD.square = YES;

[HUD showWhileExecuting:@selector(myTask) onTarget:self withObject:nil animated:YES];
}
Expand Down Expand Up @@ -119,6 +120,7 @@ - (IBAction)showWithLabelMixed:(id)sender {

HUD.delegate = self;
HUD.labelText = @"Connecting";
HUD.minSize = CGSizeMake(135.f, 135.f);

[HUD showWhileExecuting:@selector(myMixedTask) onTarget:self withObject:nil animated:YES];
}
Expand Down
12 changes: 12 additions & 0 deletions MBProgressHUD.h
Expand Up @@ -84,6 +84,9 @@ typedef enum {
float width;
float height;

CGSize minSize;
BOOL square;

float margin;

BOOL dimBackground;
Expand Down Expand Up @@ -276,6 +279,15 @@ typedef enum {
*/
@property (assign) float progress;

/**
* The minimum size of the HUD bezel. Defaults to CGSizeZero.
*/
@property (assign) CGSize minSize;

/**
* Force the HUD dimensions to be eual if possible.
*/
@property (assign, getter = isSquare) BOOL square;

/**
* Display the HUD. You need to make sure that the main thread completes its run loop soon after this method call so
Expand Down
44 changes: 32 additions & 12 deletions MBProgressHUD.m
Expand Up @@ -54,6 +54,8 @@ @implementation MBProgressHUD
@synthesize height;
@synthesize xOffset;
@synthesize yOffset;
@synthesize minSize;
@synthesize square;
@synthesize margin;
@synthesize dimBackground;

Expand Down Expand Up @@ -261,6 +263,8 @@ - (id)initWithFrame:(CGRect)frame {
self.graceTime = 0.0f;
self.minShowTime = 0.0f;
self.removeFromSuperViewOnHide = NO;
self.minSize = CGSizeZero;
self.square = NO;

self.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;

Expand Down Expand Up @@ -322,7 +326,7 @@ - (void)layoutSubviews {
// Compute label dimensions based on font metrics if size is larger than max then clip the label width
float lHeight = dims.height;
float lWidth;
if (dims.width <= (frame.size.width - 2 * margin)) {
if (dims.width <= (frame.size.width - 4 * margin)) {
lWidth = dims.width;
}
else {
Expand Down Expand Up @@ -358,17 +362,6 @@ - (void)layoutSubviews {

// Add details label delatils text was set
if (nil != self.detailsLabelText) {
// Get size of label text
dims = [self.detailsLabelText sizeWithFont:self.detailsLabelFont];

// Compute label dimensions based on font metrics if size is larger than max then clip the label width
lHeight = dims.height;
if (dims.width <= (frame.size.width - 2 * margin)) {
lWidth = dims.width;
}
else {
lWidth = frame.size.width - 4 * margin;
}

// Set label properties
detailsLabel.font = self.detailsLabelFont;
Expand All @@ -378,6 +371,12 @@ - (void)layoutSubviews {
detailsLabel.backgroundColor = [UIColor clearColor];
detailsLabel.textColor = [UIColor whiteColor];
detailsLabel.text = self.detailsLabelText;
detailsLabel.numberOfLines = 0;

CGFloat maxHeight = frame.size.height - self.height - 2*margin;
CGSize labelSize = [detailsLabel.text sizeWithFont:detailsLabel.font constrainedToSize:CGSizeMake(frame.size.width - 4*margin, maxHeight) lineBreakMode:detailsLabel.lineBreakMode];
lHeight = labelSize.height;
lWidth = labelSize.width;

// Update HUD size
if (self.width < lWidth) {
Expand All @@ -401,6 +400,23 @@ - (void)layoutSubviews {
[self addSubview:detailsLabel];
}
}

if (square) {
CGFloat max = MAX(self.width, self.height);
if (max <= frame.size.width - 2*margin) {
self.width = max;
}
if (max <= frame.size.height - 2*margin) {
self.height = max;
}
}

if (self.width < minSize.width) {
self.width = minSize.width;
}
if (self.height < minSize.height) {
self.height = minSize.height;
}
}

#pragma mark -
Expand Down Expand Up @@ -626,8 +642,12 @@ - (void)deviceOrientationDidChange:(NSNotification *)notification {
if (!self.superview) {
return;
}

if ([self.superview isKindOfClass:[UIWindow class]]) {
[self setTransformForCurrentOrientation:YES];
} else {
self.bounds = self.superview.bounds;
[self setNeedsDisplay];
}
}

Expand Down

0 comments on commit e324cba

Please sign in to comment.