iOS Swift动态生成的Textview链接不可点击

时间:2018-03-09 07:24:33

标签: ios swift hyperlink textview dynamically-generated

我试图用UITextView制作可点击的电话号码。我设法让他们使用在Storyboard中创建的TextView,并将它们与@IBOutlet中的TextViews链接起来,就像How to display clickable links in UITextView中提到的那样。

但我无法点击动态生成的private func createPhoneNumber(text: String) -> UITextView { let label = UITextView() label.isEditable = false label.isSelectable = true label.isScrollEnabled = false label.isUserInteractionEnabled = true label.dataDetectorTypes = UIDataDetectorTypes.phoneNumber let padding = label.textContainer.lineFragmentPadding label.textContainerInset = UIEdgeInsetsMake(0, -padding, 0, -padding) label.text = text return label } ,我无法选择只检查检测链接和行为可选,因为这些TextView是由代码而不是代码创建的在故事板中。

以下是我获取电话号码的功能之一

TextView

这些是我动态生成标签TextView和链接private func createStackView(subViews: [UIView], xpadding: CGFloat) -> UIStackView { let stackView = UIStackView(arrangedSubviews: subViews) stackView.axis = .horizontal stackView.distribution = .fillEqually stackView.alignment = .fill stackView.spacing = xpadding stackView.translatesAutoresizingMaskIntoConstraints = false return stackView } private func createLabelValue(text: String) -> UILabel { let label = UILabel() label.font = UIFont(name: label.font.fontName, size: CGFloat(14)) label.numberOfLines = 0 label.lineBreakMode = NSLineBreakMode.byWordWrapping label.text = text return label } private func createConstraints(item: Any, toItemXW: Any, toItemY: Any, YPadding: CGFloat) -> [NSLayoutConstraint] { let DEFAULT_MULTIPLIER = CGFloat(1) let DEFAULT_PADDING = CGFloat(8) let xconstraint = NSLayoutConstraint(item: item, attribute: NSLayoutAttribute.leading, relatedBy: NSLayoutRelation.equal, toItem: toItemXW, attribute: NSLayoutAttribute.leading, multiplier: DEFAULT_MULTIPLIER, constant: DEFAULT_PADDING) let yconstraint = NSLayoutConstraint(item: item, attribute: NSLayoutAttribute.top, relatedBy: NSLayoutRelation.equal, toItem: toItemY, attribute: NSLayoutAttribute.bottom, multiplier: DEFAULT_MULTIPLIER, constant: YPadding) let widthConstraint = NSLayoutConstraint(item: item, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: toItemXW, attribute: NSLayoutAttribute.width, multiplier: DEFAULT_MULTIPLIER, constant: 0) return [xconstraint, yconstraint, widthConstraint] } 将在其中的堆栈视图的其他函数。

stackview

我使用该功能创建了一个电话号码文本链接,然后添加到动态生成的水平stackview中,并将其作为列表添加到垂直@IBOutlet weak var stackview: UIStackView! var prevStackView = self.stackview let phone_label = self.createLabelValue(text: "Phone") let phone_num = self.createPhoneNumber(text: "800-199-1999") // Faculty Phone let phoneStackView = self.createStackView(subViews: [phone_label, phone], xpadding: CGFloat(8)) self.stackview.addSubview(phoneStackView) let constraintsFacultyPhone = self.createConstraints(item: phoneStackView, toItemXW: self.scroll_view, toItemY: prevStackView ?? "", YPadding: CGFloat(2)) prevStackView = phoneStackView self.scroll_view.addConstraints(constraintsFacultyPhone) 中。

<xctk:DateTimePicker Value="{Binding StartEventDatetime}" Format="Custom" FormatString="yyyy/MM/dd HH:mm" Watermark="Start Date" Foreground="White">

<i:Interaction.Behaviors>
  <businessLogic:PropertyChangeBehavior DependencyPropertyName="Value" PropertyChangedCommand="{Binding UpdateEndDatetimeCommand}"/>
</i:Interaction.Behaviors>

</xctk:DateTimePicker>

   <xctk:DateTimePicker Value="{Binding EndEventDatetime}" Format="Custom" FormatString="yyyy/MM/dd HH:mm" Watermark="End date" Foreground="White">

         <i:Interaction.Behaviors>
              <businessLogic:PropertyChangeBehavior DependencyPropertyName="Value" PropertyChangedCommand="{Binding UpdateStartDatetimeCommand}"/>
          </i:Interaction.Behaviors>

     </xctk:DateTimePicker>

0 个答案:

没有答案