Adjust Line Height in Label

Anyone know a good way to adjust the line height in a label? I know I could make a custom renderer, but I figure someone has done this already or it's built in and I just can't find it.

Posts

  • MitchMilamMitchMilam USMember ✭✭✭

    where exactly are you using the label? Can you share some code and maybe a screen shot that shows us what you're looking for?

  • JoshuaNovakJoshuaNovak USMember ✭✭

    Basically I was trying to create something like pictured in the image below (but not Facebook), without using an image for the circle. I figured it would probably load faster if I have the platform draw the circle and just load in the center part. I have it working now in a relative layout with a boxview and and image, but obviously a boxview isn't round.

    https://cdn3.iconfinder.com/data/icons/free-social-icons/67/facebook_circle_color-512.png

  • MitchMilamMitchMilam USMember ✭✭✭

    Actually, it may be faster to have images if they are static, rather than spinning up a series of controls. Does the letter/word change?

  • JoshuaNovakJoshuaNovak USMember ✭✭

    Lol that posted to the wrong section. For the line height i'm just showing a block of text, but I need less space between the lines of text.

  • RichardGarsideRichardGarside GBMember ✭✭

    I asked this question over on Stack Overflow and got a pretty good answer.

  • AriJohannessonAriJohannesson USUniversity ✭✭

    @RichardGarside: Did you get the proposed solution to your StackOverflow question to work? Tried it but it doesn't work because ((UILabel)base.Control).Font.LineHeight is readonly and can´t be assigned to.

  • Joel.7378Joel.7378 USMember ✭✭

    @AriJohannesson - I'm in the same boat. I tried implementing the answer given on Stackoverflow, but am getting the same "property is read only" error.

  • Joel.7378Joel.7378 USMember ✭✭

    @AriJohannesson and @RichardGarside - So I found a solution that I was able to implement without the "property is read only" error. I posted it on the same Stackoverflow question, which you can find here: http://stackoverflow.com/a/36989152/422845. One note, though - for some reason it seems I'm not able to decrease the default line height, only increase it. I don't know if there's some kind of limitation there.

  • TonyDTonyD USMember ✭✭✭

    Here's my solution for iOS:

    `public class ExtendedLabelRenderer : LabelRenderer
    {

        protected override void OnElementChanged (ElementChangedEventArgs<Label> e)
        {
            base.OnElementChanged (e);
    
            var label = (ExtendedLabel)Element;
            if (label == null || Control == null)
            {
                return;
            }
    
            if (e.OldElement == null) {
                var labelString = new NSMutableAttributedString(Control.AttributedText);
    
                var paragraphStyle = new NSMutableParagraphStyle { //LineSpacing =  * Control.Font.LineHeight,
                    LineHeightMultiple = (nfloat)label.LineHeightMultiplier
                };
                var style = UIStringAttributeKey.ParagraphStyle;
                var range = new NSRange(0, labelString.Length);
    
                labelString.AddAttribute(style, paragraphStyle, range);
                Control.AttributedText = labelString;
    
            }
        }
    
    }`
    
  • OddbjornBakkeOddbjornBakke NOMember ✭✭

    I had the problem that this code writes over other attributes (like text center).

    The solution that seems to work:

    <br />            if (label == null || Control == null || string.IsNullOrEmpty(Control.Text))
                    return;
    
                var attributedString = (NSMutableAttributedString)Control.AttributedText;
    
                NSRange outRange;
                var paragraphStyle = (NSMutableParagraphStyle)attributedString.GetAttribute(UIStringAttributeKey.ParagraphStyle, 0, out outRange);
                paragraphStyle.LineSpacing = 6;
    
                var spacing = NSObject.FromObject(new nfloat(label.FontSpacing));
                attributedString.AddAttribute(UIStringAttributeKey.KerningAdjustment, spacing, outRange);
    
  • Joel.7378Joel.7378 USMember ✭✭
    edited June 2017

    @OddbjornBakke - the property FontSpacing doesn't exist for me on the label. Also, it looks like you're declaring paragraphStyle and setting LineSpacing on it...but you're not actually using it anywhere that affects the actual label.

  • tomaszBDtomaszBD Member ✭✭

    Hi all, does any of this actually work for anyone? I have a custom label that has LineHeight property. I set my custom label line height in XAML and set my FormattedText in 5 SPANs, but using the following code produces no effect on the line height / spacing (I am using OnElementChanged in the custom renderer)?

                var label = (CustomLabel)Element;
                if (label == null || Control == null || e.OldElement == null)
                {
                    return;
                }
    
                var labelString = new NSMutableAttributedString(Control.AttributedText);
    
                var paragraphStyle = new NSMutableParagraphStyle
                { //LineSpacing =  * Control.Font.LineHeight,
                    LineSpacing = (nfloat)2
                    //LineHeightMultiple = (nfloat)label.LineSpacing
                };
                var style = UIStringAttributeKey.ParagraphStyle;
                var range = new NSRange(0, labelString.Length);
    
                labelString.AddAttribute(style, paragraphStyle, range);
                Control.AttributedText = labelString;
    

    I also tried the code below, but that gives me tons of various errors I can't quite figure out (have no experience with iOS dev to be fair), eg.

    Objective-C exception thrown. Name: NSInvalidArgumentException Reason: -[__NSCFConstantString lineBreakMode]: unrecognized selector sent to instance

                var label = (CustomLabel)Element;
                if (label == null || Control == null)
                {
                    return;
                }
    
                var paragraphStyle = new NSMutableParagraphStyle
                {
                    LineSpacing = (nfloat)label.LineSpacing,
                    LineBreakMode = UILineBreakMode.WordWrap,
                    LineHeightMultiple = (nfloat)label.LineSpacing
                };
                var style = UIStringAttributeKey.ParagraphStyle;
    
                if (!string.IsNullOrEmpty(label.Text)) // if label has no Text=""
                {
                    var labelString = new NSMutableAttributedString("" + label.Text);
                    var range = new NSRange(0, labelString.Length);
                    labelString.AddAttribute(style, paragraphStyle, range);
                    Control.AttributedText = labelString;    
                }
                else if (label.FormattedText.ToAttributed(label.Font, label.TextColor) is NSMutableAttributedString attrText) // label has FormattedText
                {
                    var fullRange = new NSRange(0, attrText.Length);
                    attrText.AddAttribute(UIStringAttributeKey.ParagraphStyle, style, fullRange);
                    Control.AttributedText = attrText;
                }
    

    Any thoughts?

  • tomaszBDtomaszBD Member ✭✭

    Please ignore the above, I managed to figure it all out. Can't seem to delete my last post.

  • nefgrixisnefgrixis Member ✭✭

    Did anyone figure out how to decrease the line spacing? This limitation is killing me

Sign In or Register to comment.