我正在学习移动应用程序开发,并且尝试向标签添加链接,并且需要一些指导,这是标签的代码
var LinkLabel = new UILabel()
{
Frame = new CGRect(20, 170, View.Bounds.Width - 40, 40),
TextColor = UIColor.Blue,
TextAlignment = UITextAlignment.Center,
Font = UIFont.SystemFontOfSize(24),
Text = "Label",
};
答案 0 :(得分:0)
我认为您需要Xamarin.iOS中的代码:
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
// Perform any additional setup after loading the view, typically from a nib.
var LinkLabel = new UILabel()
{
Frame = new CGRect(20, 170, View.Bounds.Width - 40, 40),
TextColor = UIColor.Blue,
TextAlignment = UITextAlignment.Center,
Font = UIFont.SystemFontOfSize(24),
Text = "Label",
UserInteractionEnabled = true
};
Add(LinkLabel);
Action action = () => {
UIApplication.SharedApplication.OpenUrl(new NSUrl("https://www.google.com"));
};
UITapGestureRecognizer tapGes = new UITapGestureRecognizer(action);
LinkLabel.AddGestureRecognizer(tapGes);
}
您可以轻松地在标签上添加UITapGestureRecognizer
并在用户单击该事件时对其进行处理。记住将标签的UserInteractionEnabled
设置为true。
如果您希望整个文本都可单击,则可以使用a button。
如果您希望部分文本可单击,则应使用NSMutableAttributedString
,可以在this thread中查看答案,如果需要,我可以帮助您将解决方案转换为C#。
答案 1 :(得分:-1)
使用此代码,您可以将操作添加到标签
let tap = UITapGestureRecognizer(target: self, action: #selector(yourAction(sender:)))
LinkLabel.addGestureRecognizer(tap)
@objc func yourAction(sender:UITapGestureRecognizer) {
let alert = UIAlertController(title: titleStr, message: messageStr, preferredStyle: UIAlertController.Style.alert)
let action = UIAlertAction(title: "link", style: .default, handler: nil)
alert.addAction(action)
self.present(alert, animated: true, completion: nil)
}
在处理程序中说nil是因为当您单击警报控制器时,该操作将关闭视图