Skip to content

Commit

Permalink
优化代码
Browse files Browse the repository at this point in the history
  • Loading branch information
lixiang1994 committed Aug 11, 2020
1 parent af5b636 commit 23608f7
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 5 deletions.
2 changes: 2 additions & 0 deletions Demo/Demo/Debug/DebugLabelView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,15 @@ class DebugLabelView: UIView {
didSet {
let value = lineSpacing
lineSpacingLabel.text = String(format: "%.2f", value)
lineSpacingSlider.value = .init(value)
}
}

var lineHeightMultiple: CGFloat = 0 {
didSet {
let value = lineHeightMultiple
lineHeightMultipleLabel.text = String(format: "%.2f", value)
lineHeightMultipleSlider.value = .init(value)
}
}

Expand Down
16 changes: 16 additions & 0 deletions Demo/Demo/Debug/DebugLabelViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,20 @@ class DebugLabelViewController: ViewController<DebugLabelView> {

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)
}
}
Expand Down Expand Up @@ -108,11 +122,13 @@ class DebugLabelViewController: ViewController<DebugLabelView> {
}

@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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
}

// 重新赋值最终结果
Expand Down
7 changes: 7 additions & 0 deletions Sources/ParagraphStyle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

0 comments on commit 23608f7

Please sign in to comment.