mirror of https://github.com/oxen-io/session-ios
				
				
				
			
			You cannot select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
	
	
		
			543 lines
		
	
	
		
			15 KiB
		
	
	
	
		
			Matlab
		
	
		
		
			
		
	
	
			543 lines
		
	
	
		
			15 KiB
		
	
	
	
		
			Matlab
		
	
| 
											9 years ago
										 | // | ||
| 
											7 years ago
										 | //  Copyright (c) 2019 Open Whisper Systems. All rights reserved. | ||
| 
											9 years ago
										 | // | ||
|  | 
 | ||
| 
											7 years ago
										 | #import "UIView+OWS.h" | ||
| 
											7 years ago
										 | #import "OWSMath.h" | ||
| 
											5 years ago
										 | 
 | ||
| 
											5 years ago
										 | #import <SessionUtilitiesKit/AppContext.h> | ||
| 
											9 years ago
										 | 
 | ||
| 
											8 years ago
										 | NS_ASSUME_NONNULL_BEGIN | ||
|  | 
 | ||
| 
											9 years ago
										 | static inline CGFloat ScreenShortDimension() | ||
|  | { | ||
|  |     return MIN([UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height); | ||
|  | } | ||
|  | 
 | ||
|  | static const CGFloat kIPhone5ScreenWidth = 320.f; | ||
|  | static const CGFloat kIPhone7PlusScreenWidth = 414.f; | ||
|  | 
 | ||
|  | CGFloat ScaleFromIPhone5To7Plus(CGFloat iPhone5Value, CGFloat iPhone7PlusValue) | ||
|  | { | ||
|  |     CGFloat screenShortDimension = ScreenShortDimension(); | ||
| 
											7 years ago
										 |     return (CGFloat)round(CGFloatLerp(iPhone5Value, | ||
| 
											9 years ago
										 |         iPhone7PlusValue, | ||
| 
											7 years ago
										 |         CGFloatClamp01(CGFloatInverseLerp(screenShortDimension, kIPhone5ScreenWidth, kIPhone7PlusScreenWidth)))); | ||
| 
											9 years ago
										 | } | ||
|  | 
 | ||
|  | CGFloat ScaleFromIPhone5(CGFloat iPhone5Value) | ||
|  | { | ||
|  |     CGFloat screenShortDimension = ScreenShortDimension(); | ||
| 
											7 years ago
										 |     return (CGFloat)round(iPhone5Value * screenShortDimension / kIPhone5ScreenWidth); | ||
| 
											9 years ago
										 | } | ||
|  | 
 | ||
|  | #pragma mark - | ||
|  | 
 | ||
|  | @implementation UIView (OWS) | ||
|  | 
 | ||
| 
											8 years ago
										 | - (NSArray<NSLayoutConstraint *> *)autoPinWidthToSuperviewWithMargin:(CGFloat)margin | ||
| 
											9 years ago
										 | { | ||
| 
											8 years ago
										 |     NSArray<NSLayoutConstraint *> *result = @[ | ||
| 
											8 years ago
										 |         [self autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:margin], | ||
|  |         [self autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:margin], | ||
| 
											8 years ago
										 |     ]; | ||
|  |     return result; | ||
| 
											9 years ago
										 | } | ||
|  | 
 | ||
| 
											7 years ago
										 | - (NSArray<NSLayoutConstraint *> *)autoPinWidthToSuperviewMargins | ||
|  | { | ||
|  |     NSArray<NSLayoutConstraint *> *result = @[ | ||
|  |         [self autoPinEdgeToSuperviewMargin:ALEdgeLeading], | ||
|  |         [self autoPinEdgeToSuperviewMargin:ALEdgeTrailing], | ||
|  |     ]; | ||
|  |     return result; | ||
|  | } | ||
|  | 
 | ||
| 
											8 years ago
										 | - (NSArray<NSLayoutConstraint *> *)autoPinWidthToSuperview | ||
| 
											9 years ago
										 | { | ||
| 
											8 years ago
										 |     NSArray<NSLayoutConstraint *> *result = @[ | ||
| 
											8 years ago
										 |         [self autoPinEdgeToSuperviewEdge:ALEdgeLeft], | ||
|  |         [self autoPinEdgeToSuperviewEdge:ALEdgeRight], | ||
| 
											8 years ago
										 |     ]; | ||
|  |     return result; | ||
| 
											9 years ago
										 | } | ||
|  | 
 | ||
| 
											8 years ago
										 | - (NSArray<NSLayoutConstraint *> *)autoPinLeadingAndTrailingToSuperviewMargin | ||
| 
											8 years ago
										 | { | ||
| 
											8 years ago
										 |     NSArray<NSLayoutConstraint *> *result = @[ | ||
| 
											8 years ago
										 |         [self autoPinLeadingToSuperviewMargin], | ||
|  |         [self autoPinTrailingToSuperviewMargin], | ||
| 
											8 years ago
										 |     ]; | ||
|  |     return result; | ||
| 
											8 years ago
										 | } | ||
|  | 
 | ||
| 
											8 years ago
										 | - (NSArray<NSLayoutConstraint *> *)autoPinHeightToSuperviewWithMargin:(CGFloat)margin | ||
| 
											9 years ago
										 | { | ||
| 
											8 years ago
										 |     NSArray<NSLayoutConstraint *> *result = @[ | ||
| 
											8 years ago
										 |         [self autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:margin], | ||
|  |         [self autoPinEdgeToSuperviewEdge:ALEdgeBottom withInset:margin], | ||
| 
											8 years ago
										 |     ]; | ||
|  |     return result; | ||
| 
											9 years ago
										 | } | ||
|  | 
 | ||
| 
											8 years ago
										 | - (NSArray<NSLayoutConstraint *> *)autoPinHeightToSuperview | ||
| 
											9 years ago
										 | { | ||
| 
											8 years ago
										 |     NSArray<NSLayoutConstraint *> *result = @[ | ||
| 
											8 years ago
										 |         [self autoPinEdgeToSuperviewEdge:ALEdgeTop], | ||
|  |         [self autoPinEdgeToSuperviewEdge:ALEdgeBottom], | ||
| 
											8 years ago
										 |     ]; | ||
|  |     return result; | ||
| 
											9 years ago
										 | } | ||
|  | 
 | ||
| 
											7 years ago
										 | - (NSArray<NSLayoutConstraint *> *)ows_autoPinToSuperviewEdges | ||
| 
											8 years ago
										 | { | ||
| 
											8 years ago
										 |     NSArray<NSLayoutConstraint *> *result = @[ | ||
| 
											8 years ago
										 |         [self autoPinEdgeToSuperviewEdge:ALEdgeLeft], | ||
|  |         [self autoPinEdgeToSuperviewEdge:ALEdgeRight], | ||
|  |         [self autoPinEdgeToSuperviewEdge:ALEdgeTop], | ||
|  |         [self autoPinEdgeToSuperviewEdge:ALEdgeBottom], | ||
| 
											8 years ago
										 |     ]; | ||
|  |     return result; | ||
| 
											7 years ago
										 | } | ||
|  | 
 | ||
| 
											7 years ago
										 | - (NSArray<NSLayoutConstraint *> *)ows_autoPinToSuperviewMargins | ||
| 
											7 years ago
										 | { | ||
|  |     NSArray<NSLayoutConstraint *> *result = @[ | ||
|  |         [self autoPinTopToSuperviewMargin], | ||
|  |         [self autoPinLeadingToSuperviewMargin], | ||
|  |         [self autoPinTrailingToSuperviewMargin], | ||
|  |         [self autoPinBottomToSuperviewMargin], | ||
|  |     ]; | ||
|  |     return result; | ||
| 
											8 years ago
										 | } | ||
|  | 
 | ||
| 
											8 years ago
										 | - (NSLayoutConstraint *)autoHCenterInSuperview | ||
| 
											9 years ago
										 | { | ||
| 
											8 years ago
										 |     return [self autoAlignAxis:ALAxisVertical toSameAxisOfView:self.superview]; | ||
| 
											9 years ago
										 | } | ||
|  | 
 | ||
| 
											8 years ago
										 | - (NSLayoutConstraint *)autoVCenterInSuperview | ||
| 
											9 years ago
										 | { | ||
| 
											8 years ago
										 |     return [self autoAlignAxis:ALAxisHorizontal toSameAxisOfView:self.superview]; | ||
| 
											9 years ago
										 | } | ||
|  | 
 | ||
| 
											9 years ago
										 | - (void)autoPinWidthToWidthOfView:(UIView *)view | ||
|  | { | ||
|  |     [self autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:view]; | ||
|  |     [self autoPinEdge:ALEdgeRight toEdge:ALEdgeRight ofView:view]; | ||
|  | } | ||
|  | 
 | ||
|  | - (void)autoPinHeightToHeightOfView:(UIView *)view | ||
|  | { | ||
|  |     [self autoPinEdge:ALEdgeTop toEdge:ALEdgeTop ofView:view]; | ||
|  |     [self autoPinEdge:ALEdgeBottom toEdge:ALEdgeBottom ofView:view]; | ||
|  | } | ||
|  | 
 | ||
|  | - (NSLayoutConstraint *)autoPinToSquareAspectRatio | ||
| 
											8 years ago
										 | { | ||
|  |     return [self autoPinToAspectRatio:1.0]; | ||
|  | } | ||
|  | 
 | ||
| 
											7 years ago
										 | - (NSLayoutConstraint *)autoPinToAspectRatioWithSize:(CGSize)size { | ||
|  |     return [self autoPinToAspectRatio:size.width / size.height]; | ||
|  | } | ||
|  | 
 | ||
| 
											8 years ago
										 | - (NSLayoutConstraint *)autoPinToAspectRatio:(CGFloat)ratio | ||
| 
											7 years ago
										 | { | ||
|  |     return [self autoPinToAspectRatio:ratio relation:NSLayoutRelationEqual]; | ||
|  | } | ||
|  | 
 | ||
|  | - (NSLayoutConstraint *)autoPinToAspectRatio:(CGFloat)ratio relation:(NSLayoutRelation)relation | ||
| 
											9 years ago
										 | { | ||
| 
											8 years ago
										 |     // Clamp to ensure view has reasonable aspect ratio. | ||
| 
											7 years ago
										 |     CGFloat clampedRatio = CGFloatClamp(ratio, 0.05f, 95.0f); | ||
| 
											9 years ago
										 | 
 | ||
| 
											8 years ago
										 |     self.translatesAutoresizingMaskIntoConstraints = NO; | ||
| 
											9 years ago
										 |     NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:self | ||
|  |                                                                   attribute:NSLayoutAttributeWidth | ||
| 
											7 years ago
										 |                                                                   relatedBy:relation | ||
| 
											9 years ago
										 |                                                                      toItem:self | ||
|  |                                                                   attribute:NSLayoutAttributeHeight | ||
| 
											8 years ago
										 |                                                                  multiplier:clampedRatio | ||
| 
											9 years ago
										 |                                                                    constant:0.f]; | ||
|  |     [constraint autoInstall]; | ||
|  |     return constraint; | ||
|  | } | ||
|  | 
 | ||
