iOS 封装左图右输入框以及文本框上的超链接

今天主要写两个东西
<1> 一个是左面是用户头像或小标志, 右面是输入框, 这样情况分开写虽然不是很麻烦, 但是为了让代码显得高端大气上档次, 我们通常要封装一个view类.
/* 注意:我是在MRC下写的*/
自定义View的.h文件

#import 
@interface MyView : UIView
// 这个是自定义View的左边imageView属性
@property (nullable, nonatomic, retain) UIImageView *imageViewLeft;
// 这是自定义View的右面的textfield属性
@property (nullable, nonatomic, retain) UITextField *textFieldRight;
// 这个是左边imageView和右边textfield的frame的宽之比(这个值在零到一之间)
@property (nonatomic, assign) CGFloat ratio;
// 初始化 这里传进来的参数:frame是外界给的自定义View的frame ratio是宽之比 image是左边ImageView的image placeHolder是输入框的占位符
- (nullable instancetype)initWithFrame:(CGRect)frame ratio:(CGFloat)ratio withImage:(nullable UIImage *)image placeHolder:(nullable NSString *)placeHolder;
@end

自定义View的.m文件

#import "MyView.h"
@implementation MyView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (void)dealloc {

    [_imageViewLeft release];
    [_textFieldRight release];
    
    [super dealloc];
}
// 通用
- (instancetype)initWithFrame:(CGRect)frame {

    return [self initWithFrame:frame ratio:0 withImage:nil placeHolder:nil];
}
// 指派初始化
- (instancetype)initWithFrame:(CGRect)frame ratio:(CGFloat)ratio withImage:(UIImage *)image placeHolder:(NSString *)placeHolder {

    self = [super initWithFrame:frame];
    if (self) {
            // 创建视图对象
        [self createSubViewsWithFrame:frame ratio:ratio];
        self.imageViewLeft.image = image;
        self.textFieldRight.placeholder = placeHolder;
    }
    return self;
}

- (void)createSubViewsWithFrame:(CGRect)frame ratio:(CGFloat)ratio {

    self.imageViewLeft = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width * ratio, frame.size.height)];
    [self addSubview:_imageViewLeft];
    [_imageViewLeft release];
    
    self.textFieldRight = [[UITextField alloc] initWithFrame:CGRectMake(frame.size.width * ratio, 0, frame.size.width * (1 - ratio), frame.size.height)];
    [self addSubview:_textFieldRight];
    [_textFieldRight release];
}
@end

然后导入视图控制器里用就可以用了

效果图如下:


iOS 封装左图右输入框以及文本框上的超链接_第1张图片
屏幕快照 2016-07-16 下午4.49.52.png

<2> 另一个是文本上的点击超链接
这块也是比较方便的在使用点击文本跳转的时候
这里主要用到TTTAttributeLabel(可以在github上下载)
由于篇幅较长这里就不解释了, 感兴趣的可以自己研究研究.

这里直接在VC中导入, 引用

#import "ViewController.h"
#import "WH_AttributedLabel.h"


@interface ViewController () 

@end

@implementation ViewController

static inline NSRegularExpression *NameRegularExpression(){

    static NSRegularExpression *_nameRegularExpression = nil;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _nameRegularExpression = [[NSRegularExpression alloc] initWithPattern:@"^\\w+" options:NSRegularExpressionCaseInsensitive error:nil];
    });
    
    return _nameRegularExpression;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    WH_AttributedLabel *label = [[WH_AttributedLabel alloc] initWithFrame:CGRectMake(0, 0, 200, 40)];
    label.center = self.view.center;
    label.font = [UIFont systemFontOfSize:14];
    label.textColor = [UIColor blackColor];
    label.lineBreakMode = NSLineBreakByCharWrapping;
    label.numberOfLines = 0;
    
    // 设置高亮颜色
    label.highlightedTextColor = [UIColor greenColor];
    
    // 检测url
    label.enabledTextCheckingTypes = NSTextCheckingTypeLink;
    
    // 对齐方式
    label.verticalAlignment = WH_AttributedLabelVerticalAlignmentCenter;
    
    // 行间距
    label.lineSpacing = 8;
    
    // 设置阴影
//    label.shadowColor = [UIColor grayColor];
    
    // delegate
    label.delegate = self;
    
    // NO 不显示下划线
    label.linkAttributes = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] forKey:(NSString *)kCTUnderlineStyleAttributeName];
    
    NSString *text = @"有问题, 点这里";
    
    [label setText:text afterInheritingLabelAttributesAndConfiguringWithBlock:^NSMutableAttributedString *(NSMutableAttributedString *mutableAttributedString) {
       
        // 设置可点文字的范围
        NSRange boldRange = [[mutableAttributedString string] rangeOfString:@"点这里" options:NSCaseInsensitiveSearch];
        
        // 设定可点击文字的大小
        UIFont *boldSystwmFont = [UIFont boldSystemFontOfSize:14];
        CTFontRef font = CTFontCreateWithName((__bridge CFStringRef)boldSystwmFont.fontName, boldSystwmFont.pointSize, NULL);
        
        if (font) {
            
            // 设置可点击文本的大小
            [mutableAttributedString addAttribute:(NSString *)kCTFontAttributeName value:(__bridge id)font range:boldRange];
            
            // 设置可点击文本的颜色
            [mutableAttributedString addAttribute:(NSString *)kCTForegroundColorAttributeName value:(id)[UIColor blueColor].CGColor range:boldRange];
            
            CFRelease(font);
        }
        
        return mutableAttributedString;
        
    }];
 
    NSRegularExpression *regexp = NameRegularExpression();
    
    NSRange linkRange = [regexp rangeOfFirstMatchInString:text options:0 range:NSMakeRange(5, 3)];
    
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http:www.baidu.com"]];
    
    // 设置连接的url
    [label addLinkToURL:url withRange:linkRange];
  
    [self.view addSubview:label];
    
}
- (void)attributedLabel:(__unused WH_AttributedLabel *)label didSelectLinkWithURL:(NSURL *)url {
    
     [[UIApplication sharedApplication] openURL:url];
}

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

@end

效果如下:

iOS 封装左图右输入框以及文本框上的超链接_第2张图片
文本.gif

你可能感兴趣的:(iOS 封装左图右输入框以及文本框上的超链接)