生活随笔
收集整理的這篇文章主要介紹了
第二十一章:变换(三)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
文字效果 TranslationX和TranslationY的一個(gè)常見應(yīng)用是將少量偏移應(yīng)用于將它們從布局位置略微偏移的元素。 如果在單單元格網(wǎng)格中有多個(gè)重疊元素并且需要移動(dòng)一個(gè)以便從另一個(gè)網(wǎng)格下面查看,這有時(shí)很有用。 您甚至可以將此技術(shù)用于常見文本效果。 僅XAML TextOffsets程序?qū)⑷龑?duì)Label元素放在三個(gè)單格網(wǎng)格布局中。 每個(gè)網(wǎng)格中的一對(duì)Label元素大小相同,并顯示相同的文本:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"x:Class="TextOffsets.TextOffsetsPage"><ContentPage.Padding><OnPlatform x:TypeArguments="Thickness"iOS="0, 20, 0, 0" /></ContentPage.Padding><ContentPage.Resources><ResourceDictionary><Color x:Key="backgroundColor">White</Color><Color x:Key="foregroundColor">Black</Color><Style TargetType="Grid"><Setter Property="VerticalOptions" Value="CenterAndExpand" /></Style><Style TargetType="Label"><Setter Property="FontSize" Value="72" /><Setter Property="FontAttributes" Value="Bold" /><Setter Property="HorizontalOptions" Value="Center" /></Style></ResourceDictionary></ContentPage.Resources><StackLayout BackgroundColor="{StaticResource backgroundColor}"><Grid><Label Text="Shadow"TextColor="{StaticResource foregroundColor}"Opacity="0.5"TranslationX="5"TranslationY="5" /><Label Text="Shadow"TextColor="{StaticResource foregroundColor}" /></Grid><Grid><Label Text="Emboss"TextColor="{StaticResource foregroundColor}"TranslationX="2"TranslationY="2" /><Label Text="Emboss"TextColor="{StaticResource backgroundColor}" /></Grid><Grid><Label Text="Engrave"TextColor="{StaticResource foregroundColor}"TranslationX="-2"TranslationY="-2" /><Label Text="Engrave"TextColor="{StaticResource backgroundColor}" /></Grid></StackLayout>
</ContentPage>
通常,網(wǎng)格的Children集合中的第一個(gè)Label將被第二個(gè)Label遮擋,但應(yīng)用于第一個(gè)Label的TranslationX和TranslationY值允許它部分可見。 相同的基本技術(shù)會(huì)產(chǎn)生三種不同的文本效果:投影,看似從屏幕表面抬起的文本,以及看起來像是鑿入屏幕的文本: 這些效果為其他平面圖像提供了一些3D外觀。 光學(xué)錯(cuò)覺基于光從左上角照亮屏幕的慣例。 因此,陰影被拋出到凸起物體的下方和右側(cè)。 浮雕和雕刻效果之間的差異完全是由于模糊的黑色文本和頂部的白色文本的相對(duì)位置。 如果黑色文本略低于右側(cè),則會(huì)成為凸起的白色文本的陰影。 如果黑色文本位于白色文本的上方和左側(cè),則它將成為沉入表面下方的文本陰影。 下一個(gè)示例不是您要定期使用的內(nèi)容,因?yàn)樗枰鄠€(gè)Label元素,但如果您想為文本提供一點(diǎn)“深度”,則BlockText程序中說明的技術(shù)非常有用: BlockText XAML文件使用單格網(wǎng)格在白色背景上顯示黑色文本。 但是,為L(zhǎng)abel定義的隱式(和擴(kuò)展)樣式指定了Gray的TextColor屬性:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"x:Class="BlockText.BlockTextPage"><Grid x:Name="grid"BackgroundColor="White"><Grid.Resources><ResourceDictionary><Style TargetType="Label"><Setter Property="Text" Value="DEPTH" /><Setter Property="FontSize" Value="72" /><Setter Property="FontAttributes" Value="Bold" /><Setter Property="TextColor" Value="Gray" /><Setter Property="HorizontalOptions" Value="Center" /><Setter Property="VerticalOptions" Value="Center" /></Style></ResourceDictionary></Grid.Resources><Label TextColor="Black" /></Grid>
</ContentPage>
代碼隱藏文件中的構(gòu)造函數(shù)向Grid添加了幾個(gè)Label元素。 Style確保它們都具有相同的屬性(包括灰色),但每個(gè)屬性都偏離XAML文件中的Label:
public partial class BlockTextPage : ContentPage
{public BlockTextPage(){InitializeComponent();for (int i = 0; i < Device.OnPlatform(12, 12, 18); i++){grid.Children.Insert(0, new Label{TranslationX = i,TranslationY = -i});}}
}
這是另一種情況,其中標(biāo)簽元素在單格網(wǎng)格中相互重疊,但現(xiàn)在還有更多。 XAML文件中的黑色標(biāo)簽必須是Children集合中的最后一個(gè)子標(biāo)簽,以便它位于所有其他子集之上。 具有最大TranslationX和TranslationY偏移量的元素必須是Children集合中的第一個(gè)子元素,因此它必須位于堆棧的最底部。 這就是為什么每個(gè)連續(xù)的Label都插入到Children集合的開頭。
總結(jié)
以上是生活随笔 為你收集整理的第二十一章:变换(三) 的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔 推薦給好友。