| 
											9 years ago
										 | #pragma mark - Content Hugging and Compression Resistance | ||
|  | 
 | ||
|  | - (void)setContentHuggingLow | ||
|  | { | ||
|  |     [self setContentHuggingHorizontalLow]; | ||
|  |     [self setContentHuggingVerticalLow]; | ||
|  | } | ||
|  | 
 | ||
|  | - (void)setContentHuggingHigh | ||
|  | { | ||
|  |     [self setContentHuggingHorizontalHigh]; | ||
|  |     [self setContentHuggingVerticalHigh]; | ||
|  | } | ||
|  | 
 | ||
|  | - (void)setContentHuggingHorizontalLow | ||
|  | { | ||
|  |     [self setContentHuggingPriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisHorizontal]; | ||
|  | } | ||
|  | 
 | ||
|  | - (void)setContentHuggingHorizontalHigh | ||
|  | { | ||
|  |     [self setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal]; | ||
|  | } | ||
|  | 
 | ||
|  | - (void)setContentHuggingVerticalLow | ||
|  | { | ||
|  |     [self setContentHuggingPriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisVertical]; | ||
|  | } | ||
|  | 
 | ||
|  | - (void)setContentHuggingVerticalHigh | ||
|  | { | ||
|  |     [self setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical]; | ||
|  | } | ||
|  | 
 | ||
