Pārlūkot izejas kodu

implement self-sizing basic cell

cyberta 5 gadi atpakaļ
vecāks
revīzija
5d26a2f7bf

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

@@ -98,6 +98,7 @@
 		308FEA50246AB67100FCEAD6 /* FileMessageCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 308FEA4F246AB67100FCEAD6 /* FileMessageCell.swift */; };
 		308FEA52246ABA2700FCEAD6 /* FileMessageSizeCalculator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 308FEA51246ABA2700FCEAD6 /* FileMessageSizeCalculator.swift */; };
 		3095A351237DD1F700AB07F7 /* MediaPicker.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3095A350237DD1F700AB07F7 /* MediaPicker.swift */; };
+		30A2039D24A4A1A100BFF51C /* BasicCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 30A2039C24A4A1A100BFF51C /* BasicCell.swift */; };
 		30A2EC36247D72720024ADD8 /* AnimatedImageMessageCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 30A2EC35247D72720024ADD8 /* AnimatedImageMessageCell.swift */; };
 		30A4D9AE2332672700544344 /* QrInviteViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 30A4D9AD2332672600544344 /* QrInviteViewController.swift */; };
 		30B0ACFA24AB5B99004D5E29 /* SettingsEphemeralMessageController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 30B0ACF924AB5B99004D5E29 /* SettingsEphemeralMessageController.swift */; };
@@ -384,6 +385,7 @@
 		308FEA4F246AB67100FCEAD6 /* FileMessageCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FileMessageCell.swift; sourceTree = "<group>"; };
 		308FEA51246ABA2700FCEAD6 /* FileMessageSizeCalculator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FileMessageSizeCalculator.swift; sourceTree = "<group>"; };
 		3095A350237DD1F700AB07F7 /* MediaPicker.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MediaPicker.swift; sourceTree = "<group>"; };
+		30A2039C24A4A1A100BFF51C /* BasicCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BasicCell.swift; sourceTree = "<group>"; };
 		30A2EC35247D72720024ADD8 /* AnimatedImageMessageCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AnimatedImageMessageCell.swift; sourceTree = "<group>"; };
 		30A4D9AD2332672600544344 /* QrInviteViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = QrInviteViewController.swift; sourceTree = "<group>"; };
 		30AC265E237F1807002A943F /* AvatarHelper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AvatarHelper.swift; sourceTree = "<group>"; };
@@ -992,6 +994,7 @@
 				AEFBE22E23FEF23D0045327A /* ProviderInfoCell.swift */,
 				AEB54C7E246DBA610004624C /* FlexLabel.swift */,
 				AED62BCD247687E6009E220D /* LocationStreamingIndicator.swift */,
+				30A2039C24A4A1A100BFF51C /* BasicCell.swift */,
 			);
 			path = View;
 			sourceTree = "<group>";
@@ -1421,6 +1424,7 @@
 				302B84C6239676F0001C261F /* AvatarHelper.swift in Sources */,
 				AE77838D23E32ED20093EABD /* ContactDetailViewModel.swift in Sources */,
 				305961E62346125100C80F33 /* LocationMessageSnapshotOptions.swift in Sources */,
+				30A2039D24A4A1A100BFF51C /* BasicCell.swift in Sources */,
 				AEE6EC3F2282C59C00EDC689 /* GroupMembersViewController.swift in Sources */,
 				B26B3BC7236DC3DC008ED35A /* SwitchCell.swift in Sources */,
 				AEE700252438E0E500D6992E /* ProgressAlertHandler.swift in Sources */,

+ 102 - 0
deltachat-ios/View/BasicCell.swift

@@ -0,0 +1,102 @@
+import Foundation
+import UIKit
+
+class BasicCell: UITableViewCell {
+
+    static let reuseIdentifier = "basic_cell_reuse_identifier"
+
+    private var fontSize: CGFloat {
+        return UIFont.preferredFont(forTextStyle: .body).pointSize
+    }
+    private let maxFontSizeHorizontalLayout: CGFloat = 24
+    private var layoutConstraints: [NSLayoutConstraint] = []
+    var margin: CGFloat = 12
+
+    public lazy var title: UILabel = {
+        let label = UILabel()
+        label.font = .preferredFont(forTextStyle: .body)
+        label.adjustsFontForContentSizeCategory = true
+        label.lineBreakMode = .byTruncatingTail
+        label.translatesAutoresizingMaskIntoConstraints = false
+        label.setContentCompressionResistancePriority(UILayoutPriority(rawValue: 10), for: NSLayoutConstraint.Axis.horizontal)
+        return label
+    }()
+
+    public lazy var value: UILabel = {
+        let label = UILabel()
+        label.font = .preferredFont(forTextStyle: .body)
+        label.adjustsFontForContentSizeCategory = true
+        label.textColor = .darkGray
+        label.lineBreakMode = .byTruncatingTail
+        label.translatesAutoresizingMaskIntoConstraints = false
+        label.setContentHuggingPriority(.defaultHigh, for: NSLayoutConstraint.Axis.horizontal)
+        label.setContentCompressionResistancePriority(UILayoutPriority(rawValue: 1), for: NSLayoutConstraint.Axis.horizontal)
+        label.textAlignment = .right
+        return label
+    }()
+
+    public lazy var stackView: UIStackView = {
+        let view = UIStackView()
+        view.translatesAutoresizingMaskIntoConstraints = false
+        view.clipsToBounds = true
+
+        view.addArrangedSubview(title)
+        view.addArrangedSubview(value)
+        view.axis = .horizontal
+        view.spacing = 10
+        return view
+    }()
+
+    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
+        super.init(style: style, reuseIdentifier: reuseIdentifier)
+        setupSubviews()
+    }
+
+    required init?(coder _: NSCoder) {
+        fatalError("init(coder:) has not been implemented")
+    }
+
+
+    private func setupSubviews() {
+        contentView.addSubview(stackView)
+        contentView.removeConstraints(contentView.constraints)
+               contentView.addConstraints([
+                   stackView.constraintAlignLeadingTo(contentView, paddingLeading: margin),
+                   stackView.constraintAlignTopTo(contentView, paddingTop: margin),
+                   stackView.constraintAlignBottomTo(contentView, paddingBottom: margin),
+                   stackView.constraintAlignTrailingTo(/*accessoryView ??*/ contentView, paddingTrailing: margin)
+               ])
+        updateViews()
+    }
+
+    override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
+        if previousTraitCollection?.preferredContentSizeCategory !=
+            traitCollection.preferredContentSizeCategory {
+            updateViews()
+        }
+    }
+
+    public func updateViews() {
+        if fontSize <= maxFontSizeHorizontalLayout {
+            title.numberOfLines = 1
+            value.numberOfLines = 1
+            value.textAlignment = .right
+            stackView.axis = .horizontal
+
+        } else {
+            title.numberOfLines = 0
+            value.numberOfLines = 0
+            value.textAlignment = .left
+            stackView.axis = .vertical
+        }
+    }
+
+    override func prepareForReuse() {
+        super.prepareForReuse()
+        title.text = nil
+        value.text = nil
+        title.attributedText = nil
+        value.attributedText = nil
+
+    }
+}