c# – Methods to Make UI Label with Electronic mail Textual content inside it clickable in .Web iOS

0
23
c# – Methods to Make UI Label with Electronic mail Textual content inside it clickable in .Web iOS


I’ve a .Web iOS software by which I’ve a Multiline UILabel with an electronic mail inside your entire textual content.

My requirement is to have a Faucet on electronic mail solely and I need to open Electronic mail App. For that I registered a Faucet and tried to do as per the place however the contact place shouldn’t be recognized correctly with beneath code.

Under is my XIB file. XIB file

non-public void OnEmailLabelTapped(UITapGestureRecognizer recognizer)
    {
        var location = recognizer.LocationInView(AlertMsg);
        var textual content = AlertMsg.Textual content;

        // Outline the e-mail deal with to search for
        var emailRange = new NSRange(textual content.IndexOf(Electronic mail), Electronic mail.Size);

        if (emailRange.Location == NSRange.NotFound)
        {
            // Electronic mail not discovered, nothing to deal with
            return;
        }

        // Create attributed string for the entire textual content and get attributes
        var attributedText = new NSMutableAttributedString(textual content);
        var font = AlertMsg.Font;

        // Get the attributes for the e-mail
        var emailAttributedText = new NSAttributedString(Electronic mail);

        // Create a Drawing Context to measure
        var textRect = attributedText.GetBoundingRect(
            new CGSize(AlertMsg.Body.Width, nfloat.MaxValue),
            NSStringDrawingOptions.UsesLineFragmentOrigin | NSStringDrawingOptions.UsesFontLeading,
            new NSStringDrawingContext());

        // Calculate Y place by checking every line
        var currentY = AlertMsg.Body.Y;
        var emailYPosition = -1.0f; // To be adjusted if discovered

        // Iterate via the textual content to search out Y place of electronic mail
        for (int i = 0; i < emailRange.Location; i++)
        {
            // Simulate getting the char's bounding field
            var charSize = new NSString(textual content.Substring(i, 1)).GetBoundingRect(
                new CGSize(nfloat.MaxValue, nfloat.MaxValue),
                NSStringDrawingOptions.UsesFontLeading | NSStringDrawingOptions.UsesLineFragmentOrigin, new UIStringAttributes { Font = AlertMsg.Font },
                new NSStringDrawingContext());

            if (currentY + charSize.Peak >= location.Y)
            {
                // If the present higher Y with top is in bounds of the faucet, set the e-mail place
                emailYPosition = (float)currentY;
                break;
            }
            // Modify the present Y place for the following character. This crude measurement assumes all characters have related top.
            currentY += charSize.Peak;
        }

        // Fantastic tune for the e-mail itself
        if (emailYPosition != -1)
        {
            // Estimate Character Width for Electronic mail
            var emailRect = new CGRect(
                AlertMsg.Body.X + emailRange.Location * (textRect.Width / textual content.Size), // Horizontal place
                emailYPosition,
                emailAttributedText.GetBoundingRect(new CGSize(nfloat.MaxValue, nfloat.MaxValue),
                NSStringDrawingOptions.UsesLineFragmentOrigin | NSStringDrawingOptions.UsesFontLeading,
                new NSStringDrawingContext()).Width,
                font.LineHeight // assume the peak is not less than line top
            );

            // Test if the faucet location is inside the e-mail bounds
            if (emailRect.Comprises(location))
            {
                // Deal with opening electronic mail consumer
                OpenEmailClient(Electronic mail);
            }
        }
    }

How can I obtain this? Can anybody information me?

Thanks upfront.

LEAVE A REPLY

Please enter your comment!
Please enter your name here