|  | - (void)setCompressionResistanceLow | ||
|  | { | ||
|  |     [self setCompressionResistanceHorizontalLow]; | ||
|  |     [self setCompressionResistanceVerticalLow]; | ||
|  | } | ||
|  | 
 | ||
|  | - (void)setCompressionResistanceHigh | ||
|  | { | ||
|  |     [self setCompressionResistanceHorizontalHigh]; | ||
|  |     [self setCompressionResistanceVerticalHigh]; | ||
|  | } | ||
|  | 
 | ||
|  | - (void)setCompressionResistanceHorizontalLow | ||
|  | { | ||
|  |     [self setContentCompressionResistancePriority:0 forAxis:UILayoutConstraintAxisHorizontal]; | ||
|  | } | ||
|  | 
 | ||
|  | - (void)setCompressionResistanceHorizontalHigh | ||
|  | { | ||
|  |     [self setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal]; | ||
|  | } | ||
|  | 
 | ||
|  | - (void)setCompressionResistanceVerticalLow | ||
|  | { | ||
|  |     [self setContentCompressionResistancePriority:0 forAxis:UILayoutConstraintAxisVertical]; | ||
|  | } | ||
|  | 
 | ||
|  | - (void)setCompressionResistanceVerticalHigh | ||
|  | { | ||
|  |     [self setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical]; | ||
|  | } | ||
|  | 
 | ||
