标签:
UITextView的UIDataDetectorTypes可以实现自动检验电话号码、网址、地址等
//
// UITextView+AutomaticTest.h
// UITextView
//
// Created by apple on 16/7/5.
// Copyright © 2016年 lys. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface UITextView (AutomaticTest)
/**
* 自动检验电话号码、网址、地址等(可自定义字体和大小以及行间距)
*/
/**
* UIDataDetectorTypePhoneNumber 自动检验电话号码
* UIDataDetectorTypeLink 自动检验网址以及邮箱
* UIDataDetectorTypeAddress 自动检验地址
* UIDataDetectorTypeCalendarEvent 自动检验符合格式的日期(符合格式的日期会弹出一个ActionSheet,有创建事件,在Calendar中显示,和拷贝三个选项)
* UIDataDetectorTypeNone 不自动检验
* UIDataDetectorTypeAll 自动检验所有
*/
- (void)automaticTestWithFont:(UIFont *)font withParagraphStyle:(NSParagraphStyle *)paragraphStyle withDataDetectorTypes:(UIDataDetectorTypes)dataDetectorTypes;
/**
* 获取TextView的高度(自定义行间距)
*/
- (CGFloat)getTextHeightWithFont:(UIFont *)font withParagraphStyle:(NSParagraphStyle *)paragraphStyle;
@end
//
// UITextView+AutomaticTest.m
// UITextView
//
// Created by apple on 16/7/5.
// Copyright © 2016年 lys. All rights reserved.
//
#import "UITextView+AutomaticTest.h"
@implementation UITextView (AutomaticTest)
/**
* 自动检验电话号码、网址、地址等(可自定义字体以及行间距)
*/
- (void)automaticTestWithFont:(UIFont *)font withParagraphStyle:(NSParagraphStyle *)paragraphStyle withDataDetectorTypes:(UIDataDetectorTypes)dataDetectorType{
self.layer.cornerRadius = 6; // 设置圆角
self.layer.masksToBounds = YES;
self.backgroundColor=[UIColor whiteColor]; //背景色
self.scrollEnabled = NO; //当文字超过视图的边框时是否允许滑动,默认为“YES”
self.editable = NO; //是否允许编辑内容,默认为“YES”
self.textColor = [UIColor blackColor];
if (font == nil) {
UIFont *tfont = [UIFont systemFontOfSize:14];
font = tfont;
}
if (paragraphStyle == nil) {
NSMutableParagraphStyle *tparagraphStyle = [[NSMutableParagraphStyle alloc]init];
tparagraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
paragraphStyle = tparagraphStyle;
}
self.dataDetectorTypes = dataDetectorType; //设置自动检验类型
NSDictionary *attributes = @{NSFontAttributeName:font.copy,
NSParagraphStyleAttributeName:paragraphStyle.copy}; // 设置内容样式
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:self.text attributes:attributes];
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [self.text length])];
self.attributedText = attributedString; // 把属性字符串交给textView显示
}
/**
* 获取TextView的高度(可以自定义行间距)
*/
- (CGFloat)getTextHeightWithFont:(UIFont *)font withParagraphStyle:(NSParagraphStyle *)paragraphStyle{
if (paragraphStyle == nil) {
NSMutableParagraphStyle *tparagraphStyle = [[NSMutableParagraphStyle alloc]init];
tparagraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
paragraphStyle = tparagraphStyle;
}
NSDictionary *attributes = @{NSFontAttributeName:font,
NSParagraphStyleAttributeName:paragraphStyle.copy}; // 设置内容样式
// 获取内容高度(在Textview里无法显示最后一行)
CGRect rect = [self.text boundingRectWithSize:CGSizeMake(self.frame.size.width, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil];
NSString *string = [self.text substringToIndex:3]; // 截取字符串
// 获取单行高度
CGFloat heght = [string boundingRectWithSize:CGSizeMake(1000, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font} context:nil].size.height;
return rect.size.height+heght;
}
@end
标签:
原文地址:http://www.cnblogs.com/lys-iOS-study/p/5684325.html