We know that a multiline text box (Uitextview) has a URL detection function, it will highlight the content of the URL link text, click on the link will be opened using Safari.
1, let TextView support custom links
In addition to being able to open URL links in a browser, sometimes we want the links in the content to achieve some personalized functional requirements. For example, when you click "View Details", app jumps to the feature description page. When you click on the "Problem Feedback" link, you will be back to the problem feedback operation.
2, the principle of implementation:
(1) For these special links we use custom URL scheme to represent. For example: about: Representative detailed description, feedback: representative problem feedback.
(2) by splicing the nsmutableattributedstring, each with link attribute, without link attribute of the text stitching up, assign value to TextView.
(3) using the Shouldinteractwithurl method of the TextView uitextfielddelegate proxy, we can capture these custom URL scheme clicks and then perform different actions by judging the url.scheme.
3, to achieve the steps
(1) for TextView to be displayed, tick its detection links (link detection) and remove the Editable (so that it is not editable).
(2) To facilitate the use of extended Uitextview:
Extension Uitextview {
Add link text (indicates normal text when the link is blank)
Func appendlinkstring (string:string, withurlstring:string = "") {
The original text content
Let attrstring:nsmutableattributedstring = nsmutableattributedstring ()
Attrstring.appendattributedstring (Self.attributedtext)
New text content (font style with default settings)
Let attrs = [nsfontattributename:self.font!]
Let appendstring = nsmutableattributedstring (string:string, Attributes:attrs)
Determine if the link text
If withurlstring!= "" {
Let Range:nsrange = Nsmakerange (0, Appendstring.length)
Appendstring.beginediting ()
Appendstring.addattribute (Nslinkattributename, value:withurlstring, Range:range)
Appendstring.endediting ()
}
Merging new text
Attrstring.appendattributedstring (appendstring)
Set the merged text
Self.attributedtext = attrstring
}
}
(3) Sample code:
Import Uikit
Class Viewcontroller:uiviewcontroller, Uitextviewdelegate {
@IBOutlet weak var textview:uitextview!
Override Func Viewdidload () {
Super.viewdidload ()
Set the proxy for the display text box
Textview.delegate = Self
Textview.text = ""
Textview.appendlinkstring ("Welcome to the sailing song app!\n")
Textview.appendlinkstring ("(1)")
Textview.appendlinkstring ("View detailed description", withurlstring: "about:from123")
Textview.appendlinkstring ("\ n (2)")
Textview.appendlinkstring ("Question feedback", withurlstring: "feedback:from234")
}
Link Click Response method
Func TextView (Textview:uitextview, Shouldinteractwithurl Url:nsurl,
InRange characterrange:nsrange)-> Bool {
Switch Url.scheme {
Case "about":
Showalert ("About",
payload:url.resourcespecifier.stringbyremovingpercentencoding!)
Case "Feedback":
Showalert ("Feedback",
payload:url.resourcespecifier.stringbyremovingpercentencoding!)
Default
Print ("This is a normal URL")
}
return True
}
Display message
Func Showalert (tagtype:string, payload:string) {
Let Alertcontroller = Uialertcontroller (title: "Detected \ (tagtype) label",
Message:payload, Preferredstyle:. Alert)
Let cancelaction = uialertaction (title: "OK", style:. Cancel, Handler:nil)
Alertcontroller.addaction (cancelaction)
Self.presentviewcontroller (Alertcontroller, Animated:true, Completion:nil)
}
}
Original: www.hangge.com