| 
											9 years ago
										 | #pragma mark - Manual Layout | ||
|  | 
 | ||
|  | - (CGFloat)left | ||
|  | { | ||
|  |     return self.frame.origin.x; | ||
|  | } | ||
|  | 
 | ||
|  | - (CGFloat)right | ||
|  | { | ||
|  |     return self.frame.origin.x + self.frame.size.width; | ||
|  | } | ||
|  | 
 | ||
|  | - (CGFloat)top | ||
|  | { | ||
|  |     return self.frame.origin.y; | ||
|  | } | ||
|  | 
 | ||
|  | - (CGFloat)bottom | ||
|  | { | ||
| 
											9 years ago
										 |     return self.frame.origin.y + self.frame.size.height; | ||
| 
											9 years ago
										 | } | ||
|  | 
 | ||
|  | - (CGFloat)width | ||
|  | { | ||
|  |     return self.frame.size.width; | ||
|  | } | ||
|  | 
 | ||
|  | - (CGFloat)height | ||
|  | { | ||
|  |     return self.frame.size.height; | ||
|  | } | ||
|  | 
 | ||
|  | - (void)centerOnSuperview | ||
|  | { | ||
| 
											7 years ago
										 |     CGFloat x = (CGFloat)round((self.superview.width - self.width) * 0.5f); | ||
|  |     CGFloat y = (CGFloat)round((self.superview.height - self.height) * 0.5f); | ||
|  |     self.frame = CGRectMake(x, y, self.width, self.height); | ||
| 
											9 years ago
										 | } | ||
|  | 
 | ||
| 
											8 years ago
										 | #pragma mark - RTL | ||
|  | 
 | ||
| 
											8 years ago
										 | - (NSLayoutConstraint *)autoPinLeadingToSuperviewMargin | ||
| 
											8 years ago
										 | { | ||
| 
											8 years ago
										 |     return [self autoPinLeadingToSuperviewMarginWithInset:0]; | ||
| 
											8 years ago
										 | } | ||
|  | 
 | ||
| 
											8 years ago
										 | - (NSLayoutConstraint *)autoPinLeadingToSuperviewMarginWithInset:(CGFloat)inset | ||
