Make image clickable in UITextView - ios

I am trying to create a notetaking app where you can click on images as well as edit text normally.
Currently, I have placed a tap gesture recognizer over the UITextView which calls:
#IBAction func onImageTap(_ sender: UITapGestureRecognizer) {
if sender.state == .ended {
let textView = sender.view as! UITextView
let layoutManager = textView.layoutManager
// location of tap in textView coordinates
var location = sender.location(in: textView)
location.x -= textView.textContainerInset.left;
location.y -= textView.textContainerInset.top;
// character index at tap location
let characterIndex = layoutManager.characterIndex(for: location, in: textView.textContainer, fractionOfDistanceBetweenInsertionPoints: nil)
// if index is valid
if characterIndex < textView.textStorage.length {
// check if the tap location has the image attribute
let attributeValue = textView.attributedText.attribute(NSAttributedString.Key.fileType, at: characterIndex, effectiveRange: nil) as? String
// if location does have custom attribute, extract the url of the PDF and perform segue to display PDF
if let value = attributeValue {
if value == "PDF" {
storedFileName = textView.attributedText.attribute(NSAttributedString.Key.fileName, at: characterIndex, effectiveRange: nil) as? String
performSegue(withIdentifier: "displayPDF", sender: textView)
}
} else {
textView.isEditable = true
textView.becomeFirstResponder()
}
}
}
}
This makes all of the images clickable. However, then I stop being able to click anywhere else in the UITextView to edit the text.
What do I do so that the images remain clickable but, if I click anywhere else in the UITextView, I am able to start editing the text?

Assuming you have added the image to the UITextView as an NSAttachment, as of iOS 9.0 you can check whether or not a given NSRange contains an attachment:
For instance, if the image was located at the 25th character:
let characterIndex = 25
let range = NSRange(location: characterIndex, length: 1)
// Using an IBOutlet in the current view controller called 'textView'
if textView.attributedText.containsAttachments(in: range) {
// Do some activity
}

Related

UITextInput.characterRange(at:) is off by a few pixels

After adding a tap recognizer to my UITextView subclass, I'm attempting to get the character that is being tapped:
var textRecognizer: UITapGestureRecognizer!
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
textContainer.lineFragmentPadding = 0
textContainerInset = .zero
textRecognizer = UITapGestureRecognizer(target: self, action: #selector(textTapped))
textRecognizer.numberOfTapsRequired = 1
addGestureRecognizer(textRecognizer)
}
#objc func textTapped(recognizer: UITapGestureRecognizer) {
let location = recognizer.location(in: self)
if let cRange = characterRange(at: location) {
let cPosition = offset(from: beginningOfDocument, to: cRange.start)
let cChar = text[Range(NSRange(location: cPosition, length: 1), in: text)!]
print(cChar)
}
}
Problem is that if my attributedText is "Hello world\nWelcome to Stack Overflow" and I tap on the left part of a letter, like the left side of letter f, then characterRange(at: location) returns the previous letter r instead of returning f.
From my perspective, characterRange(at:) is buggy:
If you give it a point on the left half of character at index n, it returns range (n-1, n)
If you give it a point on the right half of character at index n, it returns range (n, n+1)
If you give it a point on the left half of character at index beginningOfDocument, it returns nil
If you give it a point on the right half of character at index endOfDocument, it returns (endOfDocument, endOfDocument+1)
The discrepancy of the behaviors at the extremities of the textInput demonstrate that there is a bug somewhere.
It behaves like a sort of "cursor position at point" function, which makes it unreliable to determine which character is actually at this point: is it the character before the cursor or the character after the cursor?
closestPosition(to:) suffers from the exact same issue.
A working alternative is layoutManager.characterIndex(for:in:fractionOfDistanceBetweenInsertionPoints:). Credit to vacawama:
#objc func textTapped(recognizer: UITapGestureRecognizer) {
var location = recognizer.location(in: self)
location.x -= textContainerInset.left
location.y -= textContainerInset.top
let cPosition = layoutManager.characterIndex(for: location, in: textContainer, fractionOfDistanceBetweenInsertionPoints: nil)
let cChar = text[Range(NSRange(location: cPosition, length: 1), in: text)!]
print(cChar)
}

