ios - 向 UIlabel 添加超链接
问题描述
我正在学习移动应用程序开发,我正在尝试向标签添加链接,并需要一些指导,这是标签的代码
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",
};
解决方案
我认为您需要 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);
}
您可以轻松地将 a 添加UITapGestureRecognizer
到标签并在用户单击它时处理事件。请记住将标签设置UserInteractionEnabled
为 true。
如果您希望整个文本都是可点击的,您可以使用按钮来代替。
如果您希望部分文本是可点击的,您应该使用,您可以在此线程NSMutableAttributedString
中查看答案,如果您需要,我可以帮助您将解决方案翻译成 C#。
推荐阅读
- ios-simulator - 录制模拟器屏幕时忽略蒙版
- jmx - 使用 Zabbix 进行 Apache Artemis 队列监控
- android - Flutter如何打开SD卡目录选择文件?
- javascript - 如何将 reduce-reducers 与 combineReducers 一起使用
- java - 来自响应的 TextView
- java - 在导航栏中分享一切都很完美,但在 Whatsapp 中它只显示链接而不是文本
- c# - 如何将多个 SQL 命令合二为一?
- vba - 如何快速识别Word中的空白表格列?
- node.js - 如何使用 MS Team 机器人在最终用户/客户端机器上运行命令/代码
- nhibernate - NHibernate 加入 2 个表并通过集合项目到 DTO