Skip to main content

Detect url from string and want it highlight?

Take IBOutlet for TextField ,  Button and Lable:

TextField:  To take input from user
Label: To show output to user
Button : To Convert from string to Attributed string with highlighting HTTP string as a URL.

Code:
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextField *textField;

@property (weak, nonatomic) IBOutlet UILabel *label;

-(IBAction)click:(id)sender;

@end
@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
  
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(IBAction)click:(id)sender
{
  
    NSMutableArray *mutableWords = [[_textField.text componentsSeparatedByString: @" "] mutableCopy];
    NSLog(@"MutableWords: %@",mutableWords);
    NSMutableAttributedString *resultString;
    NSMutableAttributedString *attStr1,*attStr2;
    resultString = [[NSMutableAttributedString alloc]init];
  


    for (NSString *string in mutableWords)
    {
      
        NSRange match;
      
        attStr1 = [[NSMutableAttributedString alloc] initWithString:string];
        match = [string rangeOfString: @"http"];
        if (match.location == NSNotFound)
        {
            [resultString appendAttributedString:attStr1];
          
            NSLog(@"string:%@",resultString);
        }
        else
        {
            attStr2 = [[NSMutableAttributedString alloc] initWithString:string];

             [attStr2 addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleThick) range:NSMakeRange(0, [attStr2 length])];
            [attStr2 addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0, [attStr2 length])];

            [resultString appendAttributedString:attStr2];
          
        }

    }
  
    _label.attributedText = (NSAttributedString*)resultString;
   
}

@end

Input:

OutPut:



or

Do it in a smart way:

1. TextField: For input text (inputTextField)
2. TextView: For Output (outPutTextView)
3. Button: On click action

Button Onclick Action:

   self.textView.text = self.inputTextField.text;
   self. outPutTextView.editable = NO;

   self. outPutTextView.dataDetectorTypes = UIDataDetectorTypeAll;



Comments

Popular posts from this blog

UIKit Framework Hierarchy

UIKIT Framework Hierarchy

CoreData and its Interview Questions

Core Data takes advantage of the Objective-C language and its runtime, and neatly integrates with the Core Foundation framework. The result is an easy to use framework for managing an object graph that is elegant to use and incredibly efficient in terms of memory usage. Every component of the Core Data framework has a specific purpose and function. If you try to use Core Data in a way it wasn't designed for, you will inevitably end up struggling with the framework. Developers new to the Core Data framework often confuse it with and expect it to work as a database. If there's one thing I hope you'll remember from this series, it is that Core Data isn't a database and you shouldn't expect it to act like one. It's the Model in the Model-View-Controller pattern that permeates the iOS SDK. Core Data isn't the database of your application nor is it an API for persisting data to a database. Core Data is a framework that manages an object graph. It&

Middle Level iOS Developer interview questions

Middle Level iOS Developer interview questions 1) App Thinning 2) Steps for pushing application to app store 3) How push notification works 4) How google TEZ app getting near devices for get and send money 5)Retain Concept in iOS 6)Memory management  in iOS 7)ARC means 8) Syntax for shared instance 9) optionals and unwraping 10) Design pattarns 11) Tab bar insertion between screens 12) how to load images in table view using urls for each image by smooth scrolling  13) Extenstions 14)Clousers 15)Life cycles  16)how Table view reusable works?

Like us on Facebbok