question

DhruvGohil-5504 avatar image
0 Votes"
DhruvGohil-5504 asked ·

how to add hyphen when using CharacterWrap

How can I add hyphen at the end when character wraps in label
(e.g. Iabel has word called "donation")

currently it wraps like,
donati
on

I want it to be wrapped using hyphen, like
donati-
on

dotnet-xamarinforms
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

ColeXia-MSFT avatar image
0 Votes"
ColeXia-MSFT answered ·

Hello,

Welcome to Microsoft Q&A!

This could be achieved with custom renderer .

For example

iOS Solution

[assembly: ExportRenderer(typeof(Label), typeof(MyLabelRenderer))]
namespace FormsA.iOS
{
    public class MyLabelRenderer : LabelRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
        {
            base.OnElementChanged(e);

            if (Control != null)
            {
                string content = Control.Text;

                NSMutableAttributedString attributedString = new NSMutableAttributedString(content);

                NSMutableParagraphStyle paragraphStyle = new NSMutableParagraphStyle();
                paragraphStyle.HyphenationFactor = 1.0f;

                attributedString.AddAttribute(UIStringAttributeKey.ParagraphStyle, paragraphStyle, new NSRange(0, content.Length));

                Control.AttributedText = attributedString;
            }
        }

    }
}

Refer to https://stackoverflow.com/a/19414663/8187800 .


If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.