Skip to content

Commit

Permalink
Trying to fix auto layout with lclabel (#29)
Browse files Browse the repository at this point in the history
Trying to fix lclabel with autolayout

Updates Screenshots & fixes issues with LCLabel and autolayout
  • Loading branch information
mustiikhalil committed Aug 24, 2022
1 parent 3a5f300 commit ece857b
Show file tree
Hide file tree
Showing 13 changed files with 93 additions and 21 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
39 changes: 18 additions & 21 deletions Sources/LCLabel/LCLabel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -167,17 +167,10 @@ final public class LCLabel: UILabel {
}
/// Returns intrinsicContentSize of the current label
public override var intrinsicContentSize: CGSize {
guard let text = renderedStorage, !text.string.isEmpty else {
return .zero
}
let rect = CGRect(
x: 0,
y: 0,
width: preferredMaxLayoutWidth,
height: .greatestFiniteMagnitude)
return textRect(
forBounds: rect,
limitedToNumberOfLines: numberOfLines).size
textContainer.size = bounds.size
layoutManager.ensureLayout(for: textContainer)
let rect = layoutManager.usedRect(for: textContainer)
return rect.size
}
/// Text to be displayed
public override var attributedText: NSAttributedString? {
Expand Down Expand Up @@ -229,6 +222,8 @@ final public class LCLabel: UILabel {
layoutManager.drawGlyphs(
forGlyphRange: range,
at: bounds.origin)

invalidateIntrinsicContentSize()
}

public override func textRect(
Expand All @@ -241,22 +236,23 @@ final public class LCLabel: UILabel {
assert(
!newBounds.isNegative,
"The new bounds are negative with isnt allowed, check the frame or the textInsets")
guard let text = renderedStorage else {
return .zero
}
let calculatedHeight = text.boundingRect(
with: newBounds.size,
options: .usesLineFragmentOrigin,
context: nil)
textContainer.size = newBounds.size
layoutManager.ensureLayout(for: textContainer)
let calculatedValues = layoutManager.usedRect(for: textContainer)
switch centeringTextAlignment {
case .center:
newBounds.origin.y = (newBounds.height - calculatedHeight.height) / 2
newBounds.origin
.y = floor((newBounds.height - calculatedValues.height) / 2)
case .bottom:
newBounds.origin.y = (newBounds.height - calculatedHeight.height)
newBounds.origin.y = (newBounds.height - calculatedValues.height)
case .top:
break
}
return newBounds
return CGRect(
x: newBounds.origin.x,
y: newBounds.origin.y,
width: calculatedValues.width,
height: calculatedValues.height)
}

// MARK: Private
Expand Down Expand Up @@ -284,6 +280,7 @@ final public class LCLabel: UILabel {

private func refreshView() {
setNeedsDisplay()
invalidateIntrinsicContentSize()
}

/// The following functions replaces
Expand Down
75 changes: 75 additions & 0 deletions Tests/LCLabelTests/LCLabelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,81 @@ final class LCLabelTests: XCTestCase {
XCTFail(message)
}

func testWithinVerticalStackView() {
let text = """
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Aliquet bibendum enim facilisis gravida neque. Orci a scelerisque purus semper eget duis at. Viverra justo nec ultrices dui sapien eget mi proin. Etiam non quam lacus suspendisse faucibus. Vel fringilla est ullamcorper eget nulla facilisi etiam. Donec enim diam vulputate ut pharetra sit amet aliquam id. Ipsum faucibus vitae aliquet nec ullamcorper sit amet risus. Ultrices gravida dictum fusce ut. Nulla aliquet porttitor lacus luctus accumsan tortor posuere ac. Turpis egestas sed tempus urna et pharetra. Pellentesque nec nam aliquam sem et tortor consequat. Risus sed vulputate odio ut enim blandit volutpat maecenas. Ullamcorper velit sed ullamcorper morbi tincidunt ornare massa. Blandit massa enim nec dui nunc mattis enim ut. Tristique sollicitudin nibh sit amet.
"""
let attStr = NSMutableAttributedString(
string: "LCLabel",
attributes: [
.foregroundColor: UIColor.white,
.font: UIFont.systemFont(ofSize: 14),
])
attStr.append(NSAttributedString(
string: "\n@LCLabel",
attributes: [
.foregroundColor: UIColor.white,
.font: UIFont.systemFont(ofSize: 10),
.link: "lclabel://welcome",
]))
let label = LCLabel(frame: .zero)
label.centeringTextAlignment = .top
label.backgroundColor = .green
label.linkStyleValidation = .ensure
label.linkAttributes = [
.foregroundColor: UIColor.blue,
.font: UIFont.systemFont(ofSize: 12),
]
label.shouldExcludeUnderlinesFromText = true
label.numberOfLines = 3
label.attributedText = attStr

let label2 = LCLabel(frame: .zero)
label2.centeringTextAlignment = .top
label2.backgroundColor = .purple
label2.linkStyleValidation = .ensure
label2.linkAttributes = [
.foregroundColor: UIColor.red,
.font: UIFont.systemFont(ofSize: 12),
]
label2.shouldExcludeUnderlinesFromText = true
label2.numberOfLines = 0
label2.attributedText = NSAttributedString(
string: text,
attributes: [
.foregroundColor: UIColor.white,
.font: UIFont.systemFont(ofSize: 14),
])

label.setContentHuggingPriority(.required, for: .vertical)
label2.setContentCompressionResistancePriority(.required, for: .vertical)

let stackView = UIStackView(arrangedSubviews: [label, label2])
stackView.distribution = .fillProportionally
stackView.axis = .vertical
stackView.translatesAutoresizingMaskIntoConstraints = false
let view = UIView(
frame: CGRect(
x: 0,
y: 0,
width: 300,
height: 300))
view.addSubview(stackView)
view.backgroundColor = .red
NSLayoutConstraint.activate([
stackView.topAnchor.constraint(equalTo: view.topAnchor),
stackView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
stackView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
stackView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
])
let failure = verifySnapshot(
matching: view,
as: .image,
snapshotDirectory: path)
guard let message = failure else { return }
XCTFail(message)
}

func testHiddingView() {
let attStr = NSMutableAttributedString(
string: "LCLabel",
Expand Down

0 comments on commit ece857b

Please sign in to comment.