Skip to content
This repository has been archived by the owner on Apr 2, 2022. It is now read-only.

Add support for Label Styles V1 like #13

Merged
merged 2 commits into from
Nov 27, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions samples/Xamarin.Forms/CounterApp/CounterApp/App.fs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ module App =
let init () =
initModel (), []

let createStyleFor<'T when 'T :> BindableObject> setters =
let style = Style(typeof<'T>)
for property, value in setters do
style.Setters.Add(Setter(Property = property, Value = value))
style

let update msg model =
match msg with
| Increment -> { model with Count = model.Count + model.Step }, []
Expand All @@ -57,6 +63,10 @@ module App =
Label(string model.Count)
.automationId("CountLabel")
.centerTextHorizontal()
.style (
createStyleFor [ Label.TextColorProperty, box Color.Green
Label.FontSizeProperty, box 24. ]
)

Button("Increment", Increment)
.automationId("IncrementButton")
Expand Down
6 changes: 5 additions & 1 deletion src/Fabulous.XamarinForms/WidgetExtensions.fs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,11 @@ type AdditionalViewExtensions =
[<Extension>]
static member inline centerTextHorizontal(this: Label<_>) =
this.AddScalarAttribute(Label.HorizontalTextAlignment.WithValue(TextAlignment.Center))


[<Extension>]
static member inline style(this: Label<'msg>, style: Xamarin.Forms.Style) =
this.AddScalarAttribute(Label.Style.WithValue(style))

[<Extension>]
static member inline centerTextVertical(this: Label<_>) =
this.AddScalarAttribute(Label.VerticalTextAlignment.WithValue(TextAlignment.Center))
Expand Down
1 change: 1 addition & 0 deletions src/Fabulous.XamarinForms/Xamarin.Forms.Core.Attributes.fs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ module View =

module Label =
let Text = Attributes.defineBindable<string> Xamarin.Forms.Label.TextProperty
let Style = Attributes.defineBindable<Style> Xamarin.Forms.Label.StyleProperty
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The StyleProperty is actually coming from Xamarin.Forms.NavigableElement, you can put it in its own module module NavigableElement.
This way your extension can be applied to IViewWidgetBuilder and be available to all controls :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review. Updated following your suggestions :)

let HorizontalTextAlignment = Attributes.defineBindable<TextAlignment> Xamarin.Forms.Label.HorizontalTextAlignmentProperty
let VerticalTextAlignment = Attributes.defineBindable<TextAlignment> Xamarin.Forms.Label.VerticalTextAlignmentProperty
let FontSize = Attributes.defineBindable<double> Xamarin.Forms.Label.FontSizeProperty
Expand Down