| 
											8 years ago
										 | { | ||
| 
											8 years ago
										 |     self.translatesAutoresizingMaskIntoConstraints = NO; | ||
|  | 
 | ||
| 
											8 years ago
										 |     NSLayoutConstraint *constraint = | ||
| 
											8 years ago
										 |         [self.leadingAnchor constraintEqualToAnchor:self.superview.layoutMarginsGuide.leadingAnchor constant:inset]; | ||
| 
											8 years ago
										 |     constraint.active = YES; | ||
|  |     return constraint; | ||
| 
											8 years ago
										 | } | ||
|  | 
 | ||
| 
											8 years ago
										 | - (NSLayoutConstraint *)autoPinTrailingToSuperviewMargin | ||
| 
											8 years ago
										 | { | ||
| 
											8 years ago
										 |     return [self autoPinTrailingToSuperviewMarginWithInset:0]; | ||
| 
											8 years ago
										 | } | ||
|  | 
 | ||
| 
											8 years ago
										 | - (NSLayoutConstraint *)autoPinTrailingToSuperviewMarginWithInset:(CGFloat)inset | ||
| 
											8 years ago
										 | { | ||
| 
											8 years ago
										 |     self.translatesAutoresizingMaskIntoConstraints = NO; | ||
|  | 
 | ||
| 
											8 years ago
										 |     NSLayoutConstraint *constraint = | ||
| 
											8 years ago
										 |         [self.trailingAnchor constraintEqualToAnchor:self.superview.layoutMarginsGuide.trailingAnchor constant:-inset]; | ||
| 
											8 years ago
										 |     constraint.active = YES; | ||
|  |     return constraint; | ||
| 
											8 years ago
										 | } | ||
|  | 
 | ||
| 
											8 years ago
										 | - (NSLayoutConstraint *)autoPinBottomToSuperviewMargin | ||
| 
											8 years ago
										 | { | ||
| 
											8 years ago
										 |     return [self autoPinBottomToSuperviewMarginWithInset:0.f]; | ||
| 
											8 years ago
										 | } | ||
|  | 
 | ||
| 
											8 years ago
										 | - (NSLayoutConstraint *)autoPinBottomToSuperviewMarginWithInset:(CGFloat)inset | ||
| 
											8 years ago
										 | { | ||
| 
											8 years ago
										 |     self.translatesAutoresizingMaskIntoConstraints = NO; | ||
|  | 
 | ||
| 
											8 years ago
										 |     NSLayoutConstraint *constraint = | ||
| 
											8 years ago
										 |         [self.bottomAnchor constraintEqualToAnchor:self.superview.layoutMarginsGuide.bottomAnchor constant:-inset]; | ||
| 
											8 years ago
										 |     constraint.active = YES; | ||
|  |     return constraint; | ||
| 
											8 years ago
										 | } | ||
|  | 
 | ||
| 
											8 years ago
										 | - (NSLayoutConstraint *)autoPinTopToSuperviewMargin | ||
| 
											8 years ago
										 | { | ||
| 
											8 years ago
										 |     return [self autoPinTopToSuperviewMarginWithInset:0.f]; | ||
| 
											8 years ago
										 | } | ||
|  | 
 | ||
| 
											8 years ago
										 | - (NSLayoutConstraint *)autoPinTopToSuperviewMarginWithInset:(CGFloat)inset | ||
| 
											8 years ago
										 | { | ||
| 
											8 years ago
										 |     self.translatesAutoresizingMaskIntoConstraints = NO; | ||
|  | 
 | ||
| 
											8 years ago
										 |     NSLayoutConstraint *constraint = | ||
| 
											8 years ago
										 |         [self.topAnchor constraintEqualToAnchor:self.superview.layoutMarginsGuide.topAnchor constant:inset]; | ||
| 
											8 years ago
										 |     constraint.active = YES; | ||
|  |     return constraint; | ||
| 
											8 years ago
										 | } | ||
|  | 
 | ||
| 
											8 years ago
										 | - (NSLayoutConstraint *)autoPinLeadingToTrailingEdgeOfView:(UIView *)view | ||