why when I want to disable scroll in textview the text view will going down under the keyboard in swift 3?

I have a text view in my project that I want to expand when user input more that 20 characters so I used this method
func textViewDidChange(_ textView: UITextView) {
if chatTexts.text.characters.count > 20 {
print("up to 20")
chatTexts.isScrollEnabled = false
}
and when keyboard is going up i used this to move text view to the top of keyboard
func keyboardShown(notification: NSNotification) {
if let infoKey = notification.userInfo?[UIKeyboardFrameEndUserInfoKey],
let rawFrame = (infoKey as AnyObject).cgRectValue {
let keyboardFrame = view.convert(rawFrame, from: nil)
self.heightKeyboard = keyboardFrame.size.height
levelChatViewController.sizeForOffsetKeyboard = heightKeyboard!
if levelChatViewController.sizeForOffsetKeyboard == 253.0 {
print("the size is 253.0")
animateViewMoving(up: true, moveValue: 207)
} else {
print("the size is 216.0")
animateViewMoving(up: true, moveValue: 167)
}
}
But when the user start to write some texts in textview the textview will going down under the keyboard
remember that before adding textViewDidChange every thing was ok and the textview was going to the top of the keyboard and the only problem was that doesn't scroll so I used textViewDidChange method to do that but this problem happend

Method for word determination within UITextView.text and modifying word with prefix using Swift 2.2, iOS 9.3 & Xcode 7?

Using a TextView for user message entry it is editable and selectable during user editing.
One of the buttons below the field, toggles the TextView between editing and hash-tagging mode.
When toggled to tag, the TextView has it's editable & selectable properties disabled, and I have a function to detect taps and returns the character position within the text.
I need to determine the word, if any, tapped on, and modify the word in the UITextView.text by prefixing it with a # unless it already has two hashes it which case it removes the hashes.
I'm using regular expressions for the logic.
I have not been able to find a high level method for determining the word of the character tapped on.
I have searched through the Apple's Dev. Lib. and sites like raywenderlich and Grok Swift, but cannot find the method I am sure must be there.
I could implement by testing if the current charcter is a valid word divider if not then decrement character index and test until the word boundary is determined. At which point, I return to the prior index and test for the # character, in the case it is a #, I would test the next character and in the case it is not a #, I would add the # character to the start of the word.
Is there a function within UIKit, TextKit, or a method of UITextView or NSTextStorage, that will return the word of the character tapped and NSRange of that word?
Also what would be the correct method for adding the # to the TextView's text?
[textView:shouldChangeTextInRange:replacementText or textView.textStorage:replaceCharactersInRange:withString:]
I have worked commercially on PC, PlayStation and GameBoy, but this is the first time developing an app and using the iPhone/Mac platform, so I could really use advice.
for detecting the # you need to call the code inside the delegate shouldChangeCharactersInRange
let stringprocess = stringfordetecting.text
let tok = stringprocess!.componentsSeparatedByString(" ")
for item in tok
{
let demo = String(item)
if demo.hasPrefix("#")
{
let range = (stringfordetecting.text! as NSString).rangeOfString(item)
//add code
}
else
{
//add code
}
for detecting the tapped character index add a guesture to the textview
let tapGesture = UITapGestureRecognizer(target: self, action: "textTapped:")
tapGesture.headline = indexPath
tapGesture.numberOfTapsRequired = 1
textview2.addGestureRecognizer(tapGesture)
func textTapped(recognizer: MyTapGestureRecognizer){
let textView: UITextView = recognizer.view as! UITextView
var layoutManager: NSLayoutManager = textView.layoutManager
var location: CGPoint = recognizer.locationInView(textView)
let position: CGPoint = CGPointMake(location.x, location.y)
location.x -= textview2.textContainerInset.left
location.y -= textview2.textContainerInset.top
var charIndex: Int
charIndex = layoutManager.characterIndexForPoint(location, inTextContainer: textview2.textContainer, fractionOfDistanceBetweenInsertionPoints: nil)
if charIndex < textview2.textStorage.length
{
print(charIndex)
}
}
for detecting the tapped character in a textview inside the tapguesture recogniser function
func textTapped(recognizer: MyTapGestureRecognizer){
let textView: UITextView = recognizer.view as! UITextView
var layoutManager: NSLayoutManager = textView.layoutManager
var location: CGPoint = recognizer.locationInView(textView)
let position: CGPoint = CGPointMake(location.x, location.y)
location.x -= cell.messageLabel.textContainerInset.left
location.y -= cell.messageLabel.textContainerInset.top
var charIndex: Int
charIndex = layoutManager.characterIndexForPoint(location, inTextContainer: cell.messageLabel.textContainer, fractionOfDistanceBetweenInsertionPoints: nil)
if charIndex < cell.messageLabel.textStorage.length {
let stringprocess = textview.text
let tok = stringprocess.componentsSeparatedByString(" ")
// let attributedString1 = NSMutableAttributedString(string:stringcheck as String)
for item in tok
{
let demo = String(item)
if demo.hasPrefix("#") {
let range = (stringcheck as NSString).rangeOfString(item)
var i = range.location
while i <= range.location+range.length
{
if i == charIndex
{
print(demo)
}
i++
}
}
}
}

Find correct CGect of substring using TextKit and Touch Location

I have been working on wrapping my head around the TextKit work flow for Swift for a couple of days using some of the great questions and answers here among others.
I'm trying to find the bounding rect of a substring found in a label, if the user touches that perform an action, and if not do nothing. Currently, my bounding rect is off. Any feedback is greatly appreciated! Thank you for your time.
TableView Delegate cellForRowAtIndex:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(termsCell, forIndexPath: indexPath) as! TableViewCell
let linkRangeIndex = attributedString.string.rangeOfString("link")
let linkRange = attributedString.string.NSRangeFromRange(linkRangeIndex!)
attributedString.setAttributes(linkAttributes, range: linkRange)
cell.termsLabel.attributedText = attributedString
cell.termsLabel.userInteractionEnabled = true
textContainer.lineFragmentPadding = 0.0
textContainer.lineBreakMode = cell.termsLabel.lineBreakMode
textContainer.maximumNumberOfLines = cell.termsLabel.numberOfLines
let labelSize = cell.termsLabel.bounds.size
textContainer.size = labelSize
let tapGesture = UITapGestureRecognizer(target: self, action: "handleTapOnLabel:")
cell.termsLabel.addGestureRecognizer(tapGesture)
return cell
}
}
Handle Taps on Label Method:
What I'm attempting to do is get the bounding rect box of "link" portion of the main string and check if the users touches are with in that box (then preform an action). However my bounding rect box is off. It fires (a print method for testing) when I click on the right side of "with"
#IBAction func handleTapOnLabel(tapGesture: UITapGestureRecognizer) {
let glyphPointer = NSRangePointer()
let linkRangeIndex = attributedString.string.rangeOfString("link")
let linkRange = attributedString.string.NSRangeFromRange(linkRangeIndex!)
let myString = "A string"
myString.NSRangeFromRange(linkRangeIndex!)
let glyphRange = layoutManager.characterRangeForGlyphRange(linkRange, actualGlyphRange: glyphPointer)
print("glyphRange: \(glyphRange)")
let glyphRect = layoutManager.boundingRectForGlyphRange(glyphRange, inTextContainer: textContainer)
print("glyphRect: \(glyphRect)")
let touchPoint = tapGesture.locationOfTouch(0, inView: cell?.textLabel)
print(touchPoint)
// need range of points for label
if CGRectContainsPoint(glyphRect, locationOfTouchInLabel) {
print("I go home now")
} else {
print("no")
}
}
}

