Skip to content

Commit

Permalink
Using the new method of calculating line height
Browse files Browse the repository at this point in the history
The previous method could not calculate the correct height in iOS15.
  • Loading branch information
rakuyoMo committed Oct 11, 2021
1 parent 7e44b1a commit e0e0cd7
Showing 1 changed file with 11 additions and 12 deletions.
23 changes: 11 additions & 12 deletions JSONPreview/JSONPreview/Core/JSONPreview.swift
Original file line number Diff line number Diff line change
Expand Up @@ -204,22 +204,21 @@ private extension JSONPreview {

/// Calculate the line height of the line number display area
func calculateLineHeight(at index: Int, width: CGFloat) -> CGFloat {
let attString = decorator.slices[index].expand
let size = CGSize(width: width, height: CGFloat.greatestFiniteMagnitude)

var tmpAtt: [NSAttributedString.Key : Any] = [:]
let textContainer = NSTextContainer(size: size)
textContainer.lineFragmentPadding = 0

attString.enumerateAttributes(in: NSRange(location: 0, length: attString.length)) { (values, _, _) in
values.forEach { tmpAtt[$0] = $1 }
}
let layoutManager = NSLayoutManager()
layoutManager.addTextContainer(textContainer)
layoutManager.glyphRange(forBoundingRect: CGRect(origin: .zero, size: size), in: textContainer)

let rect = (attString.string as NSString).boundingRect(
with: CGSize(width: width, height: CGFloat.greatestFiniteMagnitude),
options: [.usesLineFragmentOrigin, .usesFontLeading],
attributes: tmpAtt,
context: nil
)
let attString = decorator.slices[index].expand
let textStorage = NSTextStorage(attributedString: attString)
textStorage.addLayoutManager(layoutManager)

return rect.height
let rect = layoutManager.usedRect(for: textContainer)
return rect.size.height
}

func listeningDeviceRotation() {
Expand Down

0 comments on commit e0e0cd7

Please sign in to comment.