0
Answer

Pinch zoom to increase font size of a textblock in xaml

JOHN JOHNNNY

JOHN JOHNNNY

9y
845
1

Hi

I will like to add pinch zoom to the content of textblock in my app for users to increase and decrease the font size as they wish. I have written some code for it but to my amazement it's not working, this is the code below kindly guide me

Xaml

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">

            

            <ScrollViewer Grid.Row="1">
                <TextBlock TextWrapping="Wrap" Foreground="Black" >
                    The life of Enoch Adejare Adeboye is a fulfillment of the scriptures in Zechariah 4: 10 which declare, "despise not the days of small beginnings.<LineBreak/><LineBreak/>
                    

                    <TextBlock.RenderTransform>
                        <CompositeTransform x:Name="transform" />
                    </TextBlock.RenderTransform>
                    <toolkit:GestureService.GestureListener>
                        <toolkit:GestureListener PinchStarted="OnPinchStarted" PinchDelta="OnPinchDelta" DoubleTap="GestureListener_DoubleTap" DragDelta="GestureListener_DragDelta"  />
                    </toolkit:GestureService.GestureListener>
                    
                </TextBlock>
            </ScrollViewer>

        </Grid>

Xaml.cs

private void OnPinchStarted(object sender, PinchStartedGestureEventArgs e)
        {
            var text = sender as TextBlock;
            var transform = text.RenderTransform as CompositeTransform;
            angle = transform.Rotation;
            scale = transform.ScaleX;
        }

        private void OnPinchDelta(object sender, PinchGestureEventArgs e)
        {
            var text = sender as TextBlock;
            var transform = text.RenderTransform as CompositeTransform;
            transform.Rotation = angle + e.TotalAngleDelta;
            transform.ScaleX = scale * e.DistanceRatio;
            transform.ScaleY = scale * e.DistanceRatio;
        }

        private void GestureListener_DoubleTap(object sender, GestureEventArgs e)
        {
            var text = sender as TextBlock;
            var transform = text.RenderTransform as CompositeTransform;
            transform.ScaleX = transform.ScaleY = 1;
        }

        private void GestureListener_DragDelta(object sender, DragDeltaGestureEventArgs e)
        {
            var text = sender as TextBlock;
            var transform = text.RenderTransform as CompositeTransform;
            transform.TranslateX += e.HorizontalChange;
            transform.TranslateY += e.VerticalChange;
        }

        public double angle { get; set; }

        public double scale { get; set; }

Thank you in advance and reply soon