| 
											8 years ago
										 | { | ||
| 
											8 years ago
										 |     return [self autoPinLeadingToTrailingEdgeOfView:view offset:0]; | ||
| 
											8 years ago
										 | } | ||
|  | 
 | ||
| 
											8 years ago
										 | - (NSLayoutConstraint *)autoPinLeadingToTrailingEdgeOfView:(UIView *)view offset:(CGFloat)offset | ||
| 
											8 years ago
										 | { | ||
| 
											8 years ago
										 |     self.translatesAutoresizingMaskIntoConstraints = NO; | ||
|  | 
 | ||
| 
											8 years ago
										 |     NSLayoutConstraint *constraint = [self.leadingAnchor constraintEqualToAnchor:view.trailingAnchor constant:offset]; | ||
| 
											8 years ago
										 |     constraint.active = YES; | ||
|  |     return constraint; | ||
| 
											8 years ago
										 | } | ||
|  | 
 | ||
| 
											8 years ago
										 | - (NSLayoutConstraint *)autoPinTrailingToLeadingEdgeOfView:(UIView *)view | ||
| 
											8 years ago
										 | { | ||
| 
											8 years ago
										 |     return [self autoPinTrailingToLeadingEdgeOfView:view offset:0]; | ||
| 
											8 years ago
										 | } | ||
|  | 
 | ||
| 
											8 years ago
										 | - (NSLayoutConstraint *)autoPinTrailingToLeadingEdgeOfView:(UIView *)view offset:(CGFloat)offset | ||
| 
											8 years ago
										 | { | ||
| 
											8 years ago
										 |     self.translatesAutoresizingMaskIntoConstraints = NO; | ||
|  | 
 | ||
| 
											8 years ago
										 |     NSLayoutConstraint *constraint = [self.trailingAnchor constraintEqualToAnchor:view.leadingAnchor constant:-offset]; | ||
| 
											8 years ago
										 |     constraint.active = YES; | ||
|  |     return constraint; | ||
| 
											8 years ago
										 | } | ||
|  | 
 | ||
| 
											8 years ago
										 | - (NSLayoutConstraint *)autoPinLeadingToEdgeOfView:(UIView *)view | ||
| 
											8 years ago
										 | { | ||
| 
											8 years ago
										 |     return [self autoPinLeadingToEdgeOfView:view offset:0]; | ||
| 
											8 years ago
										 | } | ||
|  | 
 | ||
| 
											8 years ago
										 | - (NSLayoutConstraint *)autoPinLeadingToEdgeOfView:(UIView *)view offset:(CGFloat)offset | ||
| 
											8 years ago
										 | { | ||
| 
											8 years ago
										 |     self.translatesAutoresizingMaskIntoConstraints = NO; | ||
|  | 
 | ||
| 
											8 years ago
										 |     NSLayoutConstraint *constraint = [self.leadingAnchor constraintEqualToAnchor:view.leadingAnchor constant:offset]; | ||
| 
											8 years ago
										 |     constraint.active = YES; | ||
|  |     return constraint; | ||
| 
											8 years ago
										 | } | ||
|  | 
 | ||
| 
											8 years ago
										 | - (NSLayoutConstraint *)autoPinTrailingToEdgeOfView:(UIView *)view | ||
| 
											8 years ago
										 | { | ||
| 
											8 years ago
										 |     return [self autoPinTrailingToEdgeOfView:view offset:0]; | ||
| 
											8 years ago
										 | } | ||
|  | 
 | ||
| 
											8 years ago
										 | - (NSLayoutConstraint *)autoPinTrailingToEdgeOfView:(UIView *)view offset:(CGFloat)margin | ||
| 
											8 years ago
										 | { | ||
| 
											8 years ago
										 |     self.translatesAutoresizingMaskIntoConstraints = NO; | ||
|  | 
 | ||
| 
											8 years ago
										 |     NSLayoutConstraint *constraint = [self.trailingAnchor constraintEqualToAnchor:view.trailingAnchor constant:margin]; | ||
|  |     constraint.active = YES; | ||
|  |     return constraint; | ||
| 
											8 years ago
										 | } | ||
|  | 
 | ||
