diff --git a/CSLinearLayoutView/CSLinearLayoutView.h b/CSLinearLayoutView/CSLinearLayoutView.h index b0e160b..6285fb4 100644 --- a/CSLinearLayoutView/CSLinearLayoutView.h +++ b/CSLinearLayoutView/CSLinearLayoutView.h @@ -60,6 +60,42 @@ typedef enum { @end +/* + * Shortcuts, to easily add slightly formatted text & images + */ +@interface CSLinearLayoutView () + +// configuration + +@property (nonatomic, strong) UIColor *textColor; +@property (nonatomic, strong) UIColor *shadowColor; +@property (nonatomic, assign) CGSize shadowOffset; + +@property (nonatomic, strong) UIFont *defaultFont; +@property (nonatomic, strong) UIFont *subtitleFont; +@property (nonatomic, strong) UIFont *titleFont; + +@property (nonatomic, assign) CGFloat defaultMargin; +@property (nonatomic, assign) CGFloat subtitleMargin; +@property (nonatomic, assign) CGFloat titleMargin; + +// shortcuts + +- (CSLinearLayoutItem*)addImageNamed:(NSString*)imageName; + +- (CSLinearLayoutItem*)addText:(NSString*)text; +- (CSLinearLayoutItem*)addText:(NSString*)text font:(UIFont*)font; +- (CSLinearLayoutItem*)addText:(NSString*)text frontMargin:(CGFloat)frontMargin; +- (CSLinearLayoutItem*)addText:(NSString*)text font:(UIFont*)font frontMargin:(CGFloat)frontMargin; + +- (CSLinearLayoutItem*)addTitle:(NSString*)text; +- (CSLinearLayoutItem*)addSubtitle:(NSString*)text; + +// restoring +- (void)restoreDefaultFormats; + +@end + typedef enum { CSLinearLayoutItemFillModeNormal, // Respects the view's frame size @@ -100,4 +136,7 @@ typedef struct { CSLinearLayoutItemPadding CSLinearLayoutMakePadding(CGFloat top, CGFloat left, CGFloat bottom, CGFloat right); -@end \ No newline at end of file +@end + + + diff --git a/CSLinearLayoutView/CSLinearLayoutView.m b/CSLinearLayoutView/CSLinearLayoutView.m index 94dcef1..f113d4d 100644 --- a/CSLinearLayoutView/CSLinearLayoutView.m +++ b/CSLinearLayoutView/CSLinearLayoutView.m @@ -9,20 +9,16 @@ #import "CSLinearLayoutView.h" @interface CSLinearLayoutView() - +@property (nonatomic, strong) NSMutableArray *items; // private setter for items +@property (nonatomic, readonly) CGSize innerFrameSize; // available space for content without insets +@property (nonatomic) BOOL contentsDidChange; // saves, if relayout is needed - (void)setup; - (void)adjustFrameSize; - (void)adjustContentSize; - @end @implementation CSLinearLayoutView -@synthesize items = _items; -@synthesize orientation = _orientation; -@synthesize autoAdjustFrameSize = _autoAdjustFrameSize; -@synthesize autoAdjustContentSize = _autoAdjustContentSize; - #pragma mark - Factories - (id)init { @@ -50,30 +46,57 @@ - (id)initWithCoder:(NSCoder *)aDecoder { } - (void)setup { - _items = [[NSMutableArray alloc] init]; - _orientation = CSLinearLayoutViewOrientationVertical; - _autoAdjustFrameSize = NO; - _autoAdjustContentSize = YES; + self.items = [[NSMutableArray alloc] init]; + self.orientation = CSLinearLayoutViewOrientationVertical; + self.autoAdjustFrameSize = NO; + self.autoAdjustContentSize = YES; self.autoresizesSubviews = NO; + + [self restoreDefaultFormats]; } - -#pragma mark - Lifecycle - -- (void)dealloc { - [_items release], _items = nil; - [super dealloc]; +- (void)restoreDefaultFormats { + // adding shortcut settings + self.textColor = [UIColor blackColor]; + self.shadowColor = [UIColor clearColor]; + self.shadowOffset = CGSizeMake(1, 1); + self.defaultFont = [UIFont systemFontOfSize: 14]; + self.subtitleFont = [UIFont boldSystemFontOfSize: 14]; + self.titleFont = [UIFont boldSystemFontOfSize: 18]; + self.defaultMargin = 6; + self.subtitleMargin = 20; + self.titleMargin = 15; } #pragma mark - Layout +- (void)setFrame:(CGRect)frame; +{ + [super setFrame: frame]; + self.contentsDidChange = YES; +} + +- (void)setNeedsLayout; +{ + [super setNeedsLayout]; + self.contentsDidChange = YES; +} + - (void)layoutSubviews { + [super layoutSubviews]; + + // only relayout if explicitly needed + // (because layout subviews is called on every contentOffset change) + if (!self.contentsDidChange) { + return; + } + self.contentsDidChange = NO; CGFloat relativePosition = 0.0; CGFloat absolutePosition = 0.0; - for (CSLinearLayoutItem *item in _items) { + for (CSLinearLayoutItem *item in self.items) { CGFloat startPadding = 0.0; CGFloat endPadding = 0.0; @@ -86,9 +109,9 @@ - (void)layoutSubviews { if (item.verticalAlignment == CSLinearLayoutItemVerticalAlignmentTop || item.fillMode == CSLinearLayoutItemFillModeStretch) { absolutePosition = item.padding.top; } else if (item.verticalAlignment == CSLinearLayoutItemVerticalAlignmentBottom) { - absolutePosition = self.frame.size.height - item.view.frame.size.height - item.padding.bottom; + absolutePosition = self.innerFrameSize.height - item.view.frame.size.height - item.padding.bottom; } else { // CSLinearLayoutItemVerticalCenter - absolutePosition = (self.frame.size.height / 2) - ((item.view.frame.size.height + (item.padding.bottom - item.padding.top)) / 2); + absolutePosition = (self.innerFrameSize.height / 2) - ((item.view.frame.size.height + (item.padding.bottom - item.padding.top)) / 2); } } else { @@ -99,9 +122,9 @@ - (void)layoutSubviews { if (item.horizontalAlignment == CSLinearLayoutItemHorizontalAlignmentLeft || item.fillMode == CSLinearLayoutItemFillModeStretch) { absolutePosition = item.padding.left; } else if (item.horizontalAlignment == CSLinearLayoutItemHorizontalAlignmentRight) { - absolutePosition = self.frame.size.width - item.view.frame.size.width - item.padding.right; + absolutePosition = self.innerFrameSize.width - item.view.frame.size.width - item.padding.right; } else { // CSLinearLayoutItemHorizontalCenter - absolutePosition = (self.frame.size.width / 2) - ((item.view.frame.size.width + (item.padding.right - item.padding.left)) / 2); + absolutePosition = (self.innerFrameSize.width / 2) - ((item.view.frame.size.width + (item.padding.right - item.padding.left)) / 2); } } @@ -113,7 +136,7 @@ - (void)layoutSubviews { CGFloat height = item.view.frame.size.height; if (item.fillMode == CSLinearLayoutItemFillModeStretch) { - height = self.frame.size.height - (item.padding.top + item.padding.bottom); + height = self.innerFrameSize.height - (item.padding.top + item.padding.bottom); } item.view.frame = CGRectMake(relativePosition, absolutePosition, item.view.frame.size.width, height); @@ -123,7 +146,7 @@ - (void)layoutSubviews { CGFloat width = item.view.frame.size.width; if (item.fillMode == CSLinearLayoutItemFillModeStretch) { - width = self.frame.size.width - (item.padding.left + item.padding.right); + width = self.innerFrameSize.width - (item.padding.left + item.padding.right); } item.view.frame = CGRectMake(absolutePosition, relativePosition, width, item.view.frame.size.height); @@ -135,11 +158,11 @@ - (void)layoutSubviews { } - if (_autoAdjustFrameSize == YES) { + if (self.autoAdjustFrameSize == YES) { [self adjustFrameSize]; } - if (_autoAdjustContentSize == YES) { + if (self.autoAdjustContentSize == YES) { [self adjustContentSize]; } } @@ -154,19 +177,24 @@ - (void)adjustFrameSize { - (void)adjustContentSize { if (self.orientation == CSLinearLayoutViewOrientationHorizontal) { - CGFloat contentWidth = MAX(self.frame.size.width, self.layoutOffset); - self.contentSize = CGSizeMake(contentWidth, self.frame.size.height); + CGFloat contentWidth = MAX(self.innerFrameSize.width, self.layoutOffset); + self.contentSize = CGSizeMake(contentWidth, self.innerFrameSize.height); } else { - CGFloat contentHeight = MAX(self.frame.size.height, self.layoutOffset); - self.contentSize = CGSizeMake(self.frame.size.width, contentHeight); + CGFloat contentHeight = MAX(self.innerFrameSize.height, self.layoutOffset); + self.contentSize = CGSizeMake(self.innerFrameSize.width, contentHeight); } } +- (CGSize)innerFrameSize { + return CGSizeMake(self.frame.size.width - self.contentInset.left - self.contentInset.right, + self.frame.size.height - self.contentInset.top - self.contentInset.bottom); +} + - (CGFloat)layoutOffset { CGFloat currentOffset = 0.0; - for (CSLinearLayoutItem *item in _items) { - if (_orientation == CSLinearLayoutViewOrientationHorizontal) { + for (CSLinearLayoutItem *item in self.items) { + if (self.orientation == CSLinearLayoutViewOrientationHorizontal) { currentOffset += item.padding.left + item.view.frame.size.width + item.padding.right; } else { currentOffset += item.padding.top + item.view.frame.size.height + item.padding.bottom; @@ -184,159 +212,230 @@ - (void)setOrientation:(CSLinearLayoutViewOrientation)anOrientation { - (void)addSubview:(UIView *)view { [super addSubview:view]; - if (_autoAdjustFrameSize == YES) { + if (self.autoAdjustFrameSize == YES) { [self adjustFrameSize]; } - if (_autoAdjustContentSize == YES) { + if (self.autoAdjustContentSize == YES) { [self adjustContentSize]; } + + self.contentsDidChange = YES; } #pragma mark - Add, Remove, Insert, & Move - (void)addItem:(CSLinearLayoutItem *)linearLayoutItem { - if (linearLayoutItem == nil || [_items containsObject:linearLayoutItem] == YES || linearLayoutItem.view == nil) { + if (linearLayoutItem == nil || [self.items containsObject:linearLayoutItem] == YES || linearLayoutItem.view == nil) { return; } - [_items addObject:linearLayoutItem]; + [self.items addObject:linearLayoutItem]; [self addSubview:linearLayoutItem.view]; } - (void)removeItem:(CSLinearLayoutItem *)linearLayoutItem { - if (linearLayoutItem == nil || [_items containsObject:linearLayoutItem] == NO) { + if (linearLayoutItem == nil || [self.items containsObject:linearLayoutItem] == NO) { return; } - [linearLayoutItem retain]; - - [_items removeObject:linearLayoutItem]; [linearLayoutItem.view removeFromSuperview]; - - [linearLayoutItem release]; + [self.items removeObject:linearLayoutItem]; + self.contentsDidChange = YES; } - (void)removeAllItems { - [_items removeAllObjects]; - for (UIView *subview in self.subviews) { - [subview removeFromSuperview]; + // only remove actual items, not scrollbars + for (CSLinearLayoutItem *item in self.items) { + [item.view removeFromSuperview]; } + [self.items removeAllObjects]; + self.contentsDidChange = YES; } - (void)insertItem:(CSLinearLayoutItem *)newItem beforeItem:(CSLinearLayoutItem *)existingItem { - if (newItem == nil || [_items containsObject:newItem] == YES || existingItem == nil || [_items containsObject:existingItem] == NO) { + if (newItem == nil || [self.items containsObject:newItem] == YES || existingItem == nil || [self.items containsObject:existingItem] == NO) { return; } - NSUInteger index = [_items indexOfObject:existingItem]; - [_items insertObject:newItem atIndex:index]; + NSUInteger index = [self.items indexOfObject:existingItem]; + [self.items insertObject:newItem atIndex:index]; [self addSubview:newItem.view]; } - (void)insertItem:(CSLinearLayoutItem *)newItem afterItem:(CSLinearLayoutItem *)existingItem { - if (newItem == nil || [_items containsObject:newItem] == YES || existingItem == nil || [_items containsObject:existingItem] == NO) { + if (newItem == nil || [self.items containsObject:newItem] == YES || existingItem == nil || [self.items containsObject:existingItem] == NO) { return; } - if (existingItem == [_items lastObject]) { - [_items addObject:newItem]; + if (existingItem == [self.items lastObject]) { + [self.items addObject:newItem]; } else { - NSUInteger index = [_items indexOfObject:existingItem]; - [_items insertObject:newItem atIndex:++index]; + NSUInteger index = [self.items indexOfObject:existingItem]; + [self.items insertObject:newItem atIndex:++index]; } [self addSubview:newItem.view]; } - (void)insertItem:(CSLinearLayoutItem *)newItem atIndex:(NSUInteger)index { - if (newItem == nil || [_items containsObject:newItem] == YES || index >= [_items count]) { + if (newItem == nil || [self.items containsObject:newItem] == YES || index >= [self.items count]) { return; } - [_items insertObject:newItem atIndex:index]; + [self.items insertObject:newItem atIndex:index]; [self addSubview:newItem.view]; } - (void)moveItem:(CSLinearLayoutItem *)movingItem beforeItem:(CSLinearLayoutItem *)existingItem { - if (movingItem == nil || [_items containsObject:movingItem] == NO || existingItem == nil || [_items containsObject:existingItem] == NO || movingItem == existingItem) { + if (movingItem == nil || [self.items containsObject:movingItem] == NO || existingItem == nil || [self.items containsObject:existingItem] == NO || movingItem == existingItem) { return; } - [movingItem retain]; - [_items removeObject:movingItem]; + [self.items removeObject:movingItem]; - NSUInteger existingItemIndex = [_items indexOfObject:existingItem]; - [_items insertObject:movingItem atIndex:existingItemIndex]; - [movingItem release]; + NSUInteger existingItemIndex = [self.items indexOfObject:existingItem]; + [self.items insertObject:movingItem atIndex:existingItemIndex]; [self setNeedsLayout]; } - (void)moveItem:(CSLinearLayoutItem *)movingItem afterItem:(CSLinearLayoutItem *)existingItem { - if (movingItem == nil || [_items containsObject:movingItem] == NO || existingItem == nil || [_items containsObject:existingItem] == NO || movingItem == existingItem) { + if (movingItem == nil || [self.items containsObject:movingItem] == NO || existingItem == nil || [self.items containsObject:existingItem] == NO || movingItem == existingItem) { return; } - [movingItem retain]; - [_items removeObject:movingItem]; + [self.items removeObject:movingItem]; - if (existingItem == [_items lastObject]) { - [_items addObject:movingItem]; + if (existingItem == [self.items lastObject]) { + [self.items addObject:movingItem]; } else { - NSUInteger existingItemIndex = [_items indexOfObject:existingItem]; - [_items insertObject:movingItem atIndex:++existingItemIndex]; + NSUInteger existingItemIndex = [self.items indexOfObject:existingItem]; + [self.items insertObject:movingItem atIndex:++existingItemIndex]; } - [movingItem release]; [self setNeedsLayout]; } - (void)moveItem:(CSLinearLayoutItem *)movingItem toIndex:(NSUInteger)index { - if (movingItem == nil || [_items containsObject:movingItem] == NO || index >= [_items count] || [_items indexOfObject:movingItem] == index) { + if (movingItem == nil || [self.items containsObject:movingItem] == NO || index >= [self.items count] || [self.items indexOfObject:movingItem] == index) { return; } - [movingItem retain]; - [_items removeObject:movingItem]; + [self.items removeObject:movingItem]; - if (index == ([_items count] - 1)) { - [_items addObject:movingItem]; + if (index == ([self.items count] - 1)) { + [self.items addObject:movingItem]; } else { - [_items insertObject:movingItem atIndex:index]; + [self.items insertObject:movingItem atIndex:index]; } - [movingItem release]; [self setNeedsLayout]; } - (void)swapItem:(CSLinearLayoutItem *)firstItem withItem:(CSLinearLayoutItem *)secondItem { - if (firstItem == nil || [_items containsObject:firstItem] == NO || secondItem == nil || [_items containsObject:secondItem] == NO || firstItem == secondItem) { + if (firstItem == nil || [self.items containsObject:firstItem] == NO || secondItem == nil || [self.items containsObject:secondItem] == NO || firstItem == secondItem) { return; } - NSUInteger firstItemIndex = [_items indexOfObject:firstItem]; - NSUInteger secondItemIndex = [_items indexOfObject:secondItem]; - [_items exchangeObjectAtIndex:firstItemIndex withObjectAtIndex:secondItemIndex]; + NSUInteger firstItemIndex = [self.items indexOfObject:firstItem]; + NSUInteger secondItemIndex = [self.items indexOfObject:secondItem]; + [self.items exchangeObjectAtIndex:firstItemIndex withObjectAtIndex:secondItemIndex]; [self setNeedsLayout]; } +#pragma mark Content Shortcuts + +- (CSLinearLayoutItem*)addImageNamed:(NSString*)imageName; +{ + UIImage* image = [UIImage imageNamed:imageName]; + UIImageView* imageView = [[UIImageView alloc] initWithImage:image]; + + if (image && imageView) { + // create layout item + CSLinearLayoutItem *item = [CSLinearLayoutItem layoutItemForView:imageView]; + item.horizontalAlignment = CSLinearLayoutItemHorizontalAlignmentCenter; + item.verticalAlignment = CSLinearLayoutItemVerticalAlignmentCenter; + [self addItem:item]; + return item; + } + + return nil; +} + +- (CSLinearLayoutItem*)addText:(NSString*)text; +{ + return [self addText:text font:self.defaultFont]; +} + +- (CSLinearLayoutItem*)addText:(NSString*)text font:(UIFont*)font; +{ + return [self addText:text font:font frontMargin:self.defaultMargin]; +} + +- (CSLinearLayoutItem*)addText:(NSString*)text frontMargin:(CGFloat)frontMargin; +{ + return [self addText:text font:self.defaultFont frontMargin:frontMargin]; +} + +- (CSLinearLayoutItem*)addText:(NSString*)text font:(UIFont*)font frontMargin:(CGFloat)frontMargin; +{ + BOOL isVertical = (self.orientation == CSLinearLayoutViewOrientationVertical); + CGFloat maxSize = self.frame.size.width - self.contentInset.left - self.contentInset.right; + if (!isVertical) { + maxSize = self.frame.size.height - self.contentInset.top - self.contentInset.bottom; + } + + // create label + CGRect frame = CGRectMake(0, 0, isVertical ? maxSize : CGFLOAT_MAX, + !isVertical ? maxSize : CGFLOAT_MAX); + UILabel* label = [[UILabel alloc] initWithFrame:frame]; + label.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin; + if (!isVertical) { + label.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleRightMargin; + } + label.backgroundColor = [UIColor clearColor]; + label.textColor = self.textColor; + label.shadowColor = self.shadowColor; + label.shadowOffset = self.shadowOffset; + label.font = font; + label.text = text; + label.numberOfLines = 0; + label.lineBreakMode = UILineBreakModeWordWrap; + [label sizeToFit]; + + // create layout item + CSLinearLayoutItem *item = [CSLinearLayoutItem layoutItemForView:label]; + item.padding = CSLinearLayoutMakePadding(isVertical ? frontMargin : 0, + !isVertical ? frontMargin : 0, + 0, 0); + item.verticalAlignment = CSLinearLayoutItemVerticalAlignmentCenter; + item.fillMode = CSLinearLayoutItemFillModeStretch; + [self addItem:item]; + + return item; +} + +- (CSLinearLayoutItem*)addTitle:(NSString*)text; +{ + CGFloat margin = (self.items.count > 0) ? self.titleMargin : 0; + return [self addText:text font:self.titleFont frontMargin:margin]; +} + +- (CSLinearLayoutItem*)addSubtitle:(NSString*)text; +{ + CGFloat margin = (self.items.count > 0) ? self.subtitleMargin : 0; + return [self addText:text font:self.subtitleFont frontMargin:margin]; +} + @end #pragma mark - @implementation CSLinearLayoutItem -@synthesize view = _view; -@synthesize fillMode = _fillMode; -@synthesize horizontalAlignment = _horizontalAlignment; -@synthesize verticalAlignment = _verticalAlignment; -@synthesize padding = _padding; -@synthesize tag = _tag; -@synthesize userInfo = _userInfo; - #pragma mark - Factories - (id)init { @@ -361,19 +460,10 @@ - (id)initWithView:(UIView *)aView { } + (CSLinearLayoutItem *)layoutItemForView:(UIView *)aView { - CSLinearLayoutItem *item = [[[CSLinearLayoutItem alloc] initWithView:aView] autorelease]; + CSLinearLayoutItem *item = [[CSLinearLayoutItem alloc] initWithView:aView]; return item; } -#pragma mark - Memory Management - -- (void)dealloc { - self.view = nil; - self.userInfo = nil; - - [super dealloc]; -} - #pragma mark - Helpers @@ -388,3 +478,6 @@ CSLinearLayoutItemPadding CSLinearLayoutMakePadding(CGFloat top, CGFloat left, C } @end + + + diff --git a/Demo/CSLinearLayoutView.xcodeproj/project.pbxproj b/Demo/CSLinearLayoutView.xcodeproj/project.pbxproj index 5dcfa74..fe183f2 100644 --- a/Demo/CSLinearLayoutView.xcodeproj/project.pbxproj +++ b/Demo/CSLinearLayoutView.xcodeproj/project.pbxproj @@ -21,7 +21,7 @@ 15F8EF81151DACD100C6CEBF /* CSAppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 15F8EF7D151DACD100C6CEBF /* CSAppDelegate.m */; }; 15F8EF82151DACD100C6CEBF /* CSMainViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 15F8EF7F151DACD100C6CEBF /* CSMainViewController.m */; }; 15F8EF88151DACE100C6CEBF /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 15F8EF86151DACE100C6CEBF /* main.m */; }; - 15F8EF94151DAE5B00C6CEBF /* CSLinearLayoutView.m in Sources */ = {isa = PBXBuildFile; fileRef = 15F8EF92151DAE5B00C6CEBF /* CSLinearLayoutView.m */; }; + 15F8EF94151DAE5B00C6CEBF /* CSLinearLayoutView.m in Sources */ = {isa = PBXBuildFile; fileRef = 15F8EF92151DAE5B00C6CEBF /* CSLinearLayoutView.m */; settings = {COMPILER_FLAGS = "-fobjc-arc"; }; }; 15F8EFB0151ED34D00C6CEBF /* CSVerticalViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 15F8EFAF151ED34D00C6CEBF /* CSVerticalViewController.m */; }; 15F8EFB9151ED9ED00C6CEBF /* UIColor+RandomColor.m in Sources */ = {isa = PBXBuildFile; fileRef = 15F8EFB8151ED9ED00C6CEBF /* UIColor+RandomColor.m */; }; 15F8EFBC151EDF6D00C6CEBF /* CSHorizontalViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 15F8EFBB151EDF6D00C6CEBF /* CSHorizontalViewController.m */; }; @@ -30,7 +30,9 @@ 15F8EFC4151F911900C6CEBF /* CSAlignmentViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 15F8EFC3151F911900C6CEBF /* CSAlignmentViewController.m */; }; 15F8EFC6151F91D900C6CEBF /* QuartzCore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 15F8EFC5151F91D900C6CEBF /* QuartzCore.framework */; }; 15F8EFC8151F91F100C6CEBF /* CSAlignmentViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 15F8EFC7151F91F100C6CEBF /* CSAlignmentViewController.xib */; }; - E02E749616850224000A8279 /* Default-568h@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = E02E749516850224000A8279 /* Default-568h@2x.png */; }; + D286566D16CAE76800932E46 /* Default-568h@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = D286566B16CAE76800932E46 /* Default-568h@2x.png */; }; + D286566E16CAE76800932E46 /* github-logo@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = D286566C16CAE76800932E46 /* github-logo@2x.png */; }; + D2EE864816CAE56600AC369A /* CSShortcutTestViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = D2EE864716CAE56600AC369A /* CSShortcutTestViewController.m */; settings = {COMPILER_FLAGS = "-fobjc-arc"; }; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ @@ -73,7 +75,10 @@ 15F8EFC3151F911900C6CEBF /* CSAlignmentViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CSAlignmentViewController.m; sourceTree = ""; }; 15F8EFC5151F91D900C6CEBF /* QuartzCore.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = QuartzCore.framework; path = System/Library/Frameworks/QuartzCore.framework; sourceTree = SDKROOT; }; 15F8EFC7151F91F100C6CEBF /* CSAlignmentViewController.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = CSAlignmentViewController.xib; sourceTree = ""; }; - E02E749516850224000A8279 /* Default-568h@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = "Default-568h@2x.png"; path = "../Default-568h@2x.png"; sourceTree = ""; }; + D286566B16CAE76800932E46 /* Default-568h@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "Default-568h@2x.png"; sourceTree = ""; }; + D286566C16CAE76800932E46 /* github-logo@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "github-logo@2x.png"; sourceTree = ""; }; + D2EE864616CAE56600AC369A /* CSShortcutTestViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CSShortcutTestViewController.h; sourceTree = ""; }; + D2EE864716CAE56600AC369A /* CSShortcutTestViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CSShortcutTestViewController.m; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -94,7 +99,8 @@ 15073375154DD47100DA2877 /* Resources */ = { isa = PBXGroup; children = ( - E02E749516850224000A8279 /* Default-568h@2x.png */, + D286566B16CAE76800932E46 /* Default-568h@2x.png */, + D286566C16CAE76800932E46 /* github-logo@2x.png */, 15073377154DD54400DA2877 /* lorem.txt */, ); path = Resources; @@ -194,6 +200,8 @@ 15F8EFC2151F911900C6CEBF /* CSAlignmentViewController.h */, 15F8EFC3151F911900C6CEBF /* CSAlignmentViewController.m */, 15F8EFC7151F91F100C6CEBF /* CSAlignmentViewController.xib */, + D2EE864616CAE56600AC369A /* CSShortcutTestViewController.h */, + D2EE864716CAE56600AC369A /* CSShortcutTestViewController.m */, ); name = Tests; path = Classes/Tests; @@ -269,7 +277,8 @@ 15073378154DD54400DA2877 /* lorem.txt in Resources */, 15F52BED154DDE260084DFB1 /* CSBulletListItemView.xib in Resources */, 15F52BF0154DEED40084DFB1 /* CSBulletListViewController.xib in Resources */, - E02E749616850224000A8279 /* Default-568h@2x.png in Resources */, + D286566D16CAE76800932E46 /* Default-568h@2x.png in Resources */, + D286566E16CAE76800932E46 /* github-logo@2x.png in Resources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -294,6 +303,7 @@ 15F52BE5154DDD3E0084DFB1 /* CSBulletListViewController.m in Sources */, 15F52BE8154DDDA10084DFB1 /* CSBulletListView.m in Sources */, 15F52BEB154DDE000084DFB1 /* CSBulletListItemView.m in Sources */, + D2EE864816CAE56600AC369A /* CSShortcutTestViewController.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/Demo/Classes/CSMainViewController.m b/Demo/Classes/CSMainViewController.m index c6b4908..a624542 100644 --- a/Demo/Classes/CSMainViewController.m +++ b/Demo/Classes/CSMainViewController.m @@ -12,6 +12,7 @@ #import "CSOrientationSwitchViewController.h" #import "CSAlignmentViewController.h" #import "CSBulletListViewController.h" +#import "CSShortcutTestViewController.h" @implementation CSMainViewController @@ -21,6 +22,7 @@ - (id)init { self = [super init]; if (self) { self.title = @"CSLinearLayoutView"; + self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:nil action:nil]; } return self; } @@ -66,9 +68,15 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath break; } + case 5: { + viewController = [[[CSShortcutTestViewController alloc] init] autorelease]; + break; + } + } if (viewController != nil) { + viewController.title = [tableView cellForRowAtIndexPath:indexPath].textLabel.text; [self.navigationController pushViewController:viewController animated:YES]; } @@ -78,7 +86,7 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath #pragma mark - UITableView Data Source Methods - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { - return 5; + return 6; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { @@ -108,8 +116,12 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N cell.textLabel.text = @"Alignment"; break; - case 4: - cell.textLabel.text = @"Sample Bullet List"; + case 4: + cell.textLabel.text = @"Sample Bullet List"; + break; + + case 5: + cell.textLabel.text = @"Adding Shortcuts"; break; } diff --git a/Demo/Classes/Tests/CSShortcutTestViewController.h b/Demo/Classes/Tests/CSShortcutTestViewController.h new file mode 100644 index 0000000..5997120 --- /dev/null +++ b/Demo/Classes/Tests/CSShortcutTestViewController.h @@ -0,0 +1,13 @@ +// +// CSShortcutTestViewController.h +// CSLinearLayoutView +// +// Created by Markus Emrich on 12.02.13. +// Copyright (c) 2013 Charles Scalesse. All rights reserved. +// + +#import + +@interface CSShortcutTestViewController : UIViewController + +@end diff --git a/Demo/Classes/Tests/CSShortcutTestViewController.m b/Demo/Classes/Tests/CSShortcutTestViewController.m new file mode 100644 index 0000000..12ebae2 --- /dev/null +++ b/Demo/Classes/Tests/CSShortcutTestViewController.m @@ -0,0 +1,59 @@ +// +// CSShortcutTestViewController.m +// CSLinearLayoutView +// +// Created by Markus Emrich on 12.02.13. +// Copyright (c) 2013 Charles Scalesse. All rights reserved. +// + +#import "CSLinearLayoutView.h" + +#import "CSShortcutTestViewController.h" + + +@interface CSShortcutTestViewController () +@property (nonatomic, readonly) CSLinearLayoutView* view; +@end + +@implementation CSShortcutTestViewController + + +- (void)loadView; +{ + self.view = [[CSLinearLayoutView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame]; + self.view.contentInset = UIEdgeInsetsMake(20, 20, 20, 20); + self.view.backgroundColor = [UIColor scrollViewTexturedBackgroundColor]; + self.view.textColor = [UIColor whiteColor]; + self.view.shadowColor = [UIColor colorWithWhite:0 alpha:0.3]; + self.view.shadowOffset = CGSizeMake(-1, -1); + self.view.titleMargin = 30; + + [self.view addImageNamed:@"github-logo"]; + [self.view addTitle:self.title]; + [self.view addSubtitle:@"Easily add text and images with only one line of code."]; + [self.view addText:@"The following code is all you need, to create the contents, of this view. The most important methods are addText: and addImageNamed:."]; + [self.view addTitle:@"Code"]; + + self.view.defaultMargin = -8; + [self addTextInCourier:@"CSLinearLayoutView *csView;"]; + [self addTextInCourier:@"csView = [CSLinearLayoutView new];"]; + [self addTextInCourier:@"[csView addImageNamed:@\"github-logo\"];"]; + [self addTextInCourier:@"[csView addTitle:titleText1];"]; + [self addTextInCourier:@"[csView addSubtitle:subtitleText];"]; + [self addTextInCourier:@"[csView addText:descriptionText];"]; + [self addTextInCourier:@"[csView addTitle:titleText2];"]; +} + +- (CSLinearLayoutView*)view; +{ + return (CSLinearLayoutView*)[super view]; +} + +- (void)addTextInCourier:(NSString*)text; +{ + UILabel *label = (UILabel*)[self.view addText:text].view; + label.font = [UIFont fontWithName:@"Courier" size:12]; + [label sizeToFit]; +} + +@end diff --git a/Demo/Default-568h@2x.png b/Demo/Resources/Default-568h@2x.png similarity index 100% rename from Demo/Default-568h@2x.png rename to Demo/Resources/Default-568h@2x.png diff --git a/Demo/Resources/github-logo@2x.png b/Demo/Resources/github-logo@2x.png new file mode 100644 index 0000000..8c475aa Binary files /dev/null and b/Demo/Resources/github-logo@2x.png differ