<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
- x:Class="Xamarin.Forms.Controls.GalleryPages.CharacterSpacingGallery">
+ x:Class="Xamarin.Forms.Controls.GalleryPages.CharacterSpacingGallery"
+ xmlns:controls="clr-namespace:Xamarin.Forms.Controls">
<StackLayout>
<Label>
</Label>
<Slider x:Name="slider" Minimum="-10" Maximum="10" Value="0" ValueChanged="Slider_OnValueChanged" MaximumTrackColor="Gray"
MinimumTrackColor="Gray" Margin="20,0"/>
+ <controls:ColorPicker x:Name="textColorPicker" ColorPicked="ColorPicker_OnColorPicked" Title="Text color" />
+ <controls:ColorPicker x:Name="placeholderColorPicker" ColorPicked="ColorPicker_OnColorPicked" Title="Placeholder color" />
<ScrollView>
<StackLayout>
public CharacterSpacingGallery()
{
InitializeComponent();
+ textColorPicker.InitWithColor(Color.Red);
+ placeholderColorPicker.InitWithColor(Color.BlueViolet);
}
void Slider_OnValueChanged(object sender, ValueChangedEventArgs e)
Span.CharacterSpacing = e.NewValue;
}
+ void ColorPicker_OnColorPicked(object sender, ColorPickedEventArgs e)
+ {
+ if (sender == textColorPicker)
+ {
+ Button.TextColor = e.Color;
+ DatePicker.TextColor = e.Color;
+ Editor.TextColor = e.Color;
+ Entry.TextColor = e.Color;
+ PlaceholderEntry.TextColor = e.Color;
+ PlaceholderEditor.TextColor = e.Color;
+ Label.TextColor = e.Color;
+ Picker.TextColor = e.Color;
+ SearchBar.TextColor = e.Color;
+ PlaceholderSearchBar.TextColor = e.Color;
+ TimePicker.TextColor = e.Color;
+ Span.TextColor = e.Color;
+ }
+ else
+ {
+ PlaceholderEntry.PlaceholderColor = e.Color;
+ PlaceholderEditor.PlaceholderColor = e.Color;
+ PlaceholderSearchBar.PlaceholderColor = e.Color;
+
+ }
+ }
+
void ResetButtonClicked(object sender, EventArgs e)
{
slider.Value = 0;
+ textColorPicker.InitWithColor(Color.Red);
+ placeholderColorPicker.InitWithColor(Color.BlueViolet);
}
}
}
\ No newline at end of file
--- /dev/null
+using AppKit;
+using Foundation;
+
+namespace Xamarin.Forms.Platform.MacOS
+{
+ internal static class NSAttributedStringExtensions
+ {
+ internal static NSMutableAttributedString AddCharacterSpacing(this NSMutableAttributedString attributedString, string text, double characterSpacing)
+ {
+ if (attributedString == null || attributedString.Length == 0)
+ {
+ attributedString = text == null ? new NSMutableAttributedString() : new NSMutableAttributedString(text);
+ }
+ else
+ {
+ attributedString = new NSMutableAttributedString(attributedString);
+ }
+
+ AddKerningAdjustment(attributedString, text, characterSpacing);
+
+ return attributedString;
+ }
+
+ internal static NSMutableAttributedString AddCharacterSpacing(this NSAttributedString attributedString, string text, double characterSpacing)
+ {
+ NSMutableAttributedString mutableAttributedString;
+ if (attributedString == null || attributedString.Length == 0)
+ {
+ mutableAttributedString = text == null ? new NSMutableAttributedString() : new NSMutableAttributedString(text);
+ }
+ else
+ {
+ mutableAttributedString = new NSMutableAttributedString(attributedString);
+ }
+
+ AddKerningAdjustment(mutableAttributedString, text, characterSpacing);
+
+ return mutableAttributedString;
+ }
+
+ internal static void AddKerningAdjustment(NSMutableAttributedString mutableAttributedString, string text, double characterSpacing)
+ {
+ if (!string.IsNullOrEmpty(text))
+ {
+ mutableAttributedString.AddAttribute
+ (
+ NSStringAttributeKey.KerningAdjustment,
+ NSObject.FromObject(characterSpacing), new NSRange(0, text.Length - 1)
+ );
+ }
+ }
+ }
+}
\ No newline at end of file
}
UpdateText();
+ UpdateCharacterSpacing();
UpdateFont();
UpdateBorder();
UpdateImage();
UpdateImage();
else if (e.PropertyName == Button.PaddingProperty.PropertyName)
UpdatePadding();
+ else if (e.PropertyName == Button.CharacterSpacingProperty.PropertyName)
+ UpdateCharacterSpacing();
}
void OnButtonActivated(object sender, EventArgs eventArgs)
else
{
var textWithColor = new NSAttributedString(Element.Text ?? "", font: Element.Font.ToNSFont(), foregroundColor: color.ToNSColor(), paragraphStyle: new NSMutableParagraphStyle() { Alignment = NSTextAlignment.Center });
+ textWithColor = textWithColor.AddCharacterSpacing(Element.Text ?? string.Empty, Element.CharacterSpacing);
Control.AttributedTitle = textWithColor;
}
}
(Control as FormsNSButton)?.UpdatePadding(Element.Padding);
}
+ void UpdateCharacterSpacing()
+ {
+ Control.AttributedTitle = Control.AttributedTitle.AddCharacterSpacing(Element.Text ?? string.Empty, Element.CharacterSpacing);
+ }
+
void HandleButtonPressed()
{
Element?.SendPressed();
<Compile Include="..\Xamarin.Forms.Platform.iOS\Extensions\FontExtensions.Shared.cs">
<Link>Extensions\FontExtensions.Shared.cs</Link>
</Compile>
+ <Compile Include="Extensions\NSAttributedStringExtensions.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Xamarin.Forms.Platform\Xamarin.Forms.Platform.csproj">
else
newAttributedText.AddAttribute(underlineStyleKey, NSNumber.FromInt32((int)NSUnderlineStyle.Single), range);
-#if __MOBILE__
- UpdateCharacterSpacing();
-#else
+#if !__MOBILE__
Control.AttributedStringValue = newAttributedText;
#endif
+ UpdateCharacterSpacing();
_perfectSizeValid = false;
}
void UpdateCharacterSpacing()
{
-#if __MOBILE__
if (IsElementOrControlEmpty)
return;
if (Element?.TextType != TextType.Text)
return;
-
+#if __MOBILE__
var textAttr = Control.AttributedText.AddCharacterSpacing(Element.Text, Element.CharacterSpacing);
if (textAttr != null)
Control.AttributedText = textAttr;
-
- _perfectSizeValid = false;
+#else
+ var textAttr = Control.AttributedStringValue.AddCharacterSpacing(Element.Text, Element.CharacterSpacing);
+
+ if (textAttr != null)
+ Control.AttributedStringValue = textAttr;
#endif
+
+ _perfectSizeValid = false;
}
void UpdateText()
#if __MOBILE__
Control.TextColor = textColor.ToUIColor(ColorExtensions.Black);
#else
- Control.TextColor = textColor.ToNSColor(ColorExtensions.Black);
+ var alignment = Element.HorizontalTextAlignment.ToNativeTextAlignment(((IVisualElementController)Element).EffectiveFlowDirection);
+ var textWithColor = new NSAttributedString(Element.Text ?? "", font: Element.ToNSFont(), foregroundColor: textColor.ToNSColor(), paragraphStyle: new NSMutableParagraphStyle() { Alignment = alignment });
+ textWithColor = textWithColor.AddCharacterSpacing(Element.Text ?? string.Empty, Element.CharacterSpacing);
+ Control.AttributedStringValue = textWithColor;
#endif
UpdateLayout();
}