Forum Xamarin.Forms

How to autosize Editor height?

dtaylorusdtaylorus USMember ✭✭
edited April 2015 in Xamarin.Forms

I have an Editor control inside a Grid cell that I would like to automatically expand vertically as more text is entered.

Is this possible?

Posts

  • RafaelMouraRafaelMoura BRInsider, University, Developer Group Leader ✭✭✭

    @dtaylorus can u try anthing like this

    http://www.howlin-interactive.com/2013/01/creating-a-self-sizing-uitextview-within-a-uitableviewcell-in-ios-6/

    defaulValueVertical = entry.Height

    Get size of Font, Size of component, if Lenght of text * size of font > horizontal size of component

    vertical size += defaulValueVertical

  • RafaelMouraRafaelMoura BRInsider, University, Developer Group Leader ✭✭✭
    edited April 2015

    @dtaylorus i can't see message, it is white :confounded:

    Now I See

  • RafaelMouraRafaelMoura BRInsider, University, Developer Group Leader ✭✭✭

    @dtaylorus try test on WP and iOS :)

    Good Work

    Regards,

  • YanzhiLiYanzhiLi CNMember

    @David Taylor could you give me a full example, i used the method that you said , but throw error:
    'System.Type' does not contain a definition for 'getm' and no extension method 'getm' accepting a first argument of type 'System.Type' could be found (are you missing a using directive or an assembly reference?)

  • JohnHardmanJohnHardman GBUniversity admin

    @dtaylorus - you can avoid using Reflection (if you want) by inheriting from Editor. See http://forums.xamarin.com/discussion/21951/allow-the-editor-control-to-grow-as-content-lines-are-added#latest

    Regards,

    John H.

  • Veena.5450Veena.5450 USMember

    @dtaylorus: Can you please post the complete solution?
    the code snippet:
    public static void Invalidate(this View view)
    {
    if (view == null)
    {
    return;
    }

        var method = typeof(View).GetMethod("InvalidateMeasure", BindingFlags.Instance | BindingFlags.NonPublic);
    
        method.Invoke(view, null);
    }
    

    should this be added in platform specific code or PCL??

    I am getting Build errors in PCL project even though i am using system.reflection.

    It will be very helpful if you can provide more info.
    Thanks in advance.

  • Ondra.7099Ondra.7099 USMember
    edited November 2015
            var method = typeof(View).GetMethod("InvalidateMeasure", BindingFlags.Instance | BindingFlags.NonPublic);
    
            method.Invoke(view, null);
    

    From Xamarin.Forms 2 you need to specify parameterless method because there is a new overloaded method now. If you don't, you get an AmbiguousMatchException.

    var method = typeof(View).GetMethod ("InvalidateMeasure", BindingFlags.Instance | BindingFlags.NonPublic, null, new Type[]{ }, null);

  • @dtaylorus, @RafaelMoura How did you fix the white text on that example? I need to have a white background but the text colour defaults to white, how did you change the text colour to black? thanks in advance!

  • JulienRosenJulienRosen CAMember ✭✭✭✭

    using @AndyBarajas example my editor expands nicely, but the grid row it is contained in does not. Any solutions?

  • AndyBarajasAndyBarajas USMember ✭✭

    @JulienRosen I have never used an editor in a Grid. Does the Grid view have a defined(fixed) height? If so, remove the height restriction on it. Thats all the advice I got

  • RasmusChristensenRasmusChristensen DKUniversity ✭✭

    The editor will automatically fit its parent. You need to ensure that the parent can expand and maybe fill.

  • curtis.ehrhartcurtis.ehrhart USMember ✭✭

    @RasmusChristensen said:
    The editor will automatically fit its parent. You need to ensure that the parent can expand and maybe fill.

    unless its in a scrollview

  • TonyCaseyTonyCasey IEMember ✭✭
    edited December 2016

    Hey Guys,

    This is fab, thanks.

    One problem, it expands horizontally too and in my case, pushes my button off the screen..

  • gakonstupidgakonstupid USMember

    @TonyCasey said:
    Hey Guys,

    This is fab, thanks.

    One problem, it expands horizontally too and in my case, pushes my button off the screen..

    I get same problem as Tony on Android, if I set WidthRequest property for the container stacklayout, it will work but I must set HorizontalOptions="FillAndExpand" so I cannot fix the Width of StackLayout.

    Any guys can fix this problems, please?

  • TonyCaseyTonyCasey IEMember ✭✭

    @gakonstupid you are a genius, well done & thank you

  • milten21milten21 UAMember ✭✭

    Hey guys,

    I've used this solution and gakon's width fix in my app, it works fine, but had to limit editor's height.

    I solve it with just setting native's control maximum lines on Android by creating custom renderer.

    XF control:

        public class ExtendedEditor: Editor
        {
            public static readonly BindableProperty MaxLinesProperty =
               BindableProperty.CreateAttached(
                   "MaxLines",
                   typeof(int),
                   typeof(int),
                   1);
    
            public int MaxLines
            {
                get { return (int)GetValue(MaxLinesProperty); }
                set { SetValue(MaxLinesProperty, value); }
            }
        }
    

    Android renderer:

        public class ExtendedEditorRenderer : EditorRenderer
        {
            protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
            {
                base.OnElementPropertyChanged(sender, e);
    
                // Should be called once
                if(e.PropertyName.ToLower() == "renderer")
                    Control.SetMaxLines((Element as ExtendedEditor).MaxLines);
    
            }
        }
    

    Haven't test it on iOS.

  • PhilippWegenerPhilippWegener USMember ✭✭

    And how to decrease the Height automatically ?
    After a special Height is reached, i dont want to grow up my editor.
    When i press delete on Keyboard I want the editor to decrease.

    How to do that?

  • JassimRahmaJassimRahma USMember ✭✭✭✭
Sign In or Register to comment.