How to detect touch on NSTextAttachment

What is the best way to detect when user taps on NSTextAttachment on iOS?
I think that one of the ways would be checking for the character on carret's position whether it is NSAttachmentCharacter, but it just doesn't seem right.
I've also tried UITextViewDelegate method: -(BOOL)textView:(UITextView *)textView shouldInteractWithTextAttachment:(NSTextAttachment *)textAttachment inRange:(NSRange)characterRange but it's not invoked when textView.editable=YES
Josh's answer is almost perfect. However, if you tap in the whitespace of your UITextView past the end of the input, glyphIndex(for:in:fractionOfDistanceThroughGlyph) will return the final glyph in the string. If this is your attachment, it will incorrectly evaluate to true.
Apple's docs say: If no glyph is under point, the nearest glyph is returned, where nearest is defined according to the requirements of selection by mouse. Clients who wish to determine whether the the point actually lies within the bounds of the glyph returned should follow this with a call to boundingRect(forGlyphRange:in:) and test whether the point falls in the rectangle returned by that method.
So, here is a tweaked version (Swift 5, XCode 10.2) that performs an additional check on the bounds of the detected glyph. I believe some of the characterIndex tests are now superfluous but they don't hurt anything.
One caveat: glyphs appear to extend to the height of the line containing them. If you have a tall portrait image attachment next to a landscape image attachment, taps on the whitespace above the landscape image will still evaluate to true.
import UIKit
import UIKit.UIGestureRecognizerSubclass
// Thanks to https://stackoverflow.com/a/52883387/658604
// and https://stackoverflow.com/a/49153247/658604
/// Recognizes a tap on an attachment, on a UITextView.
/// The UITextView normally only informs its delegate of a tap on an attachment if the text view is not editable, or a long tap is used.
/// If you want an editable text view, where you can short cap an attachment, you have a problem.
/// This gesture recognizer can be added to the text view, and will add requirments in order to recognize before any built-in recognizers.
class AttachmentTapGestureRecognizer: UITapGestureRecognizer {
typealias TappedAttachment = (attachment: NSTextAttachment, characterIndex: Int)
private(set) var tappedState: TappedAttachment?
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent) {
tappedState = nil
guard let textView = view as? UITextView else {
state = .failed
return
}
if let touch = touches.first {
tappedState = evaluateTouch(touch, on: textView)
}
if tappedState != nil {
// UITapGestureRecognizer can accurately differentiate discrete taps from scrolling
// Therefore, let the super view evaluate the correct state.
super.touchesBegan(touches, with: event)
} else {
// User didn't initiate a touch (tap or otherwise) on an attachment.
// Force the gesture to fail.
state = .failed
}
}
/// Tests to see if the user has tapped on a text attachment in the target text view.
private func evaluateTouch(_ touch: UITouch, on textView: UITextView) -> TappedAttachment? {
let point = touch.location(in: textView)
let glyphIndex: Int = textView.layoutManager.glyphIndex(for: point, in: textView.textContainer, fractionOfDistanceThroughGlyph: nil)
let glyphRect = textView.layoutManager.boundingRect(forGlyphRange: NSRange(location: glyphIndex, length: 1), in: textView.textContainer)
guard glyphRect.contains(point) else {
return nil
}
let characterIndex: Int = textView.layoutManager.characterIndexForGlyph(at: glyphIndex)
guard characterIndex < textView.textStorage.length else {
return nil
}
guard NSTextAttachment.character == (textView.textStorage.string as NSString).character(at: characterIndex) else {
return nil
}
guard let attachment = textView.textStorage.attribute(.attachment, at: characterIndex, effectiveRange: nil) as? NSTextAttachment else {
return nil
}
return (attachment, characterIndex)
}
}
Apple make this really difficult. As others point out, the delegate method is called, but only when isEditable is false, or when the user does a tap and hold on the attachment. If you want to be informed about a simple tap interaction during editing, forget it.
I went down the touchesBegan: and hitTest: paths, both with problems. The touches methods are called after the UITextView has already handled the interaction, and the hitTest: is too crude, because it messes with the first responder status and so forth.
My solution in the end was gesture recognizers. Apple are using those internally, which explains why touchesBegan: is not really viable in the first place: the gesture recognizers have already handled the event.
I created a new gesture recognizer class for use with a UITextView. It simply checks for the location of the tap, and if it is an attachment, it handles it. I make all the other gesture recognizers subordinate to my one, so we get first look at the events, and the others only come into play if our one fails.
The gesture recognizer class is below, along with an extension for adding it to UITextView. I add it in my UITextView subclass in awakeFromNib, like this. (You needn't use a subclass if you don't have one.)
override func awakeFromNib() {
super.awakeFromNib()
let recognizer = AttachmentTapGestureRecognizer(target: self, action: #selector(handleAttachmentTap(_:)))
add(recognizer)
and I handle the action by calling the existing UITextViewDelegate method textView(_:,shouldInteractWith:,in:,interaction:). You could just as easily put the handling code directly in the action, rather than using the delegate.
#IBAction func handleAttachmentTap(_ sender: AttachmentTapGestureRecognizer) {
let _ = delegate?.textView?(self, shouldInteractWith: sender.attachment!, in: NSRange(location: sender.attachmentCharacterIndex!, length: 1), interaction: .invokeDefaultAction)
}
Here is the main class.
import UIKit
import UIKit.UIGestureRecognizerSubclass
/// Recognizes a tap on an attachment, on a UITextView.
/// The UITextView normally only informs its delegate of a tap on an attachment if the text view is not editable, or a long tap is used.
/// If you want an editable text view, where you can short cap an attachment, you have a problem.
/// This gesture recognizer can be added to the text view, and will add requirments in order to recognize before any built-in recognizers.
class AttachmentTapGestureRecognizer: UIGestureRecognizer {
/// Character index of the attachment just tapped
private(set) var attachmentCharacterIndex: Int?
/// The attachment just tapped
private(set) var attachment: NSTextAttachment?
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent) {
attachmentCharacterIndex = nil
attachment = nil
let textView = view as! UITextView
if touches.count == 1, let touch = touches.first, touch.tapCount == 1 {
let point = touch.location(in: textView)
let glyphIndex: Int? = textView.layoutManager.glyphIndex(for: point, in: textView.textContainer, fractionOfDistanceThroughGlyph: nil)
let index: Int? = textView.layoutManager.characterIndexForGlyph(at: glyphIndex ?? 0)
if let characterIndex = index, characterIndex < textView.textStorage.length {
if NSAttachmentCharacter == (textView.textStorage.string as NSString).character(at: characterIndex) {
attachmentCharacterIndex = characterIndex
attachment = textView.textStorage.attribute(.attachment, at: characterIndex, effectiveRange: nil) as? NSTextAttachment
state = .recognized
} else {
state = .failed
}
}
} else {
state = .failed
}
}
}
extension UITextView {
/// Add an attachment recognizer to a UITTextView
func add(_ attachmentRecognizer: AttachmentTapGestureRecognizer) {
for other in gestureRecognizers ?? [] {
other.require(toFail: attachmentRecognizer)
}
addGestureRecognizer(attachmentRecognizer)
}
}
This same approach could presumably be used for taps on links.
The delegate method does work but ONLY if the attachment has an image in the image attribute AND if editable = NO! So if you have an image pasted in to the attributedString from somewhere else it seems the data ends up being stored in the fileWrapper and next time you put the attributedString back into the textView the image attribute is nil and the layout manager or whatever gets the image from the fileWrapper.
Somewhere in the documents it does mention that there are no methods in NSTextAttachment for persistence of the image attribute.
To test this try copy a photo from the Photo app and paste it into your textView, now if you hold down your finger on it you should see the default menu pop up. Now if you save this rich text, say into a Core Data entity and then retrieve it the image attribute will be nil but the image data will be in attachment.fileWrapper.regularFileContents
Its a pain, and I would love to know the engineers intention. So you have two options it seems.
Create your own custom NSTextAttachment and include methods for archiving the image and other settings (PLEASE SHOW ME HOW TOO WHEN YOU FIGURE THIS ONE OUT)
Every time prior to putting your string back into textView you find all the attachments and recreated the image attribute like so:
attachment.image = [UIImage imageWithData:attachment.fileWrapper.regularFileContents];
Bear in mind the side effect of doing this is invalidating the fileWrapper. I want to resize the image but also keep the original so I don't loose the full resolution. I think the only way of doing this might be to subclass NSTextAttachment.
EDIT:
I figured out how to create the custom NSTextAttachments - here is a link for those interested http://ossh.com.au/design-and-technology/software-development/implementing-rich-text-with-images-on-os-x-and-ios/
EDIT 2: To customise the menu when in Edit Mode see the following Apple documents, the issue is 'touchEnded' never seems to get called so you might have to try using touchesBegan. Careful you don't interfere with the default editing behaviour though.
https://developer.apple.com/library/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/AddingCustomEditMenuItems/AddingCustomEditMenuItems.html
Note that in the code below you would need to add code after // selection management comment to determine which character was touched, check if it is the special text attachment character and
then modify the edit menu or take some other action.
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *theTouch = [touches anyObject];
if ([theTouch tapCount] == 2 && [self becomeFirstResponder]) {
// selection management code goes here...
// bring up edit menu.
UIMenuController *theMenu = [UIMenuController sharedMenuController];
CGRect selectionRect = CGRectMake (currentSelection.x, currentSelection.y, SIDE, SIDE);
[theMenu setTargetRect:selectionRect inView:self];
[theMenu setMenuVisible:YES animated:YES];
}
}
Alternately you could add a custom menu by adding the menu item and then modifying the canPerformAction method.
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
LOG(#"canPerformAction: called");
if (action == #selector(viewImage)) {
// Check the selected character is the special text attachment character
return YES;
}
return NO;
}
Here is some addition code but its a bit fussy. Second method just disables the default edit menu if an attachment is detected.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
FLOG(#"touchesBegan:withEvent: called");
if (self.selectedRange.location != NSNotFound) {
FLOG(#" selected location is %d", self.selectedRange.location);
int ch;
if (self.selectedRange.location >= self.textStorage.length) {
// Get the character at the location
ch = [[[self textStorage] string] characterAtIndex:self.selectedRange.location-1];
} else {
// Get the character at the location
ch = [[[self textStorage] string] characterAtIndex:self.selectedRange.location];
}
if (ch == NSAttachmentCharacter) {
FLOG(#" selected character is %d, a TextAttachment", ch);
} else {
FLOG(#" selected character is %d", ch);
}
}
}
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
FLOG(#"canPerformAction: called");
FLOG(#" selected location is %d", self.selectedRange.location);
FLOG(#" TextAttachment character is %d", NSAttachmentCharacter);
if (self.selectedRange.location != NSNotFound) {
int ch;
if (self.selectedRange.location >= self.textStorage.length) {
// Get the character at the location
ch = [[[self textStorage] string] characterAtIndex:self.selectedRange.location-1];
} else {
// Get the character at the location
ch = [[[self textStorage] string] characterAtIndex:self.selectedRange.location];
}
if (ch == NSAttachmentCharacter) {
FLOG(#" selected character is %d, a TextAttachment", ch);
return NO;
} else {
FLOG(#" selected character is %d", ch);
}
// Check for an attachment
NSTextAttachment *attachment = [[self textStorage] attribute:NSAttachmentAttributeName atIndex:self.selectedRange.location effectiveRange:NULL];
if (attachment) {
FLOG(#" attachment attribute retrieved at location %d", self.selectedRange.location);
return NO;
}
else
FLOG(#" no attachment at location %d", self.selectedRange.location);
}
return [super canPerformAction:action withSender:sender];
}
Swift 3 answer:
func textView(_ textView: UITextView, shouldInteractWith textAttachment: NSTextAttachment, in characterRange: NSRange) -> Bool {
return true
}
Make sure your textView isEditable = false, isSelectable = true, and isUserInteractionEnabled = true. Duncan's answer did not mention isUserInteractionEnabled, this has to be true, otherwise it wont work.
You can do this programmatically (textView.isEditable = false), or via attributes inspector:
I have altered Drew's gesture recognizer here to subclass UITapGestureRecognizer rather than UIGestureRecognizer.
This offers one advantage in that it only detects discrete taps as opposed to the beginning of a scroll.
import UIKit
import UIKit.UIGestureRecognizerSubclass
// Modified from: https://stackoverflow.com/a/49153247/658604
/// Recognizes a tap on an attachment, on a UITextView.
/// The UITextView normally only informs its delegate of a tap on an attachment if the text view is not editable, or a long tap is used.
/// If you want an editable text view, where you can short cap an attachment, you have a problem.
/// This gesture recognizer can be added to the text view, and will add requirments in order to recognize before any built-in recognizers.
class AttachmentTapGestureRecognizer: UITapGestureRecognizer {
typealias TappedAttachment = (attachment: NSTextAttachment, characterIndex: Int)
private(set) var tappedState: TappedAttachment?
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent) {
tappedState = nil
guard let textView = view as? UITextView else {
state = .failed
return
}
if let touch = touches.first {
tappedState = evaluateTouch(touch, on: textView)
}
if tappedState != nil {
// UITapGestureRecognizer can accurately differentiate discrete taps from scrolling
// Therefore, let the super view evaluate the correct state.
super.touchesBegan(touches, with: event)
} else {
// User didn't initiate a touch (tap or otherwise) on an attachment.
// Force the gesture to fail.
state = .failed
}
}
/// Tests to see if the user has tapped on a text attachment in the target text view.
private func evaluateTouch(_ touch: UITouch, on textView: UITextView) -> TappedAttachment? {
let point = touch.location(in: textView)
let glyphIndex: Int? = textView.layoutManager.glyphIndex(for: point, in: textView.textContainer, fractionOfDistanceThroughGlyph: nil)
let index: Int? = textView.layoutManager.characterIndexForGlyph(at: glyphIndex ?? 0)
guard let characterIndex = index, characterIndex < textView.textStorage.length else {
return nil
}
guard NSTextAttachment.character == (textView.textStorage.string as NSString).character(at: characterIndex) else {
return nil
}
guard let attachment = textView.textStorage.attribute(.attachment, at: characterIndex, effectiveRange: nil) as? NSTextAttachment else {
return nil
}
return (attachment, characterIndex)
}
}
SWIFT 4.2
#objc func myMethodToHandleTap(_ sender: UITapGestureRecognizer) {
let myTextView = sender.view as! UITextView
let layoutManager = myTextView.layoutManager
// location of tap in myTextView coordinates and taking the inset into account
var location = sender.location(in: myTextView)
location.x -= myTextView.textContainerInset.left;
location.y -= myTextView.textContainerInset.top;
// character index at tap location
let characterIndex = layoutManager.characterIndex(for: location, in: myTextView.textContainer, fractionOfDistanceBetweenInsertionPoints: nil)
if characterIndex < myTextView.textStorage.length {
let attributeValue = myTextView.attributedText.attribute(NSAttributedString.Key.attachment, at: characterIndex, effectiveRange: nil) as? NSTextAttachment
if let _ = attributeValue {
print("TAPPED ATTACHMENT")
}
}
}
Use hitTest to get the touch in a subclassed UITextView. This avoids the issue of messing up the standard editing functions. From the position get the character index and then check the character for the attachment.
I found the common way to solve this kind of clicking attachment in attributed string is using UITextView . But the UITextView is so complicated to disable all the menu, the zoom and so on . So I make a simple version of UILabel wrapper to support attachment clicking . It's simple and easy to use&change . the demo is here:
self.attachmentLabel.attributeText = attributeText;
self.attachmentLabel.numberOflines = 2;
self.attachmentLabel.selectBlock = ^(NSInteger attachmentIndex) {
NSLog(#"attachment:%ld called",attachmentIndex);
};
https://github.com/ximmyxiao/TestAttributeStringClick
Hope it will helps:)

Resources