0

I am trying to accomplish the following:

  • Read and Parse the text in a UITextField. Identify all the numbers in the UITextField and convert these into hyperlinks.
  • When these hyperlinks are clicked perform a custom action, which is to display a UIActionSheet and based on the selection assign the number (in the hyperlink) to another UITextField instance

For example if the UITextField has the text - "This is a sample test with number 123445 and more numbers 44555, 66777".

I should be able to parse the above text, detect all three numbers and add hyperlinks to them.

For the first part (parsing) I found out that there is a NSRegularExpression class that can be used to detect patterns in a text. But I could not find a way of adding hyperlinks to the matched numbers. I tried looking at Three20 documentation and could not figure out a way. Even tried the answer in this link - Just how to you use TTStyledTextLabel? but it only auto detects URLs and adds hyperlinks to them, I want to add hyperlinks to any custom text.

Can someone please help me with this. Please do not ask me to use WebView. I would really appreciate some code snippets. Thanks in advance. I am using xCode4.

Community
  • 1
  • 1
AnilV
  • 179
  • 2
  • 12

1 Answers1

0

If you don't need the text to be user-editable directly, you may use my OHAttributedLabel class to achieve this. (here on github)

This allows you to display any NSAttributedString and can also autodetect links, phone numbers and everything Apple's NSDataDetector class is able to detect. You can also add custom links to your label on any part of the text.

See the sample project included in my github repository for more details. It is very customizable, both for link colors, underline style, action to perform when a link is tapped, which link types it should autodetect, and you can add any custom links and style you need on the text.

AliSoftware
  • 32,623
  • 6
  • 82
  • 77
  • Thanks AliSoftware. I will check this out. – AnilV Sep 12 '11 at 02:10
  • Hi AliSoftware. I am planning to test this out in my application. How do I use this, should I just copy the OHAttributedLabel and NSAttributedString+Attributes h and m files into my project? – AnilV Sep 17 '11 at 06:08
  • Yes, just copy (haven't embed it in a framework or lib yet). See the sample project in my github. (If you appreciate it, it would be nice if you make me some feedback like posting on you blog or mentioning on your aboutbox) – AliSoftware Sep 17 '11 at 09:36
  • Thanks AliSoftware, will definitely do so. – AnilV Sep 17 '11 at 15:17
  • I tried using it by adding the h and m files but I got a whole bunch of linker errors while building the project. One of them is below: "Apple Mach-O Linker (id) Error - "_CTFramesetterCreateWithAttributedString", referenced from: -[NSAttributedString(OHCommodityConstructors) sizeConstrainedToSize:fitRange:] in NSAttributedString+Attributes.o -[OHAttributedLabel drawTextInRect:] in OHAttributedLabel.o I am using Xcode 4.1, could it be that these classes are for 3.0 and that is the reason for the errors. How do I make this work on 4.0?? – AnilV Sep 17 '11 at 16:19
  • You probably just forgot to add the "CoreText" framework to your project that's all. The classes are totally compatible with xcode4 (but only iOS4+). See the readme file for details (or search Stackoverflow there are some questions talking about OHAttributedLabel too) – AliSoftware Sep 17 '11 at 17:24
  • I looked in the README file and could not find any mention of how to add "CoreText" framework. Looking at some other site I found that you need to include the line - "#import ". I did this in my delegate.h and other h files, but still no luck. Could you please let me know how to add "CoreText" framework to my project and resolve these issues. – AnilV Sep 18 '11 at 02:24
  • Finally was able to figure out how to add CoreText framework. Here is a quick guide for someone with similar issue - http://www.raywenderlich.com/4147/how-to-create-a-simple-magazine-app-with-core-text – AnilV Sep 18 '11 at 08:06
  • Yep you're right my bad I thought I explained this in the readme but it's not the case. Will definitely have to add it some day in a small documentation. – AliSoftware Sep 18 '11 at 10:22
  • FYI, I edited the [README](https://github.com/AliSoftware/OHAttributedLabel/blob/master/README.md) to include the instructions that were missing, and also completed the repository [wiki](https://github.com/AliSoftware/OHAttributedLabel/wiki). Thx – AliSoftware Sep 18 '11 at 17:31
  • Hi AliSoftware - Using the "OHAttributedLabel", I was able to add hyperlinks to the text I wanted. However I could not get the alert to display when I click on this hyperlink. I used the example you provided in "AttributedLabel_ExampleAppDelegate.m". I defined my own hyperlink as shown in your example method "fillLabel3". But when I click on the hyperlink nothing happens. Am I missing something? I do see the code in "shouldFollowLink" where you check if the URL scheme is "user" and display an alert msg. But where does this method get called? – AnilV Oct 09 '11 at 05:35
  • That's a delegate method, which works like every other delegate method you find everywhere in the Cocoa framework. You defined the object where you implemented this `shouldFollowLink` delegate method... as the delegate of your OHAttributedLabel, right? – AliSoftware Oct 09 '11 at 11:57
  • I have defined the object, where I am implementing this method as a delegate of OHAttributedLabel, with the the code - ['@interface MtgDetailController : UIViewController ']. Even then I do not see anything after I click on the hyperlink. I put a breakpoint at the ['shouldFollowLink'] function but it does not get hit. I even tried implementing the function in my main app delegate, but still no luck. Could you please help. – AnilV Oct 09 '11 at 15:49
  • Learn how t use delegate. Here you only declared that your class conforms to a delegate, but not that the instance is the delegate of your `OHAttributedLabel` object. Do as you would do for `UITableView`'s delegate & dataSource and every other things in Cocoa : either do it by code `attrLbl.delegate = self` or thru InterfaceBuilder... _but if you don't know those things you probably should read the basics from the doc and try simpler samples as using delegate is the very base in Cocoa_ – AliSoftware Oct 09 '11 at 16:16
  • Thanks very much. Using attrLbl.delegate = self, worked. Have one more question though. How do I access the string (hyperlink) that was clicked. For e.g. if my hyperlink is "1234" and the URL that it is linked to is "user://test". Using your example I can get hold of the URL scheme (which is "user") and URL host (which is "test"). However I could not know the actual hyperlink that was clicked (which is "1234"). Is there a property of NSTextCheckingResult that I can query to know this? – AnilV Oct 09 '11 at 22:18
  • Figured it out after looking at it for some more time. Need to use the range for the linkinfo variable. – AnilV Oct 09 '11 at 22:59
  • Hi AliSoftware - Can you tell me how to add a border to my OHAttributedLabels. I tried using the regular UILabel border function (myLabel.layer.bordercolor) but that did not work. – AnilV Oct 17 '11 at 02:14