Custom Hyperlink Control for iOS(Objective-C version)

Here comes the iOS version of the Custom Hyperlink Control. iOS also doesn’t come up with a native Hyperlink Control.

Implementation

#import <UIKit/UIKit.h>

@interface CustomHyperlink : UIControl<UITextViewDelegate>

NS_ASSUME_NONNULL_BEGIN

@property UITextView *innerText;
@property UIColor *defaultColor;
@property UIColor *pressedColor;
@property NSDictionary *hyperlinkAttribute;

@property (assign) SEL action;
@property (readonly, strong, nullable) id target;
@property (readonly, strong, nullable) id param;

- (void)setLinkText:(NSString*) valueIn;
- (NSString*)linkText;
- (void)addTarget:(nullable id)targetIn action:(SEL)actionIn param:(nullable id)paramIn;

NS_ASSUME_NONNULL_END

@end
#import "CustomHyperlink.h"

@implementation CustomHyperlink

- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self)
    {
        CGRect parentFrame = self.frame;
        CGRect frame = CGRectMake(0, 0, parentFrame.size.width, parentFrame.size.height);
        [self setHyperlinkColors];
        [self setInnerTextCtrl:frame];
        [self addTarget:self action:@selector(executeOnTappedEvent:) forControlEvents:UIControlEventTouchDown];
    }
    return self;
}

- (void)awakeFromNib {
    [super awakeFromNib];
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setHyperlinkColors];
        [self setInnerTextCtrl:frame];
        [self addTarget:self action:@selector(executeOnTappedEvent:) forControlEvents:UIControlEventTouchDown];
    }
    return self;
}

- (void)addTarget:(nullable id)targetIn action:(SEL)actionIn param:(nullable id)paramIn{
    _target = targetIn;
    _action = actionIn;
    _param = paramIn;
}

- (void)setLinkText:(NSString*)valueIn
{
    [self setHyperlinkAttribute];
    self.innerText.attributedText = [[NSAttributedString alloc] initWithString:valueIn attributes:self.hyperlinkAttribute];
}

- (NSString*)linkText
{
    return self.innerText.text;
}

#pragma tapped event

- (void)executeOnTappedEvent:(UIView *)view
{
    UIColor *beforeColor = self.innerText.textColor;
    [UIView transitionWithView:view
                      duration:1
                       options:UIViewAnimationOptionTransitionCrossDissolve
                    animations:^{
                        self.innerText.textColor = _pressedColor;
                    }
                    completion:^(BOOL finished){
                        if (finished) {
                            [self.target performSelector:self.action withObject:self.param afterDelay:0.1];
                            self.innerText.textColor = beforeColor;
                        }
                    }];
}

- (void)textViewDidChangeSelection:(UITextView *)textView
{
    CGFloat fixedWidth = textView.frame.size.width;
	CGSize newSize = [textView sizeThatFits:CGSizeMake(fixedWidth, MAXFLOAT)];
	CGRect newFrame = textView.frame;
	newFrame.size = CGSizeMake(fmaxf(newSize.width, fixedWidth), newSize.height);
	textView.frame = newFrame;
	
	CGRect selfFrame = self.frame;
	selfFrame.size = CGSizeMake(fmaxf(newSize.width, fixedWidth), newSize.height);
	self.frame = selfFrame;
}

#pragma set methods

- (void)setInnerTextCtrl:(CGRect)frame
{
    self.innerText = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
    [self.innerText setFont:[UIFont systemFontOfSize:14]];
    [self.innerText setAutocorrectionType:UITextAutocorrectionTypeNo];
    self.innerText.userInteractionEnabled = FALSE;
    self.innerText.scrollEnabled = FALSE;
    self.innerText.editable = FALSE;
    self.innerText.selectable = FALSE; // Add this property for ios 7 to disable the link type
    [self.innerText setDataDetectorTypes:UIDataDetectorTypeAll];
    self.innerText.delegate = self;
    [self.innerText setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
    [self addSubview:self.innerText];
}

- (void)setHyperlinkColors
{
    self.defaultColor = [UIColor blackColor];
    self.pressedColor = [UIColor greenColor];
}

- (void)setHyperlinkAttribute
{
    self.hyperlinkAttribute = @{NSFontAttributeName:[UIFont systemFontOfSize:self.innerText.font.pointSize], NSForegroundColorAttributeName: _defaultColor, NSUnderlineStyleAttributeName:@(NSUnderlineStyleSingle)};
}

@end

Typical Usage

CustomHyperlink *hyperlink = [[CustomHyperlink alloc] initWithFrame:CGRectMake(controlXPosition, currentYPosition, controlWidth, controlHeight)];
[hyperlink addTarget:self action:@selector(executeTapEvent:) param:hyperlink];
[self.scrollView addSubview:hyperlink];

Happy coding…. 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s