【问题标题】:swift make label clickable in UIviewcontrollerswift 在 UIviewcontroller 中使标签可点击
【发布时间】:2016-05-02 15:21:22
【问题描述】:

我希望标签可点击以在 safari 中打开 url, 点击时电子邮件中的电话号码和电子邮件地址

override func viewDidLoad() {
  super.viewDidLoad()

  // DetailsSV.contentSize.height=1120

  print("idis \(self.strUserid)")

  let ref = Firebase(url: "https://businesswallet.firebaseio.com/Users")

  ref.childByAppendingPath(self.strUserid as String).observeEventType(.Value, withBlock: { snapshot in
    if let dict = snapshot.value as? NSMutableDictionary{
      print("dict is \(dict)")
      if let Email = dict["Email"] as? String {
        self.EmailL.text = Email
      }
      if let name = dict["BusinessName"] as? String {
        self.BusinessNameL.text = name
        self.navigationItem.title = name
      }
      if let ShortDescription = dict["ShortDescription"] as? String {
        self.ShortDescriptionL.text = ShortDescription
      }
      if let City = dict["City"] as? String {
        self.CityL.text = City
      }
      if let ContactMe = dict["ContactMe"] as? String {
        self.ContactMeL.text = ContactMe
      }
      if let PhoneNumber = dict["PhoneNumber"] as? String {
        self.PhoneNumberL.text = PhoneNumber
      }
      if let Website1 = dict["Website1"] as? String {
        self.Website1L.text = Website1
      }
      if let Website2 = dict["Website2"] as? String {
        self.Website2L.text = Website2
      }
      if let Category = dict["Category"] as? String {
        self.CategoryL.text = Category
      }
      if let Details = dict["Details"] as? String {
        self.DetailsTV.text = Details
      }
    }
  })

我添加了这个功能,但它不起作用:

if let Website1 = dict["Website1"] as? String {
  self.Website1L.text = Website1
  let weblurl = NSURL(string: "open page:\(Website1)")
  UIApplication.sharedApplication().openURL(weblurl!)
}

【问题讨论】:

  • UITextView 内置支持检测和处理链接、数字、地址等。如果你关闭滚动,它基本上就是一个花哨的标签。
  • 在界面构建器中,您可以使标签突出显示地址、链接等。您甚至可以使其变得难以处理。如果文本较大,您可以使用 textView 来做同样的事情。这些对象非常强大。
  • 你为什么不直接使用UIButton?您可以将其风格化为您想要的任何外观,它带有一个TouchUpInside 处理程序。

标签: xcode swift label action uivewcontroller


【解决方案1】:

尝试使用 UITextView。如果您不希望用户能够编辑 UITextView 内的文本,您可以在 UITextView 的属性检查器中取消选中“可编辑”。如果您希望 UITextView 能够检测链接、电话号码、事件和地址等文本字符串,您可以在 UITextView 的 Attributes Inspector 中检查适用的字符串。

如果您知道只想显示一个链接,只需添加一个带有所需文本的按钮,然后将用户发送到 Safari。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多