From 23608f7ec8bcae62d46c05cd8760799cacf74830 Mon Sep 17 00:00:00 2001 From: Lee <18611401994@163.com> Date: Tue, 11 Aug 2020 17:20:10 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Demo/Demo/Debug/DebugLabelView.swift | 2 ++ Demo/Demo/Debug/DebugLabelViewController.swift | 16 ++++++++++++++++ .../UILabel/UILabelLayoutManagerDelegate.swift | 12 +++++++----- Sources/ParagraphStyle.swift | 7 +++++++ 4 files changed, 32 insertions(+), 5 deletions(-) diff --git a/Demo/Demo/Debug/DebugLabelView.swift b/Demo/Demo/Debug/DebugLabelView.swift index a837425..ec3e66a 100644 --- a/Demo/Demo/Debug/DebugLabelView.swift +++ b/Demo/Demo/Debug/DebugLabelView.swift @@ -96,6 +96,7 @@ class DebugLabelView: UIView { didSet { let value = lineSpacing lineSpacingLabel.text = String(format: "%.2f", value) + lineSpacingSlider.value = .init(value) } } @@ -103,6 +104,7 @@ class DebugLabelView: UIView { didSet { let value = lineHeightMultiple lineHeightMultipleLabel.text = String(format: "%.2f", value) + lineHeightMultipleSlider.value = .init(value) } } diff --git a/Demo/Demo/Debug/DebugLabelViewController.swift b/Demo/Demo/Debug/DebugLabelViewController.swift index 3ea90ea..bd5115f 100644 --- a/Demo/Demo/Debug/DebugLabelViewController.swift +++ b/Demo/Demo/Debug/DebugLabelViewController.swift @@ -15,6 +15,20 @@ class DebugLabelViewController: ViewController { private var info: Debug.Label = .init() { didSet { + if let value = info.lineSpacing { + paragraphs.removeAll(where: { $0 == .lineSpacing(0) }) + paragraphs.append(.lineSpacing(value)) + + } else { + paragraphs.removeAll(where: { $0 == .lineSpacing(0) }) + } + if let value = info.lineHeightMultiple { + paragraphs.removeAll(where: { $0 == .lineHeightMultiple(0) }) + paragraphs.append(.lineHeightMultiple(value)) + + } else { + paragraphs.removeAll(where: { $0 == .lineHeightMultiple(0) }) + } container.set(info: info) } } @@ -108,11 +122,13 @@ class DebugLabelViewController: ViewController { } @IBAction func lineSpacingSliderAction(_ sender: UISlider) { + paragraphs.removeAll(where: { $0 == .lineSpacing(0) }) paragraphs.append(.lineSpacing(.init(sender.value))) info.lineSpacing = .init(sender.value) update() } @IBAction func lineHeightMultipleSliderAction(_ sender: UISlider) { + paragraphs.removeAll(where: { $0 == .lineHeightMultiple(0) }) paragraphs.append(.lineHeightMultiple(.init(sender.value))) info.lineHeightMultiple = .init(sender.value) update() diff --git a/Sources/Extension/UIKit/UILabel/UILabelLayoutManagerDelegate.swift b/Sources/Extension/UIKit/UILabel/UILabelLayoutManagerDelegate.swift index 547f6b7..df5c74f 100644 --- a/Sources/Extension/UIKit/UILabel/UILabelLayoutManagerDelegate.swift +++ b/Sources/Extension/UIKit/UILabel/UILabelLayoutManagerDelegate.swift @@ -65,12 +65,8 @@ class UILabelLayoutManagerDelegate: NSObject, NSLayoutManagerDelegate { var rect = lineFragmentRect.pointee var used = lineFragmentUsedRect.pointee - // 以最大的高度为准 (可解决附件问题), 同时根据最大行数是否为1来判断used是否需要增加行间距, 以解决1行时应该无行间距的问题. - let temp = max(maximum.lineHeight, used.height) - rect.size.height = temp + maximum.lineSpacing + paragraphSpacing + paragraphSpacingBefore - used.size.height = textContainer.maximumNumberOfLines == 1 ? temp : temp + maximum.lineSpacing - // 当Label发生Scaled时 最大行数为1时 基线偏移不会按比例计算 + // 当Label发生Scaled时 最大行数为1时 if let scaledMetrics = scaledMetrics, textContainer.maximumNumberOfLines == 1 { switch baselineAdjustment { case .alignBaselines: @@ -103,6 +99,12 @@ class UILabelLayoutManagerDelegate: NSObject, NSLayoutManagerDelegate { default: break } + + } else { + // 以最大的高度为准 (可解决附件问题), 同时根据最大行数是否为1来判断used是否需要增加行间距, 以解决1行时应该无行间距的问题. + let temp = max(maximum.lineHeight, used.height) + rect.size.height = temp + maximum.lineSpacing + paragraphSpacing + paragraphSpacingBefore + used.size.height = temp } // 重新赋值最终结果 diff --git a/Sources/ParagraphStyle.swift b/Sources/ParagraphStyle.swift index f96575e..fedc115 100644 --- a/Sources/ParagraphStyle.swift +++ b/Sources/ParagraphStyle.swift @@ -156,3 +156,10 @@ extension AttributedString.Attribute.ParagraphStyle { return .init(style: [.allowsDefaultTighteningForTruncation: value]) } } + +extension AttributedString.Attribute.ParagraphStyle: Equatable { + + public static func == (lhs: AttributedString.Attribute.ParagraphStyle, rhs: AttributedString.Attribute.ParagraphStyle) -> Bool { + return lhs.style.keys == rhs.style.keys + } +}