|  | - (NSTextAlignment)textAlignmentUnnatural | ||
|  | { | ||
| 
											7 years ago
										 |     return (CurrentAppContext().isRTL ? NSTextAlignmentLeft : NSTextAlignmentRight); | ||
| 
											8 years ago
										 | } | ||
|  | 
 | ||
| 
											8 years ago
										 | - (void)setHLayoutMargins:(CGFloat)value | ||
|  | { | ||
|  |     UIEdgeInsets layoutMargins = self.layoutMargins; | ||
|  |     layoutMargins.left = value; | ||
|  |     layoutMargins.right = value; | ||
|  |     self.layoutMargins = layoutMargins; | ||
|  | } | ||
|  | 
 | ||
| 
											7 years ago
										 | - (NSArray<NSLayoutConstraint *> *)autoPinToEdgesOfView:(UIView *)view | ||
|  | { | ||
|  |     return @[ | ||
|  |         [self autoPinEdge:ALEdgeTop toEdge:ALEdgeTop ofView:view], | ||
|  |         [self autoPinEdge:ALEdgeBottom toEdge:ALEdgeBottom ofView:view], | ||
|  |         [self autoPinEdge:ALEdgeLeading toEdge:ALEdgeLeading ofView:view], | ||
|  |         [self autoPinEdge:ALEdgeTrailing toEdge:ALEdgeTrailing ofView:view], | ||
|  |     ]; | ||
|  | } | ||
|  | 
 | ||
| 
											8 years ago
										 | #pragma mark - Containers | ||
|  | 
 | ||
| 
											8 years ago
										 | + (UIView *)containerView | ||
|  | { | ||
|  |     UIView *view = [UIView new]; | ||
|  |     // Leading and trailing anchors honor layout margins. | ||
|  |     // When using a UIView as a "div" to structure layout, we don't want it to have margins. | ||
| 
											8 years ago
										 |     view.layoutMargins = UIEdgeInsetsZero; | ||
| 
											8 years ago
										 |     return view; | ||
|  | } | ||
|  | 
 | ||
| 
											8 years ago
										 | + (UIView *)verticalStackWithSubviews:(NSArray<UIView *> *)subviews spacing:(int)spacing | ||
| 
											8 years ago
										 | { | ||
| 
											8 years ago
										 |     UIView *container = [UIView containerView]; | ||
|  |     UIView *_Nullable lastSubview = nil; | ||
|  |     for (UIView *subview in subviews) { | ||
|  |         [container addSubview:subview]; | ||
|  |         [subview autoPinWidthToSuperview]; | ||
|  |         if (lastSubview) { | ||
|  |             [subview autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:lastSubview withOffset:spacing]; | ||
|  |         } else { | ||
|  |             [subview autoPinEdgeToSuperviewEdge:ALEdgeTop]; | ||
|  |         } | ||
|  |         lastSubview = subview; | ||
|  |     } | ||
|  |     [lastSubview autoPinEdgeToSuperviewEdge:ALEdgeBottom]; | ||
|  |     return container; | ||
| 
											8 years ago
										 | } | ||
|  | 
 | ||
| 
											9 years ago
										 | #pragma mark - Debugging | ||
|  | 
 | ||
|  | - (void)addBorderWithColor:(UIColor *)color | ||
|  | { | ||
|  |     self.layer.borderColor = color.CGColor; | ||
|  |     self.layer.borderWidth = 1; | ||
|  | } | ||
|  | 
 | ||
|  | - (void)addRedBorder | ||
|  | { | ||
|  |     [self addBorderWithColor:[UIColor redColor]]; | ||
|  | } | ||
|  | 
 | ||
| 
											9 years ago
										 | - (void)addRedBorderRecursively | ||
|  | { | ||
|  |     [self addRedBorder]; | ||
|  |     for (UIView *subview in self.subviews) { | ||
|  |         [subview addRedBorderRecursively]; | ||
|  |     } | ||
|  | } | ||
|  | 
 | ||
| 
											7 years ago
										 | - (void)traverseViewHierarchyWithVisitor:(UIViewVisitorBlock)visitor | ||
