Commit 0ee7a620 authored by Aral Balkan's avatar Aral Balkan

Add left-aligned aspect fit image

parent c80aaec8
......@@ -13,6 +13,8 @@
A716B5D8216E782A00C84970 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = A716B5D7216E782A00C84970 /* Assets.xcassets */; };
A716B5DB216E782A00C84970 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = A716B5D9216E782A00C84970 /* LaunchScreen.storyboard */; };
A716B5E5216F41C400C84970 /* BetterAttributedString.swift in Sources */ = {isa = PBXBuildFile; fileRef = A716B5E4216F41C400C84970 /* BetterAttributedString.swift */; };
A7E14013216F535E00EE3120 /* Common.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = A7E14012216F535E00EE3120 /* Common.xcassets */; };
A7E14019216F557400EE3120 /* LDImageView.m in Sources */ = {isa = PBXBuildFile; fileRef = A7E14018216F557400EE3120 /* LDImageView.m */; };
/* End PBXBuildFile section */
/* Begin PBXFileReference section */
......@@ -24,6 +26,10 @@
A716B5DA216E782A00C84970 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
A716B5DC216E782A00C84970 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
A716B5E4216F41C400C84970 /* BetterAttributedString.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BetterAttributedString.swift; sourceTree = "<group>"; };
A7E14012216F535E00EE3120 /* Common.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Common.xcassets; sourceTree = "<group>"; };
A7E14017216F557400EE3120 /* AutoLayout-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "AutoLayout-Bridging-Header.h"; sourceTree = "<group>"; };
A7E14018216F557400EE3120 /* LDImageView.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = LDImageView.m; sourceTree = "<group>"; };
A7E1401A216F55A700EE3120 /* LDAlignmentImageView.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = LDAlignmentImageView.h; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
......@@ -56,6 +62,8 @@
A716B5CF216E782800C84970 /* AutoLayout */ = {
isa = PBXGroup;
children = (
A7E14014216F550000EE3120 /* Library */,
A7E14012216F535E00EE3120 /* Common.xcassets */,
A716B5D0216E782800C84970 /* AppDelegate.swift */,
A716B5D2216E782800C84970 /* ViewController.swift */,
A716B5D4216E782800C84970 /* Main.storyboard */,
......@@ -67,6 +75,24 @@
path = AutoLayout;
sourceTree = "<group>";
};
A7E14014216F550000EE3120 /* Library */ = {
isa = PBXGroup;
children = (
A7E14016216F554700EE3120 /* https://github.com/LucasssD/LDAlignmentImageView */,
);
path = Library;
sourceTree = "<group>";
};
A7E14016216F554700EE3120 /* https://github.com/LucasssD/LDAlignmentImageView */ = {
isa = PBXGroup;
children = (
A7E14018216F557400EE3120 /* LDImageView.m */,
A7E14017216F557400EE3120 /* AutoLayout-Bridging-Header.h */,
A7E1401A216F55A700EE3120 /* LDAlignmentImageView.h */,
);
name = "https://github.com/LucasssD/LDAlignmentImageView";
sourceTree = "<group>";
};
/* End PBXGroup section */
/* Begin PBXNativeTarget section */
......@@ -100,6 +126,7 @@
TargetAttributes = {
A716B5CC216E782800C84970 = {
CreatedOnToolsVersion = 10.0;
LastSwiftMigration = 1000;
};
};
};
......@@ -126,6 +153,7 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
A7E14013216F535E00EE3120 /* Common.xcassets in Resources */,
A716B5DB216E782A00C84970 /* LaunchScreen.storyboard in Resources */,
A716B5D8216E782A00C84970 /* Assets.xcassets in Resources */,
A716B5D6216E782800C84970 /* Main.storyboard in Resources */,
......@@ -163,6 +191,7 @@
files = (
A716B5D3216E782800C84970 /* ViewController.swift in Sources */,
A716B5E5216F41C400C84970 /* BetterAttributedString.swift in Sources */,
A7E14019216F557400EE3120 /* LDImageView.m in Sources */,
A716B5D1216E782800C84970 /* AppDelegate.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
......@@ -309,8 +338,10 @@
isa = XCBuildConfiguration;
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_STYLE = Automatic;
DEVELOPMENT_TEAM = 5Q42VF5GXA;
FRAMEWORK_SEARCH_PATHS = "$(SRCROOT)/Carthage/Build/iOS";
INFOPLIST_FILE = AutoLayout/Info.plist;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
......@@ -318,6 +349,8 @@
);
PRODUCT_BUNDLE_IDENTIFIER = ind.ie.AutoLayout;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OBJC_BRIDGING_HEADER = "AutoLayout/Library/AutoLayout-Bridging-Header.h";
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 4.2;
TARGETED_DEVICE_FAMILY = "1,2";
};
......@@ -327,8 +360,10 @@
isa = XCBuildConfiguration;
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_STYLE = Automatic;
DEVELOPMENT_TEAM = 5Q42VF5GXA;
FRAMEWORK_SEARCH_PATHS = "$(SRCROOT)/Carthage/Build/iOS";
INFOPLIST_FILE = AutoLayout/Info.plist;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
......@@ -336,6 +371,7 @@
);
PRODUCT_BUNDLE_IDENTIFIER = ind.ie.AutoLayout;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OBJC_BRIDGING_HEADER = "AutoLayout/Library/AutoLayout-Bridging-Header.h";
SWIFT_VERSION = 4.2;
TARGETED_DEVICE_FAMILY = "1,2";
};
......
......@@ -44,7 +44,18 @@
</view>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="1c7-hR-87O" userLabel="Status Badge">
<rect key="frame" x="135.33333333333331" y="56.666666666666671" width="143.33333333333331" height="146.33333333333331"/>
<subviews>
<imageView userInteractionEnabled="NO" contentMode="scaleAspectFit" horizontalHuggingPriority="251" verticalHuggingPriority="251" image="Enabled" translatesAutoresizingMaskIntoConstraints="NO" id="e5t-G8-1L2" customClass="LDAlignmentImageView">
<rect key="frame" x="0.0" y="0.0" width="143.33333333333334" height="146.33333333333334"/>
</imageView>
</subviews>
<color key="backgroundColor" red="0.54029709100000001" green="0.54598444700000004" blue="0.54598444700000004" alpha="0.75429137323943662" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstItem="9aK-DD-cV1" firstAttribute="leading" secondItem="e5t-G8-1L2" secondAttribute="leading" id="28h-TI-3FG"/>
<constraint firstItem="e5t-G8-1L2" firstAttribute="top" secondItem="9aK-DD-cV1" secondAttribute="top" id="C2L-Nf-eI2"/>
<constraint firstItem="9aK-DD-cV1" firstAttribute="bottom" secondItem="e5t-G8-1L2" secondAttribute="bottom" id="ECM-KY-w2M"/>
<constraint firstItem="e5t-G8-1L2" firstAttribute="trailing" secondItem="9aK-DD-cV1" secondAttribute="trailing" id="WF2-R5-pSb"/>
</constraints>
<viewLayoutGuide key="safeArea" id="9aK-DD-cV1"/>
</view>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="P7i-RA-bJJ" userLabel="Status Badge Right Margin">
......@@ -90,7 +101,7 @@
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="Q2i-Mp-9LN" userLabel="Text">
<rect key="frame" x="16" y="239.66666666666666" width="382" height="329.33333333333337"/>
<subviews>
<textView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" usesAttributedText="YES" translatesAutoresizingMaskIntoConstraints="NO" id="PB8-0H-gLc">
<textView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" scrollEnabled="NO" editable="NO" usesAttributedText="YES" translatesAutoresizingMaskIntoConstraints="NO" id="PB8-0H-gLc">
<rect key="frame" x="0.0" y="0.0" width="382" height="329.33333333333331"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<attributedString key="attributedText">
......@@ -180,11 +191,12 @@
<mask key="constraints">
<exclude reference="awo-9B-XZJ"/>
<include reference="d3N-cH-IU0"/>
<exclude reference="TVf-IT-10a"/>
<exclude reference="7hw-bi-Upw"/>
<include reference="RtY-Mi-eeG"/>
<exclude reference="tco-CA-aF8"/>
<include reference="xj5-Zc-HyR"/>
<exclude reference="TVf-IT-10a"/>
<exclude reference="WMX-7Z-izQ"/>
<include reference="1Eb-4u-XsL"/>
<exclude reference="3dy-d4-0kQ"/>
<include reference="6DR-rQ-FnD"/>
......@@ -193,7 +205,6 @@
<exclude reference="Yk4-8N-LKK"/>
<include reference="eao-6v-Ycj"/>
<exclude reference="n8F-WX-aJw"/>
<exclude reference="WMX-7Z-izQ"/>
<exclude reference="9Hu-aP-DLy"/>
</mask>
</variation>
......@@ -207,6 +218,7 @@
</variation>
</view>
<connections>
<outlet property="statusImageView" destination="e5t-G8-1L2" id="gV2-J1-pzu"/>
<outlet property="statusTextView" destination="PB8-0H-gLc" id="eHF-sm-ZO0"/>
</connections>
</viewController>
......@@ -215,4 +227,7 @@
<point key="canvasLocation" x="136.80000000000001" y="133.5832083958021"/>
</scene>
</scenes>
<resources>
<image name="Enabled" width="300" height="335"/>
</resources>
</document>
......@@ -65,7 +65,7 @@ extension NSMutableAttributedString : BetterAttributedString
print("Warning: string '\(string)' not found for boldfacing.")
return false
}
self.addAttribute(NSAttributedStringKey.font, value: avenirNextDemiBoldFont, range: range)
self.addAttribute(NSAttributedString.Key.font, value: avenirNextDemiBoldFont, range: range)
return true
}
......@@ -86,7 +86,7 @@ extension NSMutableAttributedString : BetterAttributedString
return false
}
self.addAttribute(NSAttributedStringKey.link, value: url, range: range)
self.addAttribute(NSAttributedString.Key.link, value: url, range: range)
return true
}
......@@ -103,7 +103,7 @@ extension NSMutableAttributedString : BetterAttributedString
let paragraphStyleCentered = NSMutableParagraphStyle()
paragraphStyleCentered.alignment = NSTextAlignment.center
let message = NSMutableAttributedString(string: string, attributes: [NSAttributedStringKey.paragraphStyle: paragraphStyleCentered, NSAttributedStringKey.font: avenirNextRegularFont, NSAttributedStringKey.foregroundColor: UIColor.darkGray])
let message = NSMutableAttributedString(string: string, attributes: [NSAttributedString.Key.paragraphStyle: paragraphStyleCentered, NSAttributedString.Key.font: avenirNextRegularFont, NSAttributedString.Key.foregroundColor: UIColor.darkGray])
return message
}
......
{
"images" : [
{
"idiom" : "universal",
"filename" : "Plus_1e80f0_50.png",
"scale" : "1x"
},
{
"idiom" : "universal",
"filename" : "Plus_1e80f0_100.png",
"scale" : "2x"
},
{
"idiom" : "universal",
"filename" : "Plus_1e80f0_256.png",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
\ No newline at end of file
{
"images" : [
{
"idiom" : "universal",
"filename" : "Plus_1e80f0_50.png",
"scale" : "1x"
},
{
"idiom" : "universal",
"filename" : "Plus_1e80f0_100.png",
"scale" : "2x"
},
{
"idiom" : "universal",
"filename" : "Plus_1e80f0_256.png",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
\ No newline at end of file
{
"info" : {
"version" : 1,
"author" : "xcode"
}
}
\ No newline at end of file
{
"images" : [
{
"idiom" : "universal",
"filename" : "Disabled.pdf",
"scale" : "1x"
},
{
"idiom" : "universal",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
\ No newline at end of file
{
"images" : [
{
"idiom" : "universal",
"filename" : "Enabled.pdf",
"scale" : "1x"
},
{
"idiom" : "universal",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
\ No newline at end of file
{
"images" : [
{
"idiom" : "universal",
"filename" : "New Post_0f82fe_25.png",
"scale" : "1x"
},
{
"idiom" : "universal",
"filename" : "New Post_0f82fe_50.png",
"scale" : "2x"
},
{
"idiom" : "universal",
"filename" : "New Post_0f82fe_75.png",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
\ No newline at end of file
{
"images" : [
{
"idiom" : "universal",
"filename" : "New Post_0f82fe_25.png",
"scale" : "1x"
},
{
"idiom" : "universal",
"filename" : "New Post_0f82fe_50.png",
"scale" : "2x"
},
{
"idiom" : "universal",
"filename" : "New Post_0f82fe_75.png",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
\ No newline at end of file
{
"images" : [
{
"idiom" : "universal",
"filename" : "Safari_1e80f0_25.png",
"scale" : "1x"
},
{
"idiom" : "universal",
"filename" : "Safari_1e80f0_50.png",
"scale" : "2x"
},
{
"idiom" : "universal",
"filename" : "Safari_1e80f0_75.png",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
\ No newline at end of file
{
"images" : [
{
"idiom" : "universal",
"filename" : "Safari_1e80f0_25.png",
"scale" : "1x"
},
{
"idiom" : "universal",
"filename" : "Safari_1e80f0_50.png",
"scale" : "2x"
},
{
"idiom" : "universal",
"filename" : "Safari_1e80f0_75.png",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
\ No newline at end of file
//
// Use this file to import your target's public headers that you would like to expose to Swift.
//
#import "LDAlignmentImageView.h"
//
// LDImageView.h
// AutoLayout
//
// Created by Aral Balkan on 11/10/2018.
// Copyright © 2018 Aral Balkan. All rights reserved.
//
#ifndef LDAlignmentImageView_h
#define LDAlignmentImageView_h
//
// LDImageView.h
// FotoGis
//
// Created by Lucas Duda on 31.12.2016.
// Copyright © 2016 apronet. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface LDAlignmentImageView : UIImageView
typedef enum LDImageVerticalAlignment{
LDImageVerticalAlignmentTop,
LDImageVerticalAlignmentCenter,
LDImageVerticalAlignmentBottom
} LDImageVerticalAlignment;
typedef enum LDImageHorizontalAlignment{
LDImageHorizontalAlignmentLeft,
LDImageHorizontalAlignmentCenter,
LDImageHorizontalAlignmentRight
} LDImageHorizontalAlignment;
typedef enum LDImageContentMode{
LDImageContentModeScaleAspectFill,
LDImageContentModeScaleAspectFit,
LDImageContentModeScaleToFill,
LDImageContentModeOriginalSize
} LDImageContentMode;
@property (nonatomic) LDImageVerticalAlignment imageVerticalAlignment;
@property (nonatomic) LDImageHorizontalAlignment imageHorizontalAlignment;
@property (nonatomic) LDImageContentMode imageContentMode;
@end
#endif /* LDAlignmentImageView_h */
//
// LDAlignmentImageView.m
// FotoGis
//
// Created by Lucas Duda on 31.12.2016.
// Copyright © 2016 apronet. All rights reserved.
//
#import "LDAlignmentImageView.h"
@interface LDAlignmentImageView()
@property (weak, nonatomic) UIImageView *innerImageView;
//@property (nonatomic) UIViewContentMode innerContentMode;
@end
@implementation LDAlignmentImageView
- (instancetype)initWithCoder:(NSCoder *)aDecoder{
self = [super initWithCoder:aDecoder];
if (self) {
_imageVerticalAlignment = LDImageVerticalAlignmentCenter;
_imageHorizontalAlignment = LDImageHorizontalAlignmentCenter;
_imageContentMode = LDImageContentModeScaleAspectFit;
[super setContentMode:UIViewContentModeScaleToFill];
self.clipsToBounds = YES;
UIImageView *tmp = [[UIImageView alloc] init];
tmp.contentMode = UIViewContentModeScaleToFill;
tmp.backgroundColor = [UIColor clearColor];
[self addSubview:tmp];
_innerImageView = tmp;
self.contentMode = [super contentMode];
self.image = [super image];
}
return self;
}
- (void)layoutSubviews{
[super setImage:nil];
[super layoutSubviews];
if (!self.innerImageView.image) return;
CGFloat imageViewXOrigin = 0;
CGFloat imageViewYOrigin = 0;
CGFloat imageViewWidth;
CGFloat imageViewHeight;
// compute scale factor for imageView
CGFloat widthScaleFactor = CGRectGetWidth(self.frame) / self.image.size.width;
CGFloat heightScaleFactor = CGRectGetHeight(self.frame) / self.image.size.height;
//calculate size
if (_imageContentMode == LDImageContentModeScaleAspectFill) {
if (widthScaleFactor > heightScaleFactor) {
imageViewWidth = self.image.size.width * widthScaleFactor;
imageViewHeight = self.image.size.height * widthScaleFactor;
} else {
imageViewWidth = self.image.size.width * heightScaleFactor;
imageViewHeight = self.image.size.height * heightScaleFactor;
}
} else if (_imageContentMode == LDImageContentModeScaleAspectFit){
if (widthScaleFactor < heightScaleFactor) {
imageViewWidth = self.bounds.size.width;
imageViewHeight = self.bounds.size.width * (self.image.size.height / self.image.size.width);
} else {
imageViewWidth = self.bounds.size.height * (self.image.size.width / self.image.size.height);
imageViewHeight = self.bounds.size.height;
}
} else if (_imageContentMode == LDImageContentModeScaleToFill){
imageViewWidth = self.bounds.size.width;
imageViewHeight = self.bounds.size.height;
} else if (_imageContentMode == LDImageContentModeOriginalSize){
imageViewWidth = self.image.size.width;
imageViewHeight = self.image.size.height;
}
//calculate position
//x
switch (self.imageHorizontalAlignment) {
case LDImageHorizontalAlignmentLeft:
break;
case LDImageHorizontalAlignmentCenter:
imageViewXOrigin += (self.frame.size.width - imageViewWidth) / 2.0;
break;
case LDImageHorizontalAlignmentRight:
imageViewXOrigin += self.frame.size.width - imageViewWidth;
break;
default:
imageViewXOrigin += (self.frame.size.width - imageViewWidth) / 2.0;
break;
}
//y
switch (self.imageVerticalAlignment) {
case LDImageVerticalAlignmentTop:
break;
case LDImageVerticalAlignmentCenter:
imageViewYOrigin += (self.frame.size.height - imageViewHeight) / 2.0;
break;
case LDImageVerticalAlignmentBottom:
imageViewYOrigin += self.frame.size.height - imageViewHeight;
break;
default:
imageViewYOrigin += (self.frame.size.height - imageViewHeight) / 2.0;
break;
}
self.innerImageView.frame = CGRectMake(imageViewXOrigin,
imageViewYOrigin,
imageViewWidth,
imageViewHeight);
}
- (void)setImageVerticalAlignment:(LDImageVerticalAlignment)imageVerticalAlignment{
if (imageVerticalAlignment != _imageVerticalAlignment) {
_imageVerticalAlignment = imageVerticalAlignment;
// [self setNeedsUpdateConstraints];
[self layoutSubviews];
}
}
- (void)setImageHorizontalAlignment:(LDImageHorizontalAlignment)imageHorizontalAlignment{
if (imageHorizontalAlignment != _imageHorizontalAlignment) {
_imageHorizontalAlignment = imageHorizontalAlignment;
[self layoutSubviews];
}
}
- (void)setImageContentMode:(LDImageContentMode)imageContentMode{
if (imageContentMode != _imageContentMode) {
_imageContentMode = imageContentMode;
[self layoutSubviews];
}
}
- (void)setContentMode:(UIViewContentMode)contentMode{
switch (contentMode) {
case UIViewContentModeScaleToFill:
_imageContentMode = LDImageContentModeScaleToFill;
break;
case UIViewContentModeScaleAspectFill:
_imageContentMode = LDImageContentModeScaleAspectFill;
break;
case UIViewContentModeScaleAspectFit:
_imageContentMode = LDImageContentModeScaleAspectFit;
break;
case UIViewContentModeTop:
_imageVerticalAlignment = LDImageVerticalAlignmentTop;
break;
case UIViewContentModeCenter:
_imageVerticalAlignment = LDImageVerticalAlignmentCenter;
_imageHorizontalAlignment = LDImageHorizontalAlignmentCenter;
break;
case UIViewContentModeBottom:
_imageVerticalAlignment = LDImageVerticalAlignmentBottom;
break;
case UIViewContentModeLeft:
_imageHorizontalAlignment = LDImageHorizontalAlignmentLeft;
break;
case UIViewContentModeRight:
_imageHorizontalAlignment = LDImageHorizontalAlignmentRight;
break;
case UIViewContentModeTopLeft:
_imageHorizontalAlignment = LDImageHorizontalAlignmentLeft;
_imageVerticalAlignment = LDImageVerticalAlignmentTop;
break;
case UIViewContentModeTopRight:
_imageHorizontalAlignment = LDImageHorizontalAlignmentRight;
_imageVerticalAlignment = LDImageVerticalAlignmentTop;
break;