diff --git a/Submodules/UIKit/include/CATextLayer.h b/Submodules/UIKit/include/CATextLayer.h index ad26f41..ee36741 100644 --- a/Submodules/UIKit/include/CATextLayer.h +++ b/Submodules/UIKit/include/CATextLayer.h @@ -42,6 +42,8 @@ class CATextLayer: public CALayer { [[nodiscard]] NSTextAlignment textAlignment() const { return _textAlignment; } std::optional value(std::string forKeyPath) override; + + NXSize sizeThatFits(NXSize size); protected: void update(std::shared_ptr presentation, std::shared_ptr animation, float progress) override; diff --git a/Submodules/UIKit/include/UIImageView.h b/Submodules/UIKit/include/UIImageView.h index 435dd34..c5c46ce 100644 --- a/Submodules/UIKit/include/UIImageView.h +++ b/Submodules/UIKit/include/UIImageView.h @@ -16,7 +16,7 @@ class UIImageView: public UIView { std::shared_ptr image() { return _image; } // void sizeToFit() override; - // Size sizeThatFits(Size size) override; + NXSize sizeThatFits(NXSize size) override; // bool applyXMLAttribute(std::string name, std::string value) override; private: @@ -24,4 +24,4 @@ class UIImageView: public UIView { void updateTextureFromImage(); }; -} \ No newline at end of file +} diff --git a/Submodules/UIKit/include/UILabel.h b/Submodules/UIKit/include/UILabel.h index 695f4fd..63d9847 100644 --- a/Submodules/UIKit/include/UILabel.h +++ b/Submodules/UIKit/include/UILabel.h @@ -25,6 +25,8 @@ class UILabel: public UIView { void setFontWeight(NXFloat fontWeight) { _textLayer()->setFontWeight(fontWeight); } [[nodiscard]] NXFloat fontWeight() const { return _textLayer()->fontWeight(); } + NXSize sizeThatFits(NXSize size) override; + private: int _numberOfLines = 1; std::shared_ptr _textLayer() const; diff --git a/Submodules/UIKit/include/UIView.h b/Submodules/UIKit/include/UIView.h index daf102b..19674de 100644 --- a/Submodules/UIKit/include/UIView.h +++ b/Submodules/UIKit/include/UIView.h @@ -66,7 +66,7 @@ class UIView: public CALayerDelegate, public enable_shared_from_this { virtual void layoutSubviews(); virtual NXSize sizeThatFits(NXSize size); - virtual void sizeToFit(); + void sizeToFit(); void drawAndLayoutTreeIfNeeded(); diff --git a/Submodules/UIKit/lib/CATextLayer.cpp b/Submodules/UIKit/lib/CATextLayer.cpp index 5b07d7d..469ce06 100644 --- a/Submodules/UIKit/lib/CATextLayer.cpp +++ b/Submodules/UIKit/lib/CATextLayer.cpp @@ -154,3 +154,11 @@ void CATextLayer::update(std::shared_ptr presentation, std::shared_ptr< CALayer::update(presentation, animation, progress); } + +NXSize CATextLayer::sizeThatFits(NXSize size) { + paragraph->layout(size.width); + auto height = paragraph->getHeight(); + auto width = paragraph->getMaxIntrinsicWidth(); + auto rWidth = std::ceil(width); + return { rWidth, height }; +} diff --git a/Submodules/UIKit/lib/UIImageView.cpp b/Submodules/UIKit/lib/UIImageView.cpp index 64f5178..8304f9f 100644 --- a/Submodules/UIKit/lib/UIImageView.cpp +++ b/Submodules/UIKit/lib/UIImageView.cpp @@ -41,10 +41,10 @@ void UIImageView::updateTextureFromImage() { // UIView::sizeToFit(); // } -// Size UIImageView::sizeThatFits(Size size) { -// if (!_image) return Size(); -// return _image->size(); -// } + NXSize UIImageView::sizeThatFits(NXSize size) { + if (!_image) return UIView::sizeThatFits(size); + return _image->size(); + } // bool UIImageView::applyXMLAttribute(std::string name, std::string value) { // if (UIView::applyXMLAttribute(name, value)) return true; diff --git a/Submodules/UIKit/lib/UILabel.cpp b/Submodules/UIKit/lib/UILabel.cpp index 438f47f..8077bab 100644 --- a/Submodules/UIKit/lib/UILabel.cpp +++ b/Submodules/UIKit/lib/UILabel.cpp @@ -7,3 +7,7 @@ UILabel::UILabel(): UIView(NXRect(), new_shared()) {} std::shared_ptr UILabel::_textLayer() const { return std::static_pointer_cast(layer()); } + +NXSize UILabel::sizeThatFits(NXSize size) { + return _textLayer()->sizeThatFits(size); +} diff --git a/Submodules/UIKit/lib/UIView.cpp b/Submodules/UIKit/lib/UIView.cpp index 2600b7b..668b084 100644 --- a/Submodules/UIKit/lib/UIView.cpp +++ b/Submodules/UIKit/lib/UIView.cpp @@ -320,7 +320,13 @@ NXSize UIView::sizeThatFits(NXSize size) { } void UIView::sizeToFit() { - auto bounds = this->bounds(); + NXRect bounds; + if (!superview().expired()) { + bounds = this->superview().lock()->bounds(); + } else { + bounds = this->bounds(); + } + bounds.size = sizeThatFits(bounds.size); setBounds(bounds); } diff --git a/app/Screens/TestViewController/TestViewController.cpp b/app/Screens/TestViewController/TestViewController.cpp index 0ac263f..b8dd7b7 100644 --- a/app/Screens/TestViewController/TestViewController.cpp +++ b/app/Screens/TestViewController/TestViewController.cpp @@ -104,6 +104,12 @@ void TestViewController::loadView() { bottomBar->setBackgroundColor(UIColor::gray); rootView->addSubview(bottomBar); + label2 = new_shared(); + label2->setText("Test text\nTry to fit me!!!"); + label2->setBackgroundColor(UIColor::cyan); + label2->setFrame({ 100, 200, 0, 0 }); + rootView->addSubview(label2); + setView(rootView); } @@ -115,6 +121,8 @@ void TestViewController::viewDidLoad() { void TestViewController::viewDidLayoutSubviews() { UIViewController::viewDidLayoutSubviews(); + label2->sizeToFit(); + auto frame = view()->frame(); NXFloat bottomBarHeight = 48 + 35; //83; bottomBar->setFrame({ 0, frame.size.height - bottomBarHeight, frame.size.width, bottomBarHeight }); diff --git a/app/Screens/TestViewController/TestViewController.hpp b/app/Screens/TestViewController/TestViewController.hpp index 020c5ce..4c335be 100644 --- a/app/Screens/TestViewController/TestViewController.hpp +++ b/app/Screens/TestViewController/TestViewController.hpp @@ -11,5 +11,6 @@ class TestViewController: public NXKit::UIViewController { void viewDidLayoutSubviews() override; private: std::shared_ptr label; + std::shared_ptr label2; std::shared_ptr bottomBar; };