Files
yoga/csharp/iOS/Facebook.YogaKit.iOS/YogaLayout.cs
Rui Marinho 498a5980e8 Add YogaKit Shared and iOS version
Summary:
Using shared code for reuse in other platforms based on iOS native implementation.

Adds YogaKit sample.

Adds YogaKit tests (same as objc).

```
YogaKitTest : 80 ms
Facebook.YogaKit.iOS.Tests.exe : 81 ms
Tests run: 11 Passed: 8 Inconclusive: 0 Failed: 3 Ignored: 1
```

Since we don't have extension properties we need to go with a extension method to get access to the YogaLayout .

I m also not sure this is leak free yet, would love some help with testing and feedback about view/node lifecycle
Closes https://github.com/facebook/yoga/pull/336

Reviewed By: splhack

Differential Revision: D4415027

Pulled By: emilsjolander

fbshipit-source-id: c88328212426c3200e6f0c48cda594cd2c432065
2017-01-15 14:54:29 -08:00

53 lines
1.6 KiB
C#

using System.Collections.Generic;
using CoreGraphics;
using Facebook.Yoga;
using Foundation;
using UIKit;
namespace Facebook.YogaKit
{
public partial class YogaLayout : NSObject
{
static IEnumerable<UIView> GetChildren(UIView view) => view.Subviews;
static void MeasureNativeView(UIView view, float constrainedWidth, float constrainedHeight, out float sizeThatFitsWidth, out float sizeThatFitsHeight)
{
var sizeThatFits = view.SizeThatFits(new CGSize(constrainedWidth, constrainedHeight));
sizeThatFitsWidth = (float)sizeThatFits.Width;
sizeThatFitsHeight = (float)sizeThatFits.Height;
}
static void GetWidthHeightOfNativeView(UIView view, out float width, out float height)
{
width = (float)view.Bounds.Width;
height = (float)view.Bounds.Height;
}
static float NativePixelScale => (float)UIScreen.MainScreen.Scale;
static void ApplyLayoutToNativeView(UIView view, YogaNode node)
{
var topLeft = new CGPoint(node.LayoutX, node.LayoutY);
var bottomRight = new CGPoint(topLeft.X + node.LayoutWidth, topLeft.Y + node.LayoutHeight);
view.Frame = new CGRect(RoundPixelValue((float)topLeft.X), RoundPixelValue((float)topLeft.Y), RoundPixelValue((float)bottomRight.X) - RoundPixelValue((float)topLeft.X), RoundPixelValue((float)bottomRight.Y) - RoundPixelValue((float)topLeft.Y));
}
bool _disposed;
protected override void Dispose(bool disposing)
{
if (disposing && !_disposed)
{
_disposed = true;
if (YogaKit.Bridges.ContainsKey(_node))
{
YogaKit.Bridges.Remove(_node);
}
_node = null;
_viewRef = null;
}
base.Dispose(disposing);
}
}
}