Permalink
Browse files

Added OHStackView

  • Loading branch information...
0 parents commit d0fe4d36e81df70db66d9c146eef2c39abfe7b4e @AliSoftware committed Jan 16, 2011
@@ -0,0 +1,2 @@
+*.pbxproj -crlf -diff -merge
+
@@ -0,0 +1,14 @@
+# xcode noise
+build/
+*~.nib/
+*.pbxuser
+*.perspective
+*.perspectivev3
+*.mode1v3
+*.mode2v3
+
+# old school
+.svn
+
+# osx noise
+.DS_Store
35 LICENSE
@@ -0,0 +1,35 @@
+Without any further information, all the sources provided here are under the MIT License
+quoted below.
+
+Anyway, please contact me by email (olivier.halligon+ae@gmail.com) if you plan to use my work and the provided classes
+in your own software. Thanks.
+
+
+/***********************************************************************************
+ *
+ * Copyright (c) 2010 Olivier Halligon
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ ***********************************************************************************
+ *
+ * Any comment or suggestion welcome. Referencing this project in your AboutBox is appreciated.
+ * Please tell me if you use this class so we can cross-reference our projects.
+ *
+ ***********************************************************************************/
@@ -0,0 +1,71 @@
+/***********************************************************************************
+ *
+ * Copyright (c) 2010 Olivier Halligon
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ ***********************************************************************************
+ *
+ * Any comment or suggestion welcome. Referencing this project in your AboutBox is appreciated.
+ * Please tell me if you use this class so we can cross-reference our projects.
+ *
+ ***********************************************************************************/
+
+#import <UIKit/UIKit.h>
+
+typedef enum {
+ OHStackAlignmentNone = 0, // Don't align
+
+ OHStackAlignmentStackFirstEdges, // Stack views on top (vertical) / left (horizontal)
+ OHStackAlignmentStackLastEdges, // Stack edges on bottom (vertical) / right (horizontal)
+
+ OHStackAlignmentAlignFirstEdges, // Align top (vertical) / left (horizontal) edges
+ OHStackAlignmentAlignCenters, // Align centers
+ OHStackAlignmentAlignLastEdges, // Align bottom (vertical) / right (horizontal) edges
+
+
+ // more understandable aliases for vertical modes
+ OHStackAlignmentStackFromTop = OHStackAlignmentStackFirstEdges,
+ OHStackAlignmentStackFromBottom = OHStackAlignmentStackLastEdges,
+ OHStackAlignmentAlignTopEdges = OHStackAlignmentAlignFirstEdges,
+ OHStackAlignmentAlignBottomEdges = OHStackAlignmentAlignLastEdges,
+
+ // more understandable aliases for horizontal modes
+ OHStackAlignmentStackFromLeft = OHStackAlignmentStackFirstEdges,
+ OHStackAlignmentStackFromRight = OHStackAlignmentStackLastEdges,
+ OHStackAlignmentAlignLeftEdges = OHStackAlignmentAlignFirstEdges,
+ OHStackAlignmentAlignRightEdges = OHStackAlignmentAlignLastEdges,
+} OHStackAlignment;
+
+
+/////////////////////////////////////////////////////////////////////////////
+// MARK: -
+/////////////////////////////////////////////////////////////////////////////
+
+@interface OHStackView : UIView {
+ OHStackAlignment horizontalAlignment;
+ OHStackAlignment verticalAlignment;
+ CGSize margins;
+ CGSize spacing;
+}
+@property(nonatomic,assign) OHStackAlignment horizontalAlignment;
+@property(nonatomic,assign) OHStackAlignment verticalAlignment;
+@property(nonatomic,assign) CGSize spacing;
+@property(nonatomic,assign) CGSize margins;
+@end
@@ -0,0 +1,127 @@
+//
+// StackView.m
+//
+// Created by Olivier on 20/11/09.
+// Copyright 2009 AliSoftware. All rights reserved.
+//
+
+#import "OHStackView.h"
+
+@interface OHStackView()
+-(CGSize)computeForSize:(CGSize)size apply:(BOOL)apply;
+@end
+
+/////////////////////////////////////////////////////////////////////////////
+
+@implementation OHStackView
+@synthesize horizontalAlignment;
+@synthesize verticalAlignment;
+@synthesize spacing;
+@synthesize margins;
+
+
+/////////////////////////////////////////////////////////////////////////////
+// MARK: -
+// MARK: Setters
+/////////////////////////////////////////////////////////////////////////////
+
+-(void)setHorizontalAlignment:(OHStackAlignment)v
+{
+ horizontalAlignment = v;
+ [self setNeedsLayout];
+}
+-(void)setVerticalAlignment:(OHStackAlignment)v
+{
+ verticalAlignment = v;
+ [self setNeedsLayout];
+}
+-(void)setSpacing:(CGSize)v
+{
+ spacing = v;
+ [self setNeedsLayout];
+}
+-(void)setMargins:(CGSize)v
+{
+ margins = v;
+ [self setNeedsLayout];
+}
+
+/////////////////////////////////////////////////////////////////////////////
+// MARK: -
+// MARK: Overriden methods
+/////////////////////////////////////////////////////////////////////////////
+
+-(void)layoutSubviews
+{
+ (void)[self computeForSize:self.bounds.size apply:YES];
+ [super layoutSubviews];
+}
+-(CGSize)sizeThatFits:(CGSize)size
+{
+ return [self computeForSize:size apply:NO];
+}
+
+-(CGSize)computeForSize:(CGSize)size apply:(BOOL)apply
+{
+ CGSize cumul = margins;
+ for(UIView* v in self.subviews)
+ {
+ CGRect f = v.frame;
+ switch (horizontalAlignment) {
+ case OHStackAlignmentStackFirstEdges:
+ f.origin.x = cumul.width;
+ cumul.width += f.size.width + spacing.width;
+ break;
+ case OHStackAlignmentStackLastEdges:
+ f.origin.x = (self.bounds.size.width-cumul.width) - f.size.width;
+ cumul.width += f.size.width + spacing.width;
+ break;
+ case OHStackAlignmentAlignFirstEdges:
+ f.origin.x = margins.width;
+ cumul.width = MAX(cumul.width , margins.width + f.size.width + spacing.width);
+ break;
+ case OHStackAlignmentAlignCenters:
+ f.origin.x = (self.bounds.size.width-f.size.width)/2;
+ cumul.width = MAX(cumul.width , margins.width + f.size.width + spacing.width);
+ break;
+ case OHStackAlignmentAlignLastEdges:
+ f.origin.x = self.bounds.size.width-f.size.width-margins.width;
+ cumul.width = MAX(cumul.width , margins.width + f.size.width + spacing.width);
+ break;
+ default: // OHStackAlignmentNone
+ cumul.width = MAX(cumul.width , margins.width + f.origin.x+f.size.width + spacing.width);
+ break;
+ }
+ switch (verticalAlignment) {
+ case OHStackAlignmentStackFirstEdges:
+ f.origin.y = cumul.height;
+ cumul.height += f.size.height + spacing.height;
+ break;
+ case OHStackAlignmentStackLastEdges:
+ f.origin.y = (self.bounds.size.height-cumul.height) - f.size.height;
+ cumul.height += f.size.height + spacing.height;
+ break;
+ case OHStackAlignmentAlignFirstEdges:
+ f.origin.y = margins.height;
+ cumul.height = MAX(cumul.height , margins.height + f.size.height + spacing.height);
+ break;
+ case OHStackAlignmentAlignCenters:
+ f.origin.y = (self.bounds.size.height-f.size.height)/2;
+ cumul.height = MAX(cumul.height , margins.height + f.size.height + spacing.height);
+ break;
+ case OHStackAlignmentAlignLastEdges:
+ f.origin.y = self.bounds.size.height-f.size.height-margins.height;
+ cumul.height = MAX(cumul.height , margins.height + f.size.height + spacing.height);
+ break;
+ default: // OHStackAlignmentNone
+ cumul.height = MAX(cumul.height , margins.height + f.origin.y+f.size.height + spacing.height);
+ break;
+ }
+ if (apply) v.frame = f;
+ }
+ cumul.width -= spacing.width;
+ cumul.height -= spacing.height;
+ return CGSizeMake(cumul.width+margins.width, cumul.height+margins.height);
+}
+
+@end
4 README
@@ -0,0 +1,4 @@
+This class allows you to stack its subviews, and automatically layout its subviews.
+It update the position of every of its subviews automagically as soon as you modify the size of one of its subview.
+
+See the "StackView Example" project for a basic usage example
@@ -0,0 +1,15 @@
+//
+// AppDelegate.h
+// StackView Example
+//
+// Created by Olivier on 16/01/11.
+// Copyright 2011 AliSoftware. All rights reserved.
+//
+
+#import <UIKit/UIKit.h>
+#import "OHStackView.h"
+
+@interface AppDelegate : NSObject <UIApplicationDelegate> {
+ IBOutlet OHStackView* verticalLayout;
+}
+@end
@@ -0,0 +1,29 @@
+//
+// AppDelegate.m
+// StackView Example
+//
+// Created by Olivier on 16/01/11.
+// Copyright 2011 AliSoftware. All rights reserved.
+//
+
+#import "AppDelegate.h"
+
+
+@implementation AppDelegate
+
+-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
+ verticalLayout.spacing = CGSizeMake(5,5);
+ verticalLayout.horizontalAlignment = OHStackAlignmentAlignLeftEdges;
+ verticalLayout.verticalAlignment = OHStackAlignmentStackFromTop;
+
+ for(OHStackView* horizontalLayout in verticalLayout.subviews) {
+ horizontalLayout.spacing = CGSizeMake(5,5);
+ horizontalLayout.horizontalAlignment = OHStackAlignmentStackFromLeft;
+ horizontalLayout.verticalAlignment = OHStackAlignmentAlignCenters;
+ }
+
+ return YES;
+}
+
+@end
+
@@ -0,0 +1,16 @@
+//
+// TapToZoomView.h
+// StackView Example
+//
+// Created by Olivier on 16/01/11.
+// Copyright 2011 AliSoftware. All rights reserved.
+//
+
+#import <UIKit/UIKit.h>
+
+
+@interface TapZoomLabel : UILabel {
+ BOOL zoomed;
+}
+
+@end
@@ -0,0 +1,37 @@
+//
+// TapToZoomView.m
+// StackView Example
+//
+// Created by Olivier on 16/01/11.
+// Copyright 2011 AliSoftware. All rights reserved.
+//
+
+#import "TapZoomLabel.h"
+
+
+@implementation TapZoomLabel
+
+- (id) initWithCoder:(NSCoder*)aDecoder
+{
+ self = [super initWithCoder:aDecoder];
+ if (self != nil) {
+ self.userInteractionEnabled = YES;
+ }
+ return self;
+}
+
+-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
+ CGRect r = self.frame;
+ if (zoomed) {
+ r.size.width /= 2;
+ r.size.height /= 2;
+ } else {
+ r.size.width *= 2;
+ r.size.height *= 2;
+ }
+ self.frame = r;
+ zoomed = !zoomed;
+ [self.superview sizeToFit];
+}
+
+@end
Oops, something went wrong.

0 comments on commit d0fe4d3

Please sign in to comment.