瀏覽代碼

Merge pull request #713 from deltachat/padding-label

Wrapped PaddingView into FlexView
nayooti 5 年之前
父節點
當前提交
cef32b69bb

+ 8 - 4
deltachat-ios.xcodeproj/project.pbxproj

@@ -88,7 +88,7 @@
 		3059620C2346125100C80F33 /* MessageSizeCalculator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 305961CB2346125100C80F33 /* MessageSizeCalculator.swift */; };
 		3059620E234614E700C80F33 /* DcContact+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3059620D234614E700C80F33 /* DcContact+Extension.swift */; };
 		305962102346154D00C80F33 /* String+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3059620F2346154D00C80F33 /* String+Extension.swift */; };
-		305FE03623A81B4C0053BE90 /* PaddingLabel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 305FE03523A81B4C0053BE90 /* PaddingLabel.swift */; };
+		305FE03623A81B4C0053BE90 /* EmptyStateLabel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 305FE03523A81B4C0053BE90 /* EmptyStateLabel.swift */; };
 		3060119C22DDE24000C1CE6F /* Localizable.strings in Resources */ = {isa = PBXBuildFile; fileRef = 3060119E22DDE24000C1CE6F /* Localizable.strings */; };
 		306011B622E5E7FB00C1CE6F /* Localizable.stringsdict in Resources */ = {isa = PBXBuildFile; fileRef = 306011B422E5E7FB00C1CE6F /* Localizable.stringsdict */; };
 		306C32322445CDE9001D89F3 /* DcLogger.swift in Sources */ = {isa = PBXBuildFile; fileRef = 306C32312445CDE9001D89F3 /* DcLogger.swift */; };
@@ -156,6 +156,7 @@
 		AEACE2DF1FB3246400DCDD78 /* Message.swift in Sources */ = {isa = PBXBuildFile; fileRef = AEACE2DE1FB3246400DCDD78 /* Message.swift */; };
 		AEACE2E31FB32B5C00DCDD78 /* Constants.swift in Sources */ = {isa = PBXBuildFile; fileRef = AEACE2E21FB32B5C00DCDD78 /* Constants.swift */; };
 		AEACE2E51FB32E1900DCDD78 /* Utils.swift in Sources */ = {isa = PBXBuildFile; fileRef = AEACE2E41FB32E1900DCDD78 /* Utils.swift */; };
+		AEB54C7F246DBA610004624C /* FlexLabel.swift in Sources */ = {isa = PBXBuildFile; fileRef = AEB54C7E246DBA610004624C /* FlexLabel.swift */; };
 		AEC67A1C241CE9E4007DDBE1 /* AppStateRestorer.swift in Sources */ = {isa = PBXBuildFile; fileRef = AEC67A1B241CE9E4007DDBE1 /* AppStateRestorer.swift */; };
 		AEC67A1E241FCFE0007DDBE1 /* ChatListViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = AEC67A1D241FCFE0007DDBE1 /* ChatListViewModel.swift */; };
 		AECEF03E244F2D55006C90DA /* QrPageController.swift in Sources */ = {isa = PBXBuildFile; fileRef = AECEF03D244F2D55006C90DA /* QrPageController.swift */; };
@@ -329,7 +330,7 @@
 		305961CB2346125100C80F33 /* MessageSizeCalculator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MessageSizeCalculator.swift; sourceTree = "<group>"; };
 		3059620D234614E700C80F33 /* DcContact+Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "DcContact+Extension.swift"; sourceTree = "<group>"; };
 		3059620F2346154D00C80F33 /* String+Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "String+Extension.swift"; sourceTree = "<group>"; };
-		305FE03523A81B4C0053BE90 /* PaddingLabel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PaddingLabel.swift; sourceTree = "<group>"; };
+		305FE03523A81B4C0053BE90 /* EmptyStateLabel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EmptyStateLabel.swift; sourceTree = "<group>"; };
 		3060119D22DDE24000C1CE6F /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/Localizable.strings; sourceTree = "<group>"; };
 		3060119F22DDE24500C1CE6F /* de */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = de; path = de.lproj/Localizable.strings; sourceTree = "<group>"; };
 		306011A022DDE24700C1CE6F /* sq */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = sq; path = sq.lproj/Localizable.strings; sourceTree = "<group>"; };
@@ -442,6 +443,7 @@
 		AEACE2DE1FB3246400DCDD78 /* Message.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Message.swift; sourceTree = "<group>"; };
 		AEACE2E21FB32B5C00DCDD78 /* Constants.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Constants.swift; sourceTree = "<group>"; };
 		AEACE2E41FB32E1900DCDD78 /* Utils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Utils.swift; sourceTree = "<group>"; };
+		AEB54C7E246DBA610004624C /* FlexLabel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FlexLabel.swift; sourceTree = "<group>"; };
 		AEC67A1B241CE9E4007DDBE1 /* AppStateRestorer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppStateRestorer.swift; sourceTree = "<group>"; };
 		AEC67A1D241FCFE0007DDBE1 /* ChatListViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChatListViewModel.swift; sourceTree = "<group>"; };
 		AECEF03D244F2D55006C90DA /* QrPageController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = QrPageController.swift; sourceTree = "<group>"; };
@@ -939,8 +941,9 @@
 				AE52EA18229EB53C00C586C9 /* ContactDetailHeader.swift */,
 				AE9DAF0E22C278C6004C9591 /* ChatTitleView.swift */,
 				30F9B9EB235F2116006E7ACF /* MessageCounter.swift */,
-				305FE03523A81B4C0053BE90 /* PaddingLabel.swift */,
+				305FE03523A81B4C0053BE90 /* EmptyStateLabel.swift */,
 				AEFBE22E23FEF23D0045327A /* ProviderInfoCell.swift */,
+				AEB54C7E246DBA610004624C /* FlexLabel.swift */,
 			);
 			path = View;
 			sourceTree = "<group>";
@@ -1360,7 +1363,7 @@
 				AEE56D80225504DB007DC082 /* Extensions.swift in Sources */,
 				7A0052C81FBE6CB40048C3BF /* NewContactController.swift in Sources */,
 				AEE56D762253431E007DC082 /* AccountSetupController.swift in Sources */,
-				305FE03623A81B4C0053BE90 /* PaddingLabel.swift in Sources */,
+				305FE03623A81B4C0053BE90 /* EmptyStateLabel.swift in Sources */,
 				AEACE2DD1FB323CA00DCDD78 /* ChatViewController.swift in Sources */,
 				AEE6EC412282DF5700EDC689 /* MailboxViewController.swift in Sources */,
 				AEE6EC482283045D00EDC689 /* EditSettingsController.swift in Sources */,
@@ -1420,6 +1423,7 @@
 				305961F12346125100C80F33 /* ContactMessageCell.swift in Sources */,
 				AE851AD0227DF50900ED86F0 /* GroupChatDetailViewController.swift in Sources */,
 				305961D12346125100C80F33 /* Bundle+Extensions.swift in Sources */,
+				AEB54C7F246DBA610004624C /* FlexLabel.swift in Sources */,
 				305962002346125100C80F33 /* MessagesCollectionView.swift in Sources */,
 				7A451DB01FB1F84900177250 /* AppCoordinator.swift in Sources */,
 				AE38B31822672DFC00EC37A1 /* ActionCell.swift in Sources */,

+ 7 - 8
deltachat-ios/Controller/ChatViewController.swift

@@ -67,10 +67,8 @@ class ChatViewController: MessagesViewController {
         return MediaPicker(navigationController: navigationController)
     }()
 
-    var emptyStateView: PaddingLabel = {
-        let view =  PaddingLabel()
-        view.backgroundColor = DcColors.systemMessageBackgroundColor
-        view.textColor = DcColors.defaultTextColor
+    var emptyStateView: EmptyStateLabel = {
+        let view =  EmptyStateLabel()
         return view
     }()
 
@@ -137,10 +135,11 @@ class ChatViewController: MessagesViewController {
 
     private func configureEmptyStateView() {
         view.addSubview(emptyStateView)
-        view.addConstraints([emptyStateView.constraintCenterYTo(view),
-                             emptyStateView.constraintCenterXTo(view),
-                             emptyStateView.constraintAlignLeadingTo(view, paddingLeading: 40),
-                             emptyStateView.constraintAlignTrailingTo(view, paddingTrailing: 40)])
+        emptyStateView.translatesAutoresizingMaskIntoConstraints = false
+        emptyStateView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 40).isActive = true
+        emptyStateView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -40).isActive = true
+        emptyStateView.centerYAnchor.constraint(equalTo: view.safeAreaLayoutGuide.centerYAnchor).isActive = true
+        
     }
 
     override func viewWillAppear(_ animated: Bool) {

+ 4 - 5
deltachat-ios/Controller/GroupMembersViewController.swift

@@ -229,11 +229,9 @@ class AddGroupMembersViewController: GroupMembersViewController {
 
 class BlockedContactsViewController: GroupMembersViewController, GroupMemberSelectionDelegate {
 
-    var emptyStateView: PaddingLabel = {
-        let view =  PaddingLabel()
+    var emptyStateView: EmptyStateLabel = {
+        let view =  EmptyStateLabel()
         view.text = String.localized("none_blocked_desktop")
-        view.backgroundColor = DcColors.systemMessageBackgroundColor
-        view.textColor = DcColors.defaultTextColor
         return view
     }()
 
@@ -268,7 +266,8 @@ class BlockedContactsViewController: GroupMembersViewController, GroupMemberSele
         emptyStateView.translatesAutoresizingMaskIntoConstraints = false
         emptyStateView.centerYAnchor.constraint(equalTo: view.safeAreaLayoutGuide.centerYAnchor).isActive = true
         emptyStateView.centerXAnchor.constraint(equalTo: view.safeAreaLayoutGuide.centerXAnchor).isActive = true
-        emptyStateView.widthAnchor.constraint(equalToConstant: 200).isActive = true
+        emptyStateView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 40).isActive = true
+        emptyStateView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -40).isActive = true
     }
 
     // MARK: - actions + updates

+ 41 - 0
deltachat-ios/View/EmptyStateLabel.swift

@@ -0,0 +1,41 @@
+import UIKit
+import DcCore
+
+class EmptyStateLabel: FlexLabel {
+
+    override var text: String? {
+        set {
+            guard let newValue = newValue else {
+                super.label.attributedText = nil
+                return
+            }
+
+            guard let style = NSMutableParagraphStyle.default.mutableCopy() as? NSMutableParagraphStyle else {
+                label.attributedText = NSAttributedString(string: newValue)
+                return
+            }
+            style.alignment = NSTextAlignment.natural
+            style.lineBreakMode = .byWordWrapping
+            attributedText = NSAttributedString(
+                string: newValue,
+                attributes: [.paragraphStyle: style]
+            )
+        }
+        get {
+            return super.label.text
+        }
+    }
+
+    override init() {
+        super.init()
+        label.backgroundColor = DcColors.systemMessageBackgroundColor
+        label.textColor = DcColors.defaultTextColor
+        label.layer.cornerRadius = 10
+        label.clipsToBounds = true
+    }
+
+    required init?(coder: NSCoder) {
+        fatalError("init(coder:) has not been implemented")
+    }
+}
+

+ 83 - 0
deltachat-ios/View/FlexLabel.swift

@@ -0,0 +1,83 @@
+import UIKit
+
+/// view that contains a label (horizontally centered) and
+/// allows it's label to grow/shrink within it's available space
+class FlexLabel: UIView {
+
+    var text: String? {
+        set {
+            label.text = newValue
+        }
+        get {
+            return label.text
+        }
+    }
+
+    var textColor: UIColor {
+        set {
+            label.textColor = newValue
+        }
+        get {
+            return label.textColor
+        }
+    }
+
+    var attributedText: NSAttributedString? {
+        set {
+            label.attributedText = newValue
+        }
+        get {
+            return label.attributedText
+        }
+    }
+
+    lazy var label: UILabel = {
+        let label = PaddingLabel(top: 15, left: 15, bottom: 15, right: 15)
+        label.numberOfLines = 0
+        return label
+    }()
+
+    init() {
+        super.init(frame: .zero)
+        setupSubviews()
+    }
+
+    required init?(coder: NSCoder) {
+        fatalError("init(coder:) has not been implemented")
+    }
+
+    private func setupSubviews() {
+        addSubview(label)
+        label.translatesAutoresizingMaskIntoConstraints = false
+        label.centerYAnchor.constraint(equalTo: centerYAnchor, constant: 0).isActive = true
+        label.centerXAnchor.constraint(equalTo: centerXAnchor, constant: 0).isActive = true
+        label.topAnchor.constraint(equalTo: layoutMarginsGuide.topAnchor).isActive = true
+        label.bottomAnchor.constraint(equalTo: layoutMarginsGuide.bottomAnchor).isActive = true
+        label.widthAnchor.constraint(lessThanOrEqualTo: widthAnchor, multiplier: 0.95).isActive = true
+    }
+
+    class PaddingLabel: UILabel {
+        let insets: UIEdgeInsets
+
+        init(top: CGFloat, left: CGFloat, bottom: CGFloat, right: CGFloat) {
+            self.insets = UIEdgeInsets(top: top, left: left, bottom: bottom, right: right)
+            super.init(frame: .zero)
+        }
+
+        required init?(coder: NSCoder) {
+            fatalError("init(coder:) has not been implemented")
+        }
+
+        override func drawText(in rect: CGRect) {
+            super.drawText(in: rect.inset(by: insets))
+        }
+
+        override var intrinsicContentSize: CGSize {
+            let size = super.intrinsicContentSize
+            return CGSize(
+                width: size.width + insets.left + insets.right,
+                height: size.height + insets.top + insets.bottom
+            )
+        }
+    }
+}

+ 0 - 57
deltachat-ios/View/PaddingLabel.swift

@@ -1,57 +0,0 @@
-import UIKit
-
-class PaddingLabel: UILabel {
-
-    var contentInsets = UIEdgeInsets(top: 12, left: 0, bottom: 12, right: 0)
-
-    required init() {
-        super.init(frame: CGRect.zero)
-        numberOfLines = 0
-        layer.cornerRadius = 12
-        clipsToBounds = true
-        self.translatesAutoresizingMaskIntoConstraints = false
-    }
-
-    required init?(coder aDecoder: NSCoder) {
-        fatalError("init(coder:) has not been implemented")
-    }
-
-    override var text: String? {
-        set {
-            guard let newValue = newValue else {
-                attributedText = nil
-                return
-            }
-
-            guard let style = NSMutableParagraphStyle.default.mutableCopy() as? NSMutableParagraphStyle else {
-                attributedText = NSAttributedString(string: newValue)
-                return
-            }
-
-            style.alignment = NSTextAlignment.natural
-            style.firstLineHeadIndent = 12.0
-            style.headIndent = 12.0
-            style.tailIndent = -12.0
-            style.lineBreakMode = .byWordWrapping
-            attributedText = NSAttributedString(string: newValue, attributes: [.paragraphStyle: style])
-        }
-
-        get {
-            return self.attributedText?.string
-        }
-    }
-
-    override func drawText(in rect: CGRect) {
-        super.drawText(in: rect.inset(by: contentInsets))
-    }
-
-    override var intrinsicContentSize: CGSize {
-        return addInsets(to: super.intrinsicContentSize)
-    }
-
-    private func addInsets(to size: CGSize) -> CGSize {
-        let width = size.width + contentInsets.left + contentInsets.right
-        let height = size.height + contentInsets.top + contentInsets.bottom
-        return CGSize(width: width, height: height)
-    }
-}