improve height calculation, render on prop changes
This commit is contained in:
@@ -15,6 +15,16 @@ class RNUITextViewChildShadow: RCTShadowView {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override func didSetProps(_ changedProps: [String]!) {
|
||||||
|
guard let superview = self.superview as? RNUITextViewShadow else {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if !YGNodeIsDirty(superview.yogaNode) {
|
||||||
|
superview.setAttributedText()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func getFontWeight() -> UIFont.Weight {
|
func getFontWeight() -> UIFont.Weight {
|
||||||
switch self.fontWeight {
|
switch self.fontWeight {
|
||||||
case "bold":
|
case "bold":
|
||||||
|
|||||||
@@ -1,11 +1,19 @@
|
|||||||
class RNUITextViewShadow: RCTShadowView {
|
class RNUITextViewShadow: RCTShadowView {
|
||||||
// Props
|
// Props
|
||||||
@objc var numberOfLines: Int = 0
|
@objc var numberOfLines: Int = 0 {
|
||||||
|
didSet {
|
||||||
|
if !YGNodeIsDirty(self.yogaNode) {
|
||||||
|
self.setAttributedText()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@objc var allowsFontScaling: Bool = true
|
@objc var allowsFontScaling: Bool = true
|
||||||
|
|
||||||
var attributedText: NSAttributedString = NSAttributedString()
|
var attributedText: NSAttributedString = NSAttributedString()
|
||||||
var frameSize: CGSize = CGSize()
|
var frameSize: CGSize = CGSize()
|
||||||
|
|
||||||
|
var lineHeight: CGFloat = 0
|
||||||
|
|
||||||
var bridge: RCTBridge
|
var bridge: RCTBridge
|
||||||
|
|
||||||
init(bridge: RCTBridge) {
|
init(bridge: RCTBridge) {
|
||||||
@@ -104,31 +112,20 @@ class RNUITextViewShadow: RCTShadowView {
|
|||||||
range: NSMakeRange(0, string.length)
|
range: NSMakeRange(0, string.length)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
self.lineHeight = child.lineHeight
|
||||||
|
|
||||||
finalAttributedString.append(string)
|
finalAttributedString.append(string)
|
||||||
}
|
}
|
||||||
|
|
||||||
self.attributedText = finalAttributedString
|
self.attributedText = finalAttributedString
|
||||||
|
self.dirtyLayout()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a YGSize based on the max width
|
// Create a YGSize based on the max width
|
||||||
func getNeededSize(maxWidth: Float) -> YGSize {
|
func getNeededSize(maxWidth: Float) -> YGSize {
|
||||||
|
|
||||||
// Create a temporary textview
|
|
||||||
let textView = UITextView()
|
|
||||||
textView.attributedText = self.attributedText
|
|
||||||
textView.textContainer.lineFragmentPadding = 0
|
|
||||||
textView.textContainer.maximumNumberOfLines = self.numberOfLines
|
|
||||||
textView.textContainerInset = .zero
|
|
||||||
textView.isScrollEnabled = false
|
|
||||||
|
|
||||||
// Get the max size for sizeThatFits
|
|
||||||
let maxSize = CGSize(width: CGFloat(maxWidth), height: CGFloat(MAXFLOAT))
|
let maxSize = CGSize(width: CGFloat(maxWidth), height: CGFloat(MAXFLOAT))
|
||||||
|
let textSize = self.attributedText.boundingRect(with: maxSize, options: .usesLineFragmentOrigin, context: nil)
|
||||||
// Get the max height
|
self.frameSize = textSize.size
|
||||||
let height = textView.sizeThatFits(maxSize).height
|
return YGSize(width: Float(textSize.width), height: Float(textSize.height))
|
||||||
|
|
||||||
// Save the frame size and return the YGSize
|
|
||||||
self.frameSize = CGSize(width: CGFloat(maxWidth), height: height)
|
|
||||||
return YGSize(width: Float(maxWidth), height: Float(height))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user