BBALayout.h 3.78 KB
//
//  BBALayout.h
//
//  Created by xmfeng on 14-10-14.
//  Copyright (c) 2014年 xmfeng. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@interface BBALayout : NSObject

@end

#pragma mark - 基于iPhone6Plus(414x736)标注
#define SX(v) ([[BBALayout layout_414x736] screenX:(v)])
#define SY(v) ([[BBALayout layout_414x736] screenY:(v)])
#define SV(v) ([[BBALayout layout_414x736] screen:(v)])
#define SFont(v) ([UIFont systemFontOfSize:SV(v)])

@interface BBALayout (Mark)

+ (instancetype)layout_414x736;  // iPhone6Plus竖屏,此时.screenWidth/.screenHeight等只考虑竖屏情况
+ (instancetype)layout_736x414;  // iPhone6Plus横屏,此时.screenWidth/.screenHeight等只考虑横屏情况
+ (instancetype)layout_414x736_736x414;  // iPhone6Plus竖屏+横屏,此时.screenWidth/.screenHeight会根据屏幕状况进行切换

@property(nonatomic, assign, readonly) CGFloat screenWidth;
@property(nonatomic, assign, readonly) CGFloat screenHeight;

@property(nonatomic, assign, readonly) CGFloat screenScale;
@property(nonatomic, assign, readonly) CGFloat screenScaleX;
@property(nonatomic, assign, readonly) CGFloat screenScaleY;

- (CGFloat)screen:(CGFloat)v;
- (CGFloat)screenX:(CGFloat)x;
- (CGFloat)screenY:(CGFloat)y;

@end

#pragma mark utils

#define BBARectWidth(width) CGRectMake(0.0f, 0.0f, (width), 0.0f)
#define BBARectHeight(height) CGRectMake(0.0f, 0.0f, 0.0f, (height))
#define BBARectSize(width, height) CGRectMake(0.0f, 0.0f, (width), (height))
#define BBARectCenter(rect) CGPointMake(CGRectGetMidX(rect), CGRectGetMidY(rect))

#pragma mark BBALayoutAlignment

#define BBALayoutAlignmentNone    0.0f
#define BBALayoutAlignmentLeft    0.0f
#define BBALayoutAlignmentRight   1.0f
#define BBALayoutAlignmentTop     0.0f
#define BBALayoutAlignmentBottom  1.0f
#define BBALayoutAlignmentCenter  0.5f
#define BBALayoutAlignmentMake(value) (value)

typedef CGFloat BBALayoutAlignment;

#pragma mark BBALayout

/*
 *  simple UIView
 *    @list for subviews
 *    @frame for frame, but can only edit origin with-no-rotation
 */
@protocol BBALayoutUnion

@optional
- (CGRect)frame;
- (void)setFrame:(CGRect)frame;
- (UIView *)superview;

@end

/*
 * BBALayout
 *   layout [UIView or layouted-UIView] horizontally or vertically
 *     - NSNumber           :indicate static offset
 *     - space              :indicate <-->
 *     - UIView
 *     - layouted-UIView    :returned-value of layout
 *
 * > only-layout, no @addSubview called, you should do outside;
 * > Donot-Support-TransformScaled-Subview
 */
@interface BBALayout (LayoutSubviews)

// utils
// union[view.frame-viewed-by-viewer]
+ (CGRect)convertFrame:(NSArray *)views toView:(UIView *)viewer;

// consts
+ (id<BBALayoutUnion>)space;

// layout-horizontally for @viewer with a preffered min @frame and @align vertically
+ (id<BBALayoutUnion>)horizontal:(NSArray *)list
                              on:(UIView *)viewer
                           frame:(CGRect)frame
                       alignment:(BBALayoutAlignment)align;

// layout-vertically for @viewer with a preffered min @frame and @align horizontally
+ (id<BBALayoutUnion>)vertical:(NSArray *)list
                            on:(UIView *)viewer
                         frame:(CGRect)frame
                     alignment:(BBALayoutAlignment)align;

// layout-utils
+ (id<BBALayoutUnion>)horizontal:(NSArray *)list;
+ (id<BBALayoutUnion>)horizontal:(NSArray *)list width:(CGFloat)width;
+ (id<BBALayoutUnion>)vertical:(NSArray *)list;
+ (id<BBALayoutUnion>)vertical:(NSArray *)list height:(CGFloat)height;
+ (id<BBALayoutUnion>)ZOrder:(NSArray *)list;

// layout-view
+ (UIView *)horizontalView:(NSArray *)list;
+ (UIView *)horizontalView:(NSArray *)list width:(CGFloat)width;
+ (UIView *)verticalView:(NSArray *)list;
+ (UIView *)verticalView:(NSArray *)list height:(CGFloat)height;

@end