Link detection in UITextView

Discussion in 'iOS Programming' started by xArtx, Feb 16, 2013.

  1. xArtx macrumors 6502a

    Mar 30, 2012
    Hi Guys,
    I have some links in a textview that work, but are ugly.

    Is it possible to replace the links with Human readable text
    as you do on a website, say by changing the text that appears from:


    Click Me


    Thanks :)
  2. hollersoft macrumors member

    Feb 10, 2013
    Not sure if this answers your question, but I've done that by writing a replacement TextView class that supports a NSAttributedString and renders it using CoreText. Then you can apply blue colour and underline to the sections you want. My class implements firstRectForNSRange: and closestPositionToPoint: for hit testing. Then you can do whatever your heart desires when the link is tapped.

    There is native support for NSAttributedString in UITextView in iOS 6, but I needed to "roll my own" for iOS 5 compatibility.
  3. PhoneyDeveloper macrumors 68040


    Sep 2, 2008
    You can do that with a UIWebView. Don't know how to do it with a UITextView.
  4. xArtx thread starter macrumors 6502a

    Mar 30, 2012
    I have my own textview, and just got everything down, except no
    horiz or vert scrollers, and no web links at all. Also not editable.
    Did it because I'm sure I'm going to want different fonts and colours
    at some stage.

    Might give the web view a go, haven't tried using one for text yet.
  5. dejo Moderator


    Staff Member

    Sep 2, 2004
    The Centennial State
    If it doesn't need to be editable, I would think a UIWebView would be a better approach.
  6. xArtx thread starter macrumors 6502a

    Mar 30, 2012
    It's worth a shot because I think it's controls would be similar,
    and no, not editable, it's supposed to be a simple documentation/credits screen,
    but I have to present this to the user... in code to preserve fixed width..

    The algorithm for Moon rise and Moon
    set calculation is based on a BASIC
    program that appeared in
    Sky & Telescope Magazine, July 1989,
    Page 78. It's original author is
    unknown, and cannot be credited.
    The original BASIC language listing
    "moonrise.bas" can be found here:
    The BASIC source was ported to C in
    Jan 2008. That is what is used in this
    iOS implementation.
    The links wrap like that, and I have to allow it, or they break.

Share This Page