Skip to content

Instantly share code, notes, and snippets.

@scottymac
Created September 12, 2013 15:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scottymac/6539442 to your computer and use it in GitHub Desktop.
Save scottymac/6539442 to your computer and use it in GitHub Desktop.
class UITextView #(Extensions) https://gist.github.com/brentsimmons/5810992/
def stringCharacterIsAllowedAsPartOfLink(s)
if (s == nil || s.length < 1)
return false
end
ch = s.characterAtIndex(0)
if NSCharacterSet.whitespaceAndNewlineCharacterSet.characterIsMember(ch)
return false
end
true
end
def rs_potentialLinkAtPoint(point)
textRange = self.characterRangeAtPoint(point)
endOfDocumentTextPosition = self.endOfDocument
if textRange.end.isEqual(endOfDocumentTextPosition)
return nil
end
tapPosition = self.closestPositionToPoint(point)
if tapPosition == nil
return nil
end
s = ""
textPosition = tapPosition
isFirstCharacter = true
loop do
rangeOfCharacter = self.tokenizer.rangeEnclosingPosition(textPosition, withGranularity:UITextGranularityCharacter, inDirection:UITextWritingDirectionNatural)
oneCharacter = self.textInRange(rangeOfCharacter)
if (isFirstCharacter)
if (oneCharacter.isEqualToString("\n") || oneCharacter.isEqualToString("\r"))
return nil
end
end
isFirstCharacter = false
if (!stringCharacterIsAllowedAsPartOfLink(oneCharacter))
break
end
s.appendString(oneCharacter)
textPosition = self.positionFromPosition(textPosition, offset:1)
if (textPosition == nil)
break
end
end
textPosition = self.positionFromPosition(tapPosition, offset:-1)
if (textPosition != nil)
loop do
rangeOfCharacter = self.tokenizer.rangeEnclosingPosition(textPosition, withGranularity:UITextGranularityCharacter, inDirection:UITextWritingDirectionNatural)
oneCharacter = self.textInRange(rangeOfCharacter)
if (!stringCharacterIsAllowedAsPartOfLink(oneCharacter))
break
end
s.insertString(oneCharacter, atIndex:0)
textPosition = self.positionFromPosition(textPosition, offset:-1)
if (textPosition == nil)
break
end
end
end
return s
end
def rs_linkAtPoint(point)
potentialLink = self.rs_potentialLinkAtPoint(point)
if (potentialLink == nil || potentialLink.length < 1)
return nil
end
return potentialLink
# links = potentialLink.rs_links
# if (links == nil || links.count < 1)
# return nil
# end
# firstLink = links[0]
# return firstLink
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment