mirror of
https://github.com/Nioux/AideDeJeu.git
synced 2025-10-29 14:35:45 +00:00
FormatedString
This commit is contained in:
parent
8089e3afb0
commit
aea9b24493
4 changed files with 195 additions and 7 deletions
|
|
@ -101,6 +101,12 @@
|
|||
<On Platform="UWP, WinRT, WinPhone" Value="Assets/Fonts/LinLibertine_RI.ttf#Linux Libertine" />
|
||||
</OnPlatform>
|
||||
|
||||
<OnPlatform x:Key="LinuxLibertineBoldItal" x:TypeArguments="x:String">
|
||||
<On Platform="iOS" Value="Linux Libertine" />
|
||||
<On Platform="Android" Value="LinLibertine_RBI.ttf#Linux Libertine" />
|
||||
<On Platform="UWP, WinRT, WinPhone" Value="Assets/Fonts/LinLibertine_RBI.ttf#Linux Libertine" />
|
||||
</OnPlatform>
|
||||
|
||||
<Style TargetType="Label">
|
||||
<Setter Property="FontFamily" Value="{DynamicResource LinuxLibertine}" />
|
||||
</Style>
|
||||
|
|
@ -141,6 +147,12 @@
|
|||
<Setter Property="FontFamily" Value="{DynamicResource LinuxLibertineItal}" />
|
||||
</Style>
|
||||
|
||||
<Style TargetType="Label" x:Key="contentboldital">
|
||||
<Setter Property="FontSize" Value="Medium" />
|
||||
<Setter Property="TextColor" Value="{StaticResource contentblack}" />
|
||||
<Setter Property="FontFamily" Value="{DynamicResource LinuxLibertineBoldItal}" />
|
||||
</Style>
|
||||
|
||||
<Style TargetType="NavigationPage">
|
||||
<Setter Property="BarBackgroundColor" Value="{StaticResource titlered}" />
|
||||
<Setter Property="BarTextColor" Value="{StaticResource bgtan}" />
|
||||
|
|
|
|||
|
|
@ -1,9 +1,12 @@
|
|||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using AideDeJeu.Models;
|
||||
using AideDeJeuLib;
|
||||
using HtmlAgilityPack;
|
||||
using Xamarin.Forms;
|
||||
using Xamarin.Forms.Internals;
|
||||
|
||||
namespace AideDeJeu.ViewModels
|
||||
{
|
||||
|
|
@ -13,9 +16,58 @@ namespace AideDeJeu.ViewModels
|
|||
public Spell Item
|
||||
{
|
||||
get { return _Item; }
|
||||
set { SetProperty(ref _Item, value); }
|
||||
set { SetProperty(ref _Item, value); OnPropertyChanged(nameof(Description)); }
|
||||
}
|
||||
|
||||
public FormattedString Description
|
||||
{
|
||||
get
|
||||
{
|
||||
var fs = new FormattedString();
|
||||
if (Item?.DescriptionDiv != null)
|
||||
{
|
||||
HtmlToFormatedString(Item?.DescriptionDiv, fs, FontAttributes.None);
|
||||
}
|
||||
return fs;
|
||||
}
|
||||
}
|
||||
|
||||
void HtmlToFormatedString(HtmlNode parentNode, FormattedString fs, FontAttributes attributes)
|
||||
{
|
||||
foreach (var node in parentNode.ChildNodes)
|
||||
{
|
||||
if (node.NodeType == HtmlNodeType.Text)
|
||||
{
|
||||
var resname = "content";
|
||||
if (attributes.HasFlag(FontAttributes.Bold))
|
||||
{
|
||||
resname += "bold";
|
||||
}
|
||||
if (attributes.HasFlag(FontAttributes.Italic))
|
||||
{
|
||||
resname += "ital";
|
||||
}
|
||||
var fd = FontData.FromResource(resname);
|
||||
fs.Spans.Add(new Span() { FontFamily = fd.FontFamily, FontAttributes = attributes | fd.FontAttributes, FontSize = fd.FontSize, ForegroundColor = fd.TextColor, Text = node.InnerText });
|
||||
}
|
||||
else if (node.NodeType == HtmlNodeType.Element && node.Name == "br")
|
||||
{
|
||||
fs.Spans.Add(new Span() { Text = "\r\n" });
|
||||
}
|
||||
else if (node.NodeType == HtmlNodeType.Element && node.Name == "strong")
|
||||
{
|
||||
HtmlToFormatedString(node, fs, attributes | FontAttributes.Bold);
|
||||
}
|
||||
else if (node.NodeType == HtmlNodeType.Element && node.Name == "em")
|
||||
{
|
||||
HtmlToFormatedString(node, fs, attributes | FontAttributes.Italic);
|
||||
}
|
||||
else if (node.NodeType == HtmlNodeType.Element)
|
||||
{
|
||||
HtmlToFormatedString(node, fs, attributes);
|
||||
}
|
||||
}
|
||||
}
|
||||
public Command LoadItemCommand { get; set; }
|
||||
|
||||
public SpellDetailViewModel(Spell item = null)
|
||||
|
|
@ -51,4 +103,108 @@ namespace AideDeJeu.ViewModels
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public class FontData
|
||||
{
|
||||
public double FontSize { get; set; }
|
||||
public FontAttributes FontAttributes { get; set; }
|
||||
public Color TextColor { get; set; }
|
||||
public string FontFamily { get; set; }
|
||||
|
||||
public static FontData DefaultValues()
|
||||
{
|
||||
return new FontData
|
||||
{
|
||||
FontSize = (double)Label.FontSizeProperty.DefaultValue,
|
||||
FontAttributes = (FontAttributes)Label.FontAttributesProperty.DefaultValue,
|
||||
TextColor = (Color)Label.TextColorProperty.DefaultValue,
|
||||
FontFamily = Label.FontFamilyProperty.DefaultValue.ToString()
|
||||
};
|
||||
}
|
||||
|
||||
public static FontData FromResource(string resourceName)
|
||||
{
|
||||
var resource = Application.Current.Resources[resourceName];
|
||||
if (resource == null)
|
||||
{
|
||||
return DefaultValues();
|
||||
}
|
||||
var style = (Style)resource;
|
||||
|
||||
var data = new FontData();
|
||||
var colorSetter = style.Setters.FirstOrDefault(x => x.Property == Label.TextColorProperty);
|
||||
var attrSetter = style.Setters.FirstOrDefault(x => x.Property == Label.FontAttributesProperty);
|
||||
var fontSizeSetter = style.Setters.FirstOrDefault(x => x.Property == Label.FontSizeProperty);
|
||||
var fontFamilySetter = style.Setters.FirstOrDefault(x => x.Property == Label.FontFamilyProperty);
|
||||
var platformFontFamilySetter = ResolveSetterToClass<string>(fontFamilySetter);
|
||||
var platformColorSetter = ResolveSetterToStruct<Color>(colorSetter);
|
||||
|
||||
if (platformColorSetter.HasValue)
|
||||
{
|
||||
data.TextColor = platformColorSetter.Value;
|
||||
}
|
||||
else
|
||||
{
|
||||
data.TextColor = colorSetter?.Value as Color? ?? (Color)Label.TextColorProperty.DefaultValue;
|
||||
}
|
||||
data.FontSize = fontSizeSetter?.Value as double? ?? (double)Label.FontSizeProperty.DefaultValue;
|
||||
|
||||
|
||||
if (platformFontFamilySetter != null)
|
||||
{
|
||||
data.FontFamily = platformFontFamilySetter;
|
||||
}
|
||||
else
|
||||
{
|
||||
data.FontFamily = fontFamilySetter != null && fontFamilySetter.Value != null
|
||||
? fontFamilySetter.Value.ToString()
|
||||
: Label.FontFamilyProperty.DefaultValue?.ToString();
|
||||
}
|
||||
|
||||
data.FontAttributes = attrSetter?.Value != null
|
||||
? (FontAttributes)Enum.Parse(typeof(FontAttributes), attrSetter.Value.ToString())
|
||||
: (FontAttributes)Label.FontAttributesProperty.DefaultValue;
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
static T ResolveOnPlatformToClass<T>(string key) where T : class
|
||||
{
|
||||
var resource = Application.Current.Resources[key];
|
||||
return resource as OnPlatform<T>;
|
||||
}
|
||||
static T ResolveSetterToClass<T>(Setter setter) where T : class
|
||||
{
|
||||
if (setter?.Value is DynamicResource)
|
||||
{
|
||||
var res = setter.Value as DynamicResource;
|
||||
return ResolveOnPlatformToClass<T>(res.Key);
|
||||
}
|
||||
else
|
||||
{
|
||||
return setter?.Value as T;
|
||||
}
|
||||
}
|
||||
|
||||
static Nullable<T> ResolveOnPlatformToStruct<T>(string key) where T : struct
|
||||
{
|
||||
var resource = Application.Current.Resources[key];
|
||||
return resource as OnPlatform<Nullable<T>>;
|
||||
}
|
||||
static Nullable<T> ResolveSetterToStruct<T>(Setter setter) where T : struct
|
||||
{
|
||||
if(setter?.Value is DynamicResource)
|
||||
{
|
||||
var res = setter.Value as DynamicResource;
|
||||
return ResolveOnPlatformToStruct<T>(res.Key);
|
||||
}
|
||||
else
|
||||
{
|
||||
return setter?.Value as T?;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,29 +15,29 @@
|
|||
</StackLayout>
|
||||
|
||||
<StackLayout Orientation="Horizontal">
|
||||
<Label Text="Durée d'incantation :" Style="{StaticResource Key=contentbold}" />
|
||||
<Label Text="Durée d'incantation :" LineBreakMode="NoWrap" Style="{StaticResource Key=contentbold}" />
|
||||
<Label Text="{Binding Item.CastingTime}" Style="{StaticResource Key=content}" />
|
||||
</StackLayout>
|
||||
|
||||
|
||||
<StackLayout Orientation="Horizontal">
|
||||
<Label Text="Portée :" Style="{StaticResource Key=contentbold}" />
|
||||
<Label Text="Portée :" LineBreakMode="NoWrap" Style="{StaticResource Key=contentbold}" />
|
||||
<Label Text="{Binding Item.Range}" Style="{StaticResource Key=content}" />
|
||||
</StackLayout>
|
||||
|
||||
<StackLayout Orientation="Horizontal">
|
||||
<Label Text="Composantes :" Style="{StaticResource Key=contentbold}" />
|
||||
<Label Text="Composantes :" LineBreakMode="NoWrap" Style="{StaticResource Key=contentbold}" />
|
||||
<Label Text="{Binding Item.Components}" Style="{StaticResource Key=content}" />
|
||||
</StackLayout>
|
||||
|
||||
<StackLayout Orientation="Horizontal">
|
||||
<Label Text="Durée :" Style="{StaticResource Key=contentbold}" />
|
||||
<Label Text="Durée :" LineBreakMode="NoWrap" Style="{StaticResource Key=contentbold}" />
|
||||
<Label Text="{Binding Item.Duration}" Style="{StaticResource Key=content}" />
|
||||
</StackLayout>
|
||||
|
||||
<Label Text=" " />
|
||||
|
||||
<Label Text="{Binding Item.Description}" Style="{StaticResource Key=content}" />
|
||||
<Label FormattedText="{Binding Description}" Style="{StaticResource Key=content}" />
|
||||
|
||||
<Label Text="{Binding Item.Source}" Style="{StaticResource Key=content}" />
|
||||
</StackLayout>
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace AideDeJeuLib
|
||||
{
|
||||
|
|
@ -20,7 +21,26 @@ namespace AideDeJeuLib
|
|||
public string Components { get; set; }
|
||||
public string Duration { get; set; }
|
||||
public string DescriptionHtml { get { return DescriptionDiv?.InnerHtml; } }
|
||||
public string Description { get { return DescriptionDiv?.InnerHtml?.Replace("<br>", "\r\n")?.Replace("<strong>","")?.Replace("</strong>", "")?.Replace("<em>", "")?.Replace("</em>", ""); } }
|
||||
public string Description
|
||||
{
|
||||
get
|
||||
{
|
||||
string html = DescriptionDiv?.InnerText?.Replace("\n", "\r\n\r\n");
|
||||
//string html = DescriptionDiv?.InnerHtml;
|
||||
//html = html?.Replace("<br>", "\r\n");
|
||||
//html = html?.Replace("<strong>", "");
|
||||
//html = html?.Replace("</strong>", "");
|
||||
//html = html?.Replace("<em>", "");
|
||||
//html = html?.Replace("</em>", "");
|
||||
//if (html != null)
|
||||
//{
|
||||
// var regex = new Regex("<a href=.*?>");
|
||||
// html = regex.Replace(html, "");
|
||||
//}
|
||||
//html = html?.Replace("</a>", "");
|
||||
return html;
|
||||
}
|
||||
}
|
||||
public HtmlNode DescriptionDiv { get; set; }
|
||||
public string Overflow { get; set; }
|
||||
public string NoOverflow { get; set; }
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue