Skip to content

Commit

Permalink
fix attributed string version priority with Text component
Browse files Browse the repository at this point in the history
  • Loading branch information
lkzhao committed Dec 25, 2022
1 parent 8154655 commit ffe69c3
Showing 1 changed file with 15 additions and 15 deletions.
30 changes: 15 additions & 15 deletions Sources/UIComponent/Components/View/Text.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import UIKit

public struct Text: ViewComponent {
public let attributedText: NSAttributedString
public let attributedString: NSAttributedString
public let numberOfLines: Int
public let lineBreakMode: NSLineBreakMode

Expand All @@ -14,18 +14,18 @@ public struct Text: ViewComponent {
numberOfLines: Int = 0,
lineBreakMode: NSLineBreakMode = .byWordWrapping
) {
self.attributedText = NSAttributedString(string: String(localized: localized), attributes: [.font: font])
self.attributedString = NSAttributedString(string: String(localized: localized), attributes: [.font: font])
self.numberOfLines = numberOfLines
self.lineBreakMode = lineBreakMode
}

@available(iOS 15, *)
public init(
_ attributedText: AttributedString,
attributedString: AttributedString,
numberOfLines: Int = 0,
lineBreakMode: NSLineBreakMode = .byWordWrapping
) {
self.attributedText = NSAttributedString(attributedText)
self.attributedString = NSAttributedString(attributedString)
self.numberOfLines = numberOfLines
self.lineBreakMode = lineBreakMode
}
Expand All @@ -36,36 +36,36 @@ public struct Text: ViewComponent {
numberOfLines: Int = 0,
lineBreakMode: NSLineBreakMode = .byWordWrapping
) {
self.attributedText = NSAttributedString(string: text, attributes: [.font: font])
self.attributedString = NSAttributedString(string: text, attributes: [.font: font])
self.numberOfLines = numberOfLines
self.lineBreakMode = lineBreakMode
}

public init(
_ attributedText: NSAttributedString,
attributedString: NSAttributedString,
numberOfLines: Int = 0,
lineBreakMode: NSLineBreakMode = .byWordWrapping
) {
self.attributedText = attributedText
self.attributedString = attributedString
self.numberOfLines = numberOfLines
self.lineBreakMode = lineBreakMode
}

public func layout(_ constraint: Constraint) -> TextRenderNode {
guard numberOfLines != 0 else {
return TextRenderNode(
attributedText: attributedText,
attributedString: attributedString,
numberOfLines: numberOfLines,
lineBreakMode: lineBreakMode,
size: attributedText.boundingRect(with: constraint.maxSize, options: [.usesLineFragmentOrigin], context: nil).size.bound(to: constraint)
size: attributedString.boundingRect(with: constraint.maxSize, options: [.usesLineFragmentOrigin], context: nil).size.bound(to: constraint)
)
}
let textStorage = NSTextStorage()
let layoutManager = NSLayoutManager()
layoutManager.usesFontLeading = false
textStorage.addLayoutManager(layoutManager)

textStorage.setAttributedString(attributedText)
textStorage.setAttributedString(attributedString)
let textContainer = NSTextContainer(size: constraint.maxSize)
textContainer.lineFragmentPadding = 0
textContainer.lineBreakMode = lineBreakMode
Expand All @@ -74,7 +74,7 @@ public struct Text: ViewComponent {
layoutManager.ensureLayout(for: textContainer)
let rect = layoutManager.usedRect(for: textContainer)
return TextRenderNode(
attributedText: attributedText,
attributedString: attributedString,
numberOfLines: numberOfLines,
lineBreakMode: lineBreakMode,
size: rect.size.bound(to: constraint)
Expand All @@ -83,20 +83,20 @@ public struct Text: ViewComponent {
}

public struct TextRenderNode: ViewRenderNode {
public let attributedText: NSAttributedString
public let attributedString: NSAttributedString
public let numberOfLines: Int
public let lineBreakMode: NSLineBreakMode
public let size: CGSize

public init(attributedText: NSAttributedString, numberOfLines: Int, lineBreakMode: NSLineBreakMode, size: CGSize) {
self.attributedText = attributedText
public init(attributedString: NSAttributedString, numberOfLines: Int, lineBreakMode: NSLineBreakMode, size: CGSize) {
self.attributedString = attributedString
self.numberOfLines = numberOfLines
self.lineBreakMode = lineBreakMode
self.size = size
}

public func updateView(_ label: UILabel) {
label.attributedText = attributedText
label.attributedText = attributedString
label.numberOfLines = numberOfLines
label.lineBreakMode = lineBreakMode
}
Expand Down

0 comments on commit ffe69c3

Please sign in to comment.