From af0bde80ba2d1f7b1c5b3ce610e3cc50e1654f53 Mon Sep 17 00:00:00 2001 From: "Lvv.me" Date: Tue, 1 Jun 2021 06:54:23 +0800 Subject: [PATCH] add shorthand for MASLayoutGuide --- Masonry.xcodeproj/project.pbxproj | 4 ++ Masonry/LayoutGuide+MASAdditions.h | 2 +- Masonry/LayoutGuide+MASAdditions.m | 2 +- Masonry/LayoutGuide+MASShorthandAdditions.h | 74 +++++++++++++++++++++ Masonry/MASUtilities.h | 10 +++ Masonry/Masonry.h | 1 + Masonry/View+MASAdditions.h | 22 +++--- Masonry/View+MASShorthandAdditions.h | 50 ++++++-------- 8 files changed, 122 insertions(+), 43 deletions(-) create mode 100644 Masonry/LayoutGuide+MASShorthandAdditions.h diff --git a/Masonry.xcodeproj/project.pbxproj b/Masonry.xcodeproj/project.pbxproj index 937667af..04074021 100644 --- a/Masonry.xcodeproj/project.pbxproj +++ b/Masonry.xcodeproj/project.pbxproj @@ -7,6 +7,7 @@ objects = { /* Begin PBXBuildFile section */ + 1539A4A42665991900892F49 /* LayoutGuide+MASShorthandAdditions.h in Headers */ = {isa = PBXBuildFile; fileRef = 1539A4A22665991900892F49 /* LayoutGuide+MASShorthandAdditions.h */; settings = {ATTRIBUTES = (Public, ); }; }; 15E414912663780600D3D1B6 /* LayoutGuide+MASAdditions.h in Headers */ = {isa = PBXBuildFile; fileRef = 15E4148F2663780600D3D1B6 /* LayoutGuide+MASAdditions.h */; settings = {ATTRIBUTES = (Public, ); }; }; 15E414922663780600D3D1B6 /* LayoutGuide+MASAdditions.m in Sources */ = {isa = PBXBuildFile; fileRef = 15E414902663780600D3D1B6 /* LayoutGuide+MASAdditions.m */; }; 3AED05BD1AD59FD40053CC65 /* Masonry.h in Headers */ = {isa = PBXBuildFile; fileRef = 3AED05BC1AD59FD40053CC65 /* Masonry.h */; settings = {ATTRIBUTES = (Public, ); }; }; @@ -37,6 +38,7 @@ /* End PBXBuildFile section */ /* Begin PBXFileReference section */ + 1539A4A22665991900892F49 /* LayoutGuide+MASShorthandAdditions.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "LayoutGuide+MASShorthandAdditions.h"; sourceTree = ""; }; 15E4148F2663780600D3D1B6 /* LayoutGuide+MASAdditions.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "LayoutGuide+MASAdditions.h"; sourceTree = ""; }; 15E414902663780600D3D1B6 /* LayoutGuide+MASAdditions.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "LayoutGuide+MASAdditions.m"; sourceTree = ""; }; 3AED05B71AD59FD40053CC65 /* Masonry.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Masonry.framework; sourceTree = BUILT_PRODUCTS_DIR; }; @@ -107,6 +109,7 @@ 4473548C1B39F772004DACCB /* ViewController+MASAdditions.m */, 15E4148F2663780600D3D1B6 /* LayoutGuide+MASAdditions.h */, 15E414902663780600D3D1B6 /* LayoutGuide+MASAdditions.m */, + 1539A4A22665991900892F49 /* LayoutGuide+MASShorthandAdditions.h */, 3AED05EA1AD5A0470053CC65 /* NSArray+MASAdditions.h */, 3AED05EB1AD5A0470053CC65 /* NSArray+MASAdditions.m */, 3AED05EC1AD5A0470053CC65 /* NSArray+MASShorthandAdditions.h */, @@ -149,6 +152,7 @@ 3AED06071AD5A0470053CC65 /* View+MASShorthandAdditions.h in Headers */, 4473548D1B39F772004DACCB /* ViewController+MASAdditions.h in Headers */, 15E414912663780600D3D1B6 /* LayoutGuide+MASAdditions.h in Headers */, + 1539A4A42665991900892F49 /* LayoutGuide+MASShorthandAdditions.h in Headers */, 3AED05FC1AD5A0470053CC65 /* MASViewAttribute.h in Headers */, 3AED05BD1AD59FD40053CC65 /* Masonry.h in Headers */, 3AED05F91AD5A0470053CC65 /* MASLayoutConstraint.h in Headers */, diff --git a/Masonry/LayoutGuide+MASAdditions.h b/Masonry/LayoutGuide+MASAdditions.h index 1613bc1d..1c20b105 100644 --- a/Masonry/LayoutGuide+MASAdditions.h +++ b/Masonry/LayoutGuide+MASAdditions.h @@ -1,5 +1,5 @@ // -// NSLayoutGuide+MASAdditions.h +// LayoutGuide+MASAdditions.h // Masonry // // Created by v on 2021/5/30. diff --git a/Masonry/LayoutGuide+MASAdditions.m b/Masonry/LayoutGuide+MASAdditions.m index eb6533f2..3be94bb4 100644 --- a/Masonry/LayoutGuide+MASAdditions.m +++ b/Masonry/LayoutGuide+MASAdditions.m @@ -1,5 +1,5 @@ // -// NSLayoutGuide+MASAdditions.m +// LayoutGuide+MASAdditions.m // Masonry // // Created by v on 2021/5/30. diff --git a/Masonry/LayoutGuide+MASShorthandAdditions.h b/Masonry/LayoutGuide+MASShorthandAdditions.h new file mode 100644 index 00000000..31546836 --- /dev/null +++ b/Masonry/LayoutGuide+MASShorthandAdditions.h @@ -0,0 +1,74 @@ +// +// LayoutGuide+MASShorthandAdditions.h +// Masonry +// +// Created by v on 2021/6/1. +// Copyright © 2021 Jonas Budelmann. All rights reserved. +// + +#import "LayoutGuide+MASAdditions.h" + +NS_ASSUME_NONNULL_BEGIN + +#ifdef MAS_SHORTHAND + +/** + * Shorthand view additions without the 'mas_' prefixes, + * only enabled if MAS_SHORTHAND is defined + */ +API_AVAILABLE(macos(10.11), ios(9.0)) +@interface MASLayoutGuide (MASShorthandAdditions) + +@property (nonatomic, strong, readonly) MASViewAttribute *left; +@property (nonatomic, strong, readonly) MASViewAttribute *top; +@property (nonatomic, strong, readonly) MASViewAttribute *right; +@property (nonatomic, strong, readonly) MASViewAttribute *bottom; +@property (nonatomic, strong, readonly) MASViewAttribute *leading; +@property (nonatomic, strong, readonly) MASViewAttribute *trailing; +@property (nonatomic, strong, readonly) MASViewAttribute *width; +@property (nonatomic, strong, readonly) MASViewAttribute *height; +@property (nonatomic, strong, readonly) MASViewAttribute *centerX; +@property (nonatomic, strong, readonly) MASViewAttribute *centerY; + +@property (nonatomic, strong, readonly) MASViewAttribute *(^attribute)(NSLayoutAttribute attr); + +- (NSArray *)makeConstraints:(void(^)(MASConstraintMaker *make))block; +- (NSArray *)updateConstraints:(void(^)(MASConstraintMaker *make))block; +- (NSArray *)remakeConstraints:(void(^)(MASConstraintMaker *make))block; + +@end + +@implementation MASLayoutGuide (MASShorthandAdditions) + +MAS_ATTR_FORWARD(top); +MAS_ATTR_FORWARD(left); +MAS_ATTR_FORWARD(bottom); +MAS_ATTR_FORWARD(right); +MAS_ATTR_FORWARD(leading); +MAS_ATTR_FORWARD(trailing); +MAS_ATTR_FORWARD(width); +MAS_ATTR_FORWARD(height); +MAS_ATTR_FORWARD(centerX); +MAS_ATTR_FORWARD(centerY); + +- (MASViewAttribute *(^)(NSLayoutAttribute))attribute { + return [self mas_attribute]; +} + +- (NSArray *)makeConstraints:(void(NS_NOESCAPE ^)(MASConstraintMaker *))block { + return [self mas_makeConstraints:block]; +} + +- (NSArray *)updateConstraints:(void(NS_NOESCAPE ^)(MASConstraintMaker *))block { + return [self mas_updateConstraints:block]; +} + +- (NSArray *)remakeConstraints:(void(NS_NOESCAPE ^)(MASConstraintMaker *))block { + return [self mas_remakeConstraints:block]; +} + +@end + +#endif + +NS_ASSUME_NONNULL_END diff --git a/Masonry/MASUtilities.h b/Masonry/MASUtilities.h index 7d3f472e..393f76f8 100644 --- a/Masonry/MASUtilities.h +++ b/Masonry/MASUtilities.h @@ -136,3 +136,13 @@ static inline id _MASBoxValue(const char *type, ...) { } #define MASBoxValue(value) _MASBoxValue(@encode(__typeof__((value))), (value)) + +#define MAS_ATTR_FORWARD(attr) \ +- (MASViewAttribute *)attr { \ + return [self mas_##attr]; \ +} + +#define MAS_ATTR_FORWARD_AVAILABLE(attr, available) \ +- (MASViewAttribute *)attr available { \ + return [self mas_##attr]; \ +} diff --git a/Masonry/Masonry.h b/Masonry/Masonry.h index f37b1edb..b85dae49 100644 --- a/Masonry/Masonry.h +++ b/Masonry/Masonry.h @@ -19,6 +19,7 @@ FOUNDATION_EXPORT const unsigned char MasonryVersionString[]; #import "View+MASShorthandAdditions.h" #import "ViewController+MASAdditions.h" #import "LayoutGuide+MASAdditions.h" +#import "LayoutGuide+MASShorthandAdditions.h" #import "NSArray+MASAdditions.h" #import "NSArray+MASShorthandAdditions.h" #import "MASConstraint.h" diff --git a/Masonry/View+MASAdditions.h b/Masonry/View+MASAdditions.h index 00477c1c..8ab2b751 100644 --- a/Masonry/View+MASAdditions.h +++ b/Masonry/View+MASAdditions.h @@ -46,17 +46,17 @@ @property (nonatomic, strong, readonly) MASViewAttribute *mas_centerXWithinMargins; @property (nonatomic, strong, readonly) MASViewAttribute *mas_centerYWithinMargins; -@property (nonatomic, strong, readonly) MASViewAttribute *mas_safeAreaLayoutGuide NS_AVAILABLE_IOS(11.0); -@property (nonatomic, strong, readonly) MASViewAttribute *mas_safeAreaLayoutGuideLeading NS_AVAILABLE_IOS(11.0); -@property (nonatomic, strong, readonly) MASViewAttribute *mas_safeAreaLayoutGuideTrailing NS_AVAILABLE_IOS(11.0); -@property (nonatomic, strong, readonly) MASViewAttribute *mas_safeAreaLayoutGuideLeft NS_AVAILABLE_IOS(11.0); -@property (nonatomic, strong, readonly) MASViewAttribute *mas_safeAreaLayoutGuideRight NS_AVAILABLE_IOS(11.0); -@property (nonatomic, strong, readonly) MASViewAttribute *mas_safeAreaLayoutGuideTop NS_AVAILABLE_IOS(11.0); -@property (nonatomic, strong, readonly) MASViewAttribute *mas_safeAreaLayoutGuideBottom NS_AVAILABLE_IOS(11.0); -@property (nonatomic, strong, readonly) MASViewAttribute *mas_safeAreaLayoutGuideWidth NS_AVAILABLE_IOS(11.0); -@property (nonatomic, strong, readonly) MASViewAttribute *mas_safeAreaLayoutGuideHeight NS_AVAILABLE_IOS(11.0); -@property (nonatomic, strong, readonly) MASViewAttribute *mas_safeAreaLayoutGuideCenterX NS_AVAILABLE_IOS(11.0); -@property (nonatomic, strong, readonly) MASViewAttribute *mas_safeAreaLayoutGuideCenterY NS_AVAILABLE_IOS(11.0); +@property (nonatomic, strong, readonly) MASViewAttribute *mas_safeAreaLayoutGuide API_AVAILABLE(ios(11.0)); +@property (nonatomic, strong, readonly) MASViewAttribute *mas_safeAreaLayoutGuideLeading API_AVAILABLE(ios(11.0)); +@property (nonatomic, strong, readonly) MASViewAttribute *mas_safeAreaLayoutGuideTrailing API_AVAILABLE(ios(11.0)); +@property (nonatomic, strong, readonly) MASViewAttribute *mas_safeAreaLayoutGuideLeft API_AVAILABLE(ios(11.0)); +@property (nonatomic, strong, readonly) MASViewAttribute *mas_safeAreaLayoutGuideRight API_AVAILABLE(ios(11.0)); +@property (nonatomic, strong, readonly) MASViewAttribute *mas_safeAreaLayoutGuideTop API_AVAILABLE(ios(11.0)); +@property (nonatomic, strong, readonly) MASViewAttribute *mas_safeAreaLayoutGuideBottom API_AVAILABLE(ios(11.0)); +@property (nonatomic, strong, readonly) MASViewAttribute *mas_safeAreaLayoutGuideWidth API_AVAILABLE(ios(11.0)); +@property (nonatomic, strong, readonly) MASViewAttribute *mas_safeAreaLayoutGuideHeight API_AVAILABLE(ios(11.0)); +@property (nonatomic, strong, readonly) MASViewAttribute *mas_safeAreaLayoutGuideCenterX API_AVAILABLE(ios(11.0)); +@property (nonatomic, strong, readonly) MASViewAttribute *mas_safeAreaLayoutGuideCenterY API_AVAILABLE(ios(11.0)); #endif diff --git a/Masonry/View+MASShorthandAdditions.h b/Masonry/View+MASShorthandAdditions.h index 8e375ee3..71cdbf43 100644 --- a/Masonry/View+MASShorthandAdditions.h +++ b/Masonry/View+MASShorthandAdditions.h @@ -47,16 +47,16 @@ #if TARGET_OS_IPHONE || TARGET_OS_TV -@property (nonatomic, strong, readonly) MASViewAttribute *safeAreaLayoutGuideLeading NS_AVAILABLE_IOS(11.0); -@property (nonatomic, strong, readonly) MASViewAttribute *safeAreaLayoutGuideTrailing NS_AVAILABLE_IOS(11.0); -@property (nonatomic, strong, readonly) MASViewAttribute *safeAreaLayoutGuideLeft NS_AVAILABLE_IOS(11.0); -@property (nonatomic, strong, readonly) MASViewAttribute *safeAreaLayoutGuideRight NS_AVAILABLE_IOS(11.0); -@property (nonatomic, strong, readonly) MASViewAttribute *safeAreaLayoutGuideTop NS_AVAILABLE_IOS(11.0); -@property (nonatomic, strong, readonly) MASViewAttribute *safeAreaLayoutGuideBottom NS_AVAILABLE_IOS(11.0); -@property (nonatomic, strong, readonly) MASViewAttribute *safeAreaLayoutGuideWidth NS_AVAILABLE_IOS(11.0); -@property (nonatomic, strong, readonly) MASViewAttribute *safeAreaLayoutGuideHeight NS_AVAILABLE_IOS(11.0); -@property (nonatomic, strong, readonly) MASViewAttribute *safeAreaLayoutGuideCenterX NS_AVAILABLE_IOS(11.0); -@property (nonatomic, strong, readonly) MASViewAttribute *safeAreaLayoutGuideCenterY NS_AVAILABLE_IOS(11.0); +@property (nonatomic, strong, readonly) MASViewAttribute *safeAreaLayoutGuideLeading API_AVAILABLE(ios(11.0)); +@property (nonatomic, strong, readonly) MASViewAttribute *safeAreaLayoutGuideTrailing API_AVAILABLE(ios(11.0)); +@property (nonatomic, strong, readonly) MASViewAttribute *safeAreaLayoutGuideLeft API_AVAILABLE(ios(11.0)); +@property (nonatomic, strong, readonly) MASViewAttribute *safeAreaLayoutGuideRight API_AVAILABLE(ios(11.0)); +@property (nonatomic, strong, readonly) MASViewAttribute *safeAreaLayoutGuideTop API_AVAILABLE(ios(11.0)); +@property (nonatomic, strong, readonly) MASViewAttribute *safeAreaLayoutGuideBottom API_AVAILABLE(ios(11.0)); +@property (nonatomic, strong, readonly) MASViewAttribute *safeAreaLayoutGuideWidth API_AVAILABLE(ios(11.0)); +@property (nonatomic, strong, readonly) MASViewAttribute *safeAreaLayoutGuideHeight API_AVAILABLE(ios(11.0)); +@property (nonatomic, strong, readonly) MASViewAttribute *safeAreaLayoutGuideCenterX API_AVAILABLE(ios(11.0)); +@property (nonatomic, strong, readonly) MASViewAttribute *safeAreaLayoutGuideCenterY API_AVAILABLE(ios(11.0)); #endif @@ -66,16 +66,6 @@ @end -#define MAS_ATTR_FORWARD(attr) \ -- (MASViewAttribute *)attr { \ - return [self mas_##attr]; \ -} - -#define MAS_ATTR_FORWARD_AVAILABLE(attr, available) \ -- (MASViewAttribute *)attr available { \ - return [self mas_##attr]; \ -} - @implementation MAS_VIEW (MASShorthandAdditions) MAS_ATTR_FORWARD(top); @@ -104,16 +94,16 @@ MAS_ATTR_FORWARD(trailingMargin); MAS_ATTR_FORWARD(centerXWithinMargins); MAS_ATTR_FORWARD(centerYWithinMargins); -MAS_ATTR_FORWARD_AVAILABLE(safeAreaLayoutGuideLeading, NS_AVAILABLE_IOS(11.0)); -MAS_ATTR_FORWARD_AVAILABLE(safeAreaLayoutGuideTrailing, NS_AVAILABLE_IOS(11.0)); -MAS_ATTR_FORWARD_AVAILABLE(safeAreaLayoutGuideLeft, NS_AVAILABLE_IOS(11.0)); -MAS_ATTR_FORWARD_AVAILABLE(safeAreaLayoutGuideRight, NS_AVAILABLE_IOS(11.0)); -MAS_ATTR_FORWARD_AVAILABLE(safeAreaLayoutGuideTop, NS_AVAILABLE_IOS(11.0)); -MAS_ATTR_FORWARD_AVAILABLE(safeAreaLayoutGuideBottom, NS_AVAILABLE_IOS(11.0)); -MAS_ATTR_FORWARD_AVAILABLE(safeAreaLayoutGuideWidth, NS_AVAILABLE_IOS(11.0)); -MAS_ATTR_FORWARD_AVAILABLE(safeAreaLayoutGuideHeight, NS_AVAILABLE_IOS(11.0)); -MAS_ATTR_FORWARD_AVAILABLE(safeAreaLayoutGuideCenterX, NS_AVAILABLE_IOS(11.0)); -MAS_ATTR_FORWARD_AVAILABLE(safeAreaLayoutGuideCenterY, NS_AVAILABLE_IOS(11.0)); +MAS_ATTR_FORWARD_AVAILABLE(safeAreaLayoutGuideLeading, API_AVAILABLE(ios(11.0))); +MAS_ATTR_FORWARD_AVAILABLE(safeAreaLayoutGuideTrailing, API_AVAILABLE(ios(11.0))); +MAS_ATTR_FORWARD_AVAILABLE(safeAreaLayoutGuideLeft, API_AVAILABLE(ios(11.0))); +MAS_ATTR_FORWARD_AVAILABLE(safeAreaLayoutGuideRight, API_AVAILABLE(ios(11.0))); +MAS_ATTR_FORWARD_AVAILABLE(safeAreaLayoutGuideTop, API_AVAILABLE(ios(11.0))); +MAS_ATTR_FORWARD_AVAILABLE(safeAreaLayoutGuideBottom, API_AVAILABLE(ios(11.0))); +MAS_ATTR_FORWARD_AVAILABLE(safeAreaLayoutGuideWidth, API_AVAILABLE(ios(11.0))); +MAS_ATTR_FORWARD_AVAILABLE(safeAreaLayoutGuideHeight, API_AVAILABLE(ios(11.0))); +MAS_ATTR_FORWARD_AVAILABLE(safeAreaLayoutGuideCenterX, API_AVAILABLE(ios(11.0))); +MAS_ATTR_FORWARD_AVAILABLE(safeAreaLayoutGuideCenterY, API_AVAILABLE(ios(11.0))); #endif