Мое требование — иметь возможность касаться только электронной почты, и я хочу откройте приложение электронной почты. Для этого я зарегистрировал касание и попытался выполнить действия в соответствии с положением, но положение касания не идентифицируется должным образом с помощью приведенного ниже кода.
Ниже приведен мой файл XIB.

Код: Выделить всё
private void OnEmailLabelTapped(UITapGestureRecognizer recognizer)
{
var location = recognizer.LocationInView(AlertMsg);
var text = AlertMsg.Text;
// Define the email address to look for
var emailRange = new NSRange(text.IndexOf(Email), Email.Length);
if (emailRange.Location == NSRange.NotFound)
{
// Email not found, nothing to handle
return;
}
// Create attributed string for the whole text and get attributes
var attributedText = new NSMutableAttributedString(text);
var font = AlertMsg.Font;
// Get the attributes for the email
var emailAttributedText = new NSAttributedString(Email);
// Create a Drawing Context to measure
var textRect = attributedText.GetBoundingRect(
new CGSize(AlertMsg.Frame.Width, nfloat.MaxValue),
NSStringDrawingOptions.UsesLineFragmentOrigin | NSStringDrawingOptions.UsesFontLeading,
new NSStringDrawingContext());
// Calculate Y position by checking each line
var currentY = AlertMsg.Frame.Y;
var emailYPosition = -1.0f; // To be adjusted if found
// Iterate through the text to find Y position of email
for (int i = 0; i < emailRange.Location; i++)
{
// Simulate getting the char's bounding box
var charSize = new NSString(text.Substring(i, 1)).GetBoundingRect(
new CGSize(nfloat.MaxValue, nfloat.MaxValue),
NSStringDrawingOptions.UsesFontLeading | NSStringDrawingOptions.UsesLineFragmentOrigin, new UIStringAttributes { Font = AlertMsg.Font },
new NSStringDrawingContext());
if (currentY + charSize.Height >= location.Y)
{
// If the current upper Y with height is in bounds of the tap, set the email position
emailYPosition = (float)currentY;
break;
}
// Adjust the current Y position for the next character. This crude measurement assumes all characters have similar height.
currentY += charSize.Height;
}
// Fine tune for the email itself
if (emailYPosition != -1)
{
// Estimate Character Width for Email
var emailRect = new CGRect(
AlertMsg.Frame.X + emailRange.Location * (textRect.Width / text.Length), // Horizontal position
emailYPosition,
emailAttributedText.GetBoundingRect(new CGSize(nfloat.MaxValue, nfloat.MaxValue),
NSStringDrawingOptions.UsesLineFragmentOrigin | NSStringDrawingOptions.UsesFontLeading,
new NSStringDrawingContext()).Width,
font.LineHeight // assume the height is at least line height
);
// Check if the tap location is inside the email bounds
if (emailRect.Contains(location))
{
// Handle opening email client
OpenEmailClient(Email);
}
}
}
Подробнее здесь: https://stackoverflow.com/questions/790 ... in-net-ios