问题描述
我想使用Xcode 11中的“可注册链接”将其显示为可点击链接:
此情节提要中的所有按钮均已在情节提要中创建。我遇到了一个涉及编码的解决方案...
override func viewDidLoad() {
let attributedString = NSMutableAttributedString(string: "Don't have an account? Sign up.")
attributedString.addAttribute(.link,value: "https://udacity.com",range: NSRange(location: 23,length: 8))
webSignUp.attributedText = attributedString
}
func textView(_ textView: UITextView,shouldInteractWith URL: URL,in characterRange: NSRange,interaction: UITextItemInteraction) -> Bool {
UIApplication.shared.open(URL)
return false
}
...但是这会使我已经创建的按钮消失。
如何使用Xcode 11在情节提要中创建链接?还是在编码中创建一个不会干扰其他所有内容的链接?
解决方法
使用 TextEdit.app 这个管理RichText的小应用程序,可以将其用于属性字符串和故事板。
打开应用程序,编写文本,选择需要链接的文本部分。 编辑/添加链接... ,添加您的链接。
打开 Xcode.app ,在 Storyboard 中选择 UITextView ,然后从 TextEdit.app复制/粘贴文本放入 UITextView 。
由于有时 WYSIWYG 可能很奇怪,并且Xcode根本不是用户友好的,所以请右键单击情节提要,打开为/源代码 >,找到您的文字(您可以搜索key="NSLink"
,也可以只搜索您输入的文字,链接等)
您应该看到:
<textView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" fixedFrame="YES" usesAttributedText="YES" translatesAutoresizingMaskIntoConstraints="NO" id="NxY-fQ-0iH">
<rect key="frame" x="87" y="331" width="240" height="128"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<color key="backgroundColor" systemColor="systemBackgroundColor" cocoaTouchSystemColor="whiteColor"/>
<attributedString key="attributedText">
<fragment content="Don’t have an account? ">
<attributes>
<font key="NSFont" metaFont="user"/>
<paragraphStyle key="NSParagraphStyle" alignment="left" lineBreakMode="wordWrapping" baseWritingDirection="natural" tighteningFactorForTruncation="0.0" allowsDefaultTighteningForTruncation="NO">
<tabStops>
<textTab alignment="left" location="28.299999237060547">
<options/>
</textTab>
<textTab alignment="left" location="56.650001525878906">
<options/>
</textTab>
<textTab alignment="left" location="85">
<options/>
</textTab>
<textTab alignment="left" location="113.34999847412109">
<options/>
</textTab>
<textTab alignment="left" location="141.69999694824219">
<options/>
</textTab>
<textTab alignment="left" location="170.05000305175781">
<options/>
</textTab>
<textTab alignment="left" location="198.39999389648438">
<options/>
</textTab>
<textTab alignment="left" location="226.75">
<options/>
</textTab>
<textTab alignment="left" location="255.10000610351562">
<options/>
</textTab>
<textTab alignment="left" location="283.45001220703125">
<options/>
</textTab>
<textTab alignment="left" location="311.79998779296875">
<options/>
</textTab>
<textTab alignment="left" location="340.14999389648438">
<options/>
</textTab>
</tabStops>
</paragraphStyle>
</attributes>
</fragment>
<fragment content="Sign up">
<attributes>
<font key="NSFont" metaFont="user"/>
<url key="NSLink" string="http://stackoverflow.com"/>
<paragraphStyle key="NSParagraphStyle" alignment="left" lineBreakMode="wordWrapping" baseWritingDirection="natural" tighteningFactorForTruncation="0.0" allowsDefaultTighteningForTruncation="NO">
<tabStops>
<textTab alignment="left" location="28.299999237060547">
<options/>
</textTab>
<textTab alignment="left" location="56.650001525878906">
<options/>
</textTab>
<textTab alignment="left" location="85">
<options/>
</textTab>
<textTab alignment="left" location="113.34999847412109">
<options/>
</textTab>
<textTab alignment="left" location="141.69999694824219">
<options/>
</textTab>
<textTab alignment="left" location="170.05000305175781">
<options/>
</textTab>
<textTab alignment="left" location="198.39999389648438">
<options/>
</textTab>
<textTab alignment="left" location="226.75">
<options/>
</textTab>
<textTab alignment="left" location="255.10000610351562">
<options/>
</textTab>
<textTab alignment="left" location="283.45001220703125">
<options/>
</textTab>
<textTab alignment="left" location="311.79998779296875">
<options/>
</textTab>
<textTab alignment="left" location="340.14999389648438">
<options/>
</textTab>
</tabStops>
</paragraphStyle>
</attributes>
</fragment>
<fragment content=".">
<attributes>
<font key="NSFont" metaFont="user"/>
<paragraphStyle key="NSParagraphStyle" alignment="left" lineBreakMode="wordWrapping" baseWritingDirection="natural" tighteningFactorForTruncation="0.0" allowsDefaultTighteningForTruncation="NO">
<tabStops>
<textTab alignment="left" location="28.299999237060547">
<options/>
</textTab>
<textTab alignment="left" location="56.650001525878906">
<options/>
</textTab>
<textTab alignment="left" location="85">
<options/>
</textTab>
<textTab alignment="left" location="113.34999847412109">
<options/>
</textTab>
<textTab alignment="left" location="141.69999694824219">
<options/>
</textTab>
<textTab alignment="left" location="170.05000305175781">
<options/>
</textTab>
<textTab alignment="left" location="198.39999389648438">
<options/>
</textTab>
<textTab alignment="left" location="226.75">
<options/>
</textTab>
<textTab alignment="left" location="255.10000610351562">
<options/>
</textTab>
<textTab alignment="left" location="283.45001220703125">
<options/>
</textTab>
<textTab alignment="left" location="311.79998779296875">
<options/>
</textTab>
<textTab alignment="left" location="340.14999389648438">
<options/>
</textTab>
</tabStops>
</paragraphStyle>
</attributes>
</fragment>
</attributedString>
<textInputTraits key="textInputTraits" autocapitalizationType="sentences"/>
</textView>
这非常冗长,尤其是所有的tabstop / textTab,但是您可以从那里编辑上下文(例如更改链接)。