|  | { | ||
|  |     visitor(self); | ||
|  | 
 | ||
|  |     for (UIView *subview in self.subviews) { | ||
|  |         [subview traverseViewHierarchyWithVisitor:visitor]; | ||
|  |     } | ||
|  | } | ||
|  | 
 | ||
| 
											9 years ago
										 | @end | ||
| 
											8 years ago
										 | 
 | ||
| 
											8 years ago
										 | #pragma mark - | ||
|  | 
 | ||
|  | @implementation UIScrollView (OWS) | ||
|  | 
 | ||
| 
											7 years ago
										 | - (BOOL)applyScrollViewInsetsFix | ||
| 
											8 years ago
										 | { | ||
| 
											7 years ago
										 |     return NO; | ||
| 
											8 years ago
										 | } | ||
|  | 
 | ||
|  | @end | ||
|  | 
 | ||
| 
											8 years ago
										 | #pragma mark - | ||
|  | 
 | ||
|  | @implementation UIStackView (OWS) | ||
|  | 
 | ||
| 
											7 years ago
										 | - (UIView *)addBackgroundViewWithBackgroundColor:(UIColor *)backgroundColor | ||
| 
											8 years ago
										 | { | ||
|  |     UIView *subview = [UIView new]; | ||
|  |     subview.backgroundColor = backgroundColor; | ||
|  |     [self addSubview:subview]; | ||
|  |     [subview autoPinEdgesToSuperviewEdges]; | ||
| 
											7 years ago
										 |     [subview setCompressionResistanceLow]; | ||
|  |     [subview setContentHuggingLow]; | ||
| 
											7 years ago
										 |     [self sendSubviewToBack:subview]; | ||
|  |     return subview; | ||
| 
											8 years ago
										 | } | ||
|  | 
 | ||
| 
											7 years ago
										 | - (UIView *)addBorderViewWithColor:(UIColor *)color strokeWidth:(CGFloat)strokeWidth cornerRadius:(CGFloat)cornerRadius | ||
|  | { | ||
|  |     UIView *borderView = [UIView new]; | ||
|  |     borderView.userInteractionEnabled = NO; | ||
| 
											7 years ago
										 |     borderView.backgroundColor = UIColor.clearColor; | ||
|  |     borderView.opaque = NO; | ||
| 
											7 years ago
										 |     borderView.layer.borderColor = color.CGColor; | ||
|  |     borderView.layer.borderWidth = strokeWidth; | ||
|  |     borderView.layer.cornerRadius = cornerRadius; | ||
|  |     [self addSubview:borderView]; | ||
|  |     [borderView autoPinEdgesToSuperviewEdges]; | ||
|  |     [borderView setCompressionResistanceLow]; | ||
|  |     [borderView setContentHuggingLow]; | ||
|  |     return borderView; | ||
|  | } | ||
|  | 
 | ||
| 
											8 years ago
										 | @end | ||
|  | 
 | ||
| 
											7 years ago
										 | #pragma mark - | ||
|  | 
 | ||
|  | @implementation UIAlertAction (OWS) | ||
|  | 
 | ||
|  | + (instancetype)actionWithTitle:(nullable NSString *)title | ||
|  |         accessibilityIdentifier:(nullable NSString *)accessibilityIdentifier | ||
|  |                           style:(UIAlertActionStyle)style | ||
|  |                         handler:(void (^__nullable)(UIAlertAction *action))handler | ||
|  | { | ||
|  |     UIAlertAction *action = [UIAlertAction actionWithTitle:title style:style handler:handler]; | ||
|  |     action.accessibilityIdentifier = accessibilityIdentifier; | ||
|  |     return action; | ||
|  | } | ||
|  | 
 | ||
|  | @end | ||
|  | 
 | ||
|  | #pragma mark - | ||
|  | 
 | ||
| 
											7 years ago
										 | CGFloat CGHairlineWidth() | ||
|  | { | ||
|  |     return 1.f / UIScreen.mainScreen.scale; | ||
|  | } | ||
|  | 
 | ||
| 
											8 years ago
										 | NS_ASSUME_NONNULL_END |