浏览代码

created flexlabel and using styled version for emtpyStateLabels

nayooti 5 年之前
父节点
当前提交
99b3503b96

+ 4 - 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 */; };
@@ -329,7 +329,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>"; };
@@ -939,7 +939,7 @@
 				AE52EA18229EB53C00C586C9 /* ContactDetailHeader.swift */,
 				AE9DAF0E22C278C6004C9591 /* ChatTitleView.swift */,
 				30F9B9EB235F2116006E7ACF /* MessageCounter.swift */,
-				305FE03523A81B4C0053BE90 /* PaddingLabel.swift */,
+				305FE03523A81B4C0053BE90 /* EmptyStateLabel.swift */,
 				AEFBE22E23FEF23D0045327A /* ProviderInfoCell.swift */,
 			);
 			path = View;
@@ -1360,7 +1360,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 */,

+ 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

@@ -228,11 +228,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
     }()
 
@@ -267,7 +265,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

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

@@ -0,0 +1,122 @@
+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")
+    }
+}
+
+/// 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)
-    }
-}