本文共 10406 字,大约阅读时间需要 34 分钟。
其实这个内容蛮早了,只是今天整理到博客上。自从有了IOS7后,文字的颜色和文字与图片的混排甚至把图片作为表情都可以轻松地实现了。不打算书写了,主体代码粘贴如下,需要细看的直接下载附件就可以了。
@interface MMTextAttachment : NSTextAttachment {
}
@end
@implementation MMTextAttachment
//I want my emoticon has the same size with line's height
(CGRect)attachmentBoundsForTextContainer:(NSTextContainer *)textContainer proposedLineFragment:(CGRect)lineFrag glyphPosition:(CGPoint)position characterIndex:(NSUInteger)charIndex NS_AVAILABLE_IOS(7_0){ return CGRectMake( 0 , 0 , lineFrag.size.height , lineFrag.size.height );}
@end
@interface ViewController (){
UILabel *lbInfo;UITextView *textView;
}@end
@implementation ViewController
(void)viewDidLoad {
[super viewDidLoad];// Do any additional setup after loading the view, typically from a nib.// 添加控件 [self customizeTheLb];// 添加TextView [self customizeTheTextView];// 显示不同的颜色
// [self customizeDifferentColors];
// 正则匹配,特殊显示匹配到的文字
// [self createRegularExpressionText];
// 显示不同的文本样式
// [self customizeDifferentStyles];
// 取出某个区域
// [self customizeExceptSomeZone1];
// [self customizeExceptSomeZone2];
// 图片融入文字,作为表情[self customizeExceptSomeZone3];
}
(void) customizeTheLb{
lbInfo = [[UILabel alloc] init];[self.view addSubview:lbInfo];lbInfo.numberOfLines = -1;[lbInfo setTranslatesAutoresizingMaskIntoConstraints:NO];[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[lbInfo]-10-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(lbInfo)]];[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-40-[lbInfo]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(lbInfo)]];
}
(void) customizeTheTextView{
textView = [[UITextView alloc] init];[self.view addSubview:textView];[textView setTranslatesAutoresizingMaskIntoConstraints:NO];[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[textView]-0-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(textView)]];[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-20-[textView]-0-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(textView)]];textView.delegate = self;textView.selectable = NO;textView.text = @"wodeceshi";
}
// 显示不同的颜色
(void)customizeDifferentColors{ NSMutableString *roadInfo = [NSMutableString stringWithString:@"沿无名道路r向枫涟山庄5号楼出发"];
NSLog(@"roadInfo:%@", roadInfo);
UIFont *font1 = [UIFont boldSystemFontOfSize:28];UIColor *textColor1 = [UIColor blackColor];
UIFont *font2 = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];UIColor *textColor2 = [UIColor colorWithRed:0.175 green:0.458 blue:0.831 alpha:1.0];
NSDictionary *attrs1 = @{
NSForegroundColorAttributeName:textColor1, NSFontAttributeName:font1 };
NSRange range1 = [roadInfo rangeOfString:@"无名道路"];
NSDictionary *attrs2 = @{
NSForegroundColorAttributeName:textColor2, NSFontAttributeName:font2 };
NSRange range2 = [roadInfo rangeOfString:@"枫涟山庄5号楼"];
NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:roadInfo];if (range1.location != NSNotFound){
[attributedStr addAttributes:attrs1 range:range1];
}if (range2.location != NSNotFound){
[attributedStr addAttributes:attrs2 range:range2];
}lbInfo.attributedText = attributedStr;
lbInfo.preferredMaxLayoutWidth = 200;lbInfo.backgroundColor = [UIColor yellowColor];CGSize size = [lbInfo systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];NSLog(@"--height:%f", size.height);}
(void) createRegularExpressionText{
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"孙某人" options:0 error:Nil];NSMutableString *regularInfo = [NSMutableString stringWithString:@"这个测试孙某人信息显示正确与否--孙某人"];NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:regularInfo];NSRange searchRange = NSMakeRange(0, regularInfo.length);UIFont *font = [UIFont boldSystemFontOfSize:18];UIColor *textColor = [UIColor blueColor];NSDictionary *attrs = @{ NSForegroundColorAttributeName:textColor, NSFontAttributeName:font };[regex enumerateMatchesInString:regularInfo options:0 range:searchRange usingBlock:^(NSTextCheckingResult *match, NSMatchingFlags flags, BOOL *stop){ NSRange matchRange = [match rangeAtIndex:0]; [attributedStr addAttributes:attrs range:matchRange];}];lbInfo.attributedText = attributedStr;
}
// 文本显示不同的样式
(void) customizeDifferentStyles{
NSMutableString *roadInfo = [NSMutableString stringWithString:@"关于农村宅基地改革的问题,目前确实有一个试点方案,已经编制完成了,但还没有最后批准。”23日上午,一位不愿具名的国土资源部内部人士向记者证实,国土资源部已经初步编制完成了农村宅基地制度改革试点方案www.baidu.com。\r所谓农村宅基地,是农村的农户或个人用作住宅基地而占有、利用本集体所有的土地。包括已经建设房屋、建过房屋或者决定用于建造房屋的土地,已经建设房屋的土地、建过房屋但已无上盖物或不能居住的土地以及准备建房用的规划地三种类型。\r据悉,按照目前已经基本成型的“试点方案”,农村宅基地改革试点工作将按照“分类处理”原则的进行。所谓分类,即“城乡建设用地扩展边界外的传统农区”和“城乡建设用地扩展边界内”两类,对于这两类不同情况,将以不同方式贯彻“一户一宅”的原则。"];UIFont *font = [UIFont systemFontOfSize:18];UIColor *textColor = [UIColor blackColor];NSMutableParagraphStyle *parahStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];parahStyle.firstLineHeadIndent =20;
// parahStyle.headIndent = 20;
// parahStyle.tailIndent = 10; // parahStyle.lineSpacing = 90; // parahStyle.paragraphSpacing = 20; // parahStyle.paragraphSpacingBefore = 80;
NSDictionary *attrs = @{ NSForegroundColorAttributeName:textColor, NSFontAttributeName:font, NSParagraphStyleAttributeName:parahStyle, NSKernAttributeName:@10, // NSUnderlineStyleAttributeName:@1, // NSStrokeWidthAttributeName:@2 };NSRange range = NSMakeRange(0, roadInfo.length);NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:roadInfo];[attributedStr addAttributes:attrs range:range];[attributedStr addAttribute:NSLinkAttributeName value:@"link1://www.baidu.com" range:[roadInfo rangeOfString:@"www.baidu.com"]];NSDictionary *linkAttributes = @{NSForegroundColorAttributeName: [UIColor greenColor], NSUnderlineColorAttributeName: [UIColor lightGrayColor], NSUnderlineStyleAttributeName: @(NSUnderlinePatternSolid)};textView.linkTextAttributes = linkAttributes;textView.delegate = self;textView.editable = NO;textView.selectable = YES;textView.attributedText = attributedStr;
}
(void)textViewDidBeginEditing:(UITextView *)textView{
}
(BOOL)textView:(UITextView )textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString )text{
return YES;}
/*
(BOOL) textView:(UITextView )textView shouldInteractWithURL:(NSURL )URL inRange:(NSRange)characterRange{ if ([URL.scheme isEqualToString:@"link1"]){
NSLog(@"点击进入百度网站");return NO;
}
return YES;}
*/
(void) customizeExceptSomeZone1{
CGRect rect = CGRectMake(100, 100, 100, 100);UIImageView *imageV = [[UIImageView alloc] init];imageV.frame = rect;imageV.image = [UIImage imageNamed:@"xiang.png"];imageV.layer.masksToBounds = YES;imageV.layer.cornerRadius = CGRectGetWidth(imageV.frame) / 2.0;[textView addSubview:imageV];NSMutableString *roadInfo = [NSMutableString stringWithString:@"关于农村宅基地改革的问题,目前确实有一个试点方案,已经编制完成了,但还没有最后批准。”23日上午,一位不愿具名的国土资源部内部人士向记者证实,国土资源部已经初步编制完成了农村宅基地制度改革试点方案www.baidu.com。\r所谓农村宅基地,是农村的农户或个人用作住宅基地而占有、利用本集体所有的土地。包括已经建设房屋、建过房屋或者决定用于建造房屋的土地,已经建设房屋的土地、建过房屋但已无上盖物或不能居住的土地以及准备建房用的规划地三种类型。\r据悉,按照目前已经基本成型的“试点方案”,农村宅基地改革试点工作将按照“分类处理”原则的进行。所谓分类,即“城乡建设用地扩展边界外的传统农区”和“城乡建设用地扩展边界内”两类,对于这两类不同情况,将以不同方式贯彻“一户一宅”的原则。"];textView.text = roadInfo;textView.font = [UIFont systemFontOfSize:18];UIBezierPath *newPath = [UIBezierPath bezierPathWithOvalInRect:imageV.frame];
// UIBezierPath *newPath = [UIBezierPath bezierPathWithRect:imageV.frame];
textView.textContainer.exclusionPaths = @[newPath];
}
(void) customizeExceptSomeZone2{
CGRect rect = CGRectMake(0, 0, 40, 40);NSTextAttachment * textAttachment = [[ NSTextAttachment alloc ] initWithData:nil ofType:nil ] ;UIImage * smileImage = [ UIImage imageNamed:@"xiang.png" ] ; //my emoticon image named a.jpgtextAttachment.image = smileImage;textAttachment.bounds = rect;NSMutableString *roadInfo = [NSMutableString stringWithString:@"关于农村宅基地改革的问题,目前确实有一个试点方案,已经编制完成了,但还没有最后批准。”23日上午,一位不愿具名的国土资源部内部人士向记者证实,国土资源部已经初步编制完成了农村宅基地制度改革试点方案www.baidu.com。\r所谓农村宅基地,是农村的农户或个人用作住宅基地而占有、利用本集体所有的土地。包括已经建设房屋、建过房屋或者决定用于建造房屋的土地,已经建设房屋的土地、建过房屋但已无上盖物或不能居住的土地以及准备建房用的规划地三种类型。\r据悉,按照目前已经基本成型的“试点方案”,农村宅基地改革试点工作将按照“分类处理”原则的进行。所谓分类,即“城乡建设用地扩展边界外的传统农区”和“城乡建设用地扩展边界内”两类,对于这两类不同情况,将以不同方式贯彻“一户一宅”的原则。"];textView.text = roadInfo;NSDictionary *attrs = @{ NSFontAttributeName:[UIFont systemFontOfSize:18] };NSMutableAttributedString * string = [[ NSMutableAttributedString alloc ] initWithString:roadInfo attributes:attrs ] ;NSAttributedString * textAttachmentString = [ NSAttributedString attributedStringWithAttachment:textAttachment ] ;[ string insertAttributedString:textAttachmentString atIndex:20] ;textView.attributedText = string ;
}
// 作为文字表情,与文字大小一样,融入文字
(void) customizeExceptSomeZone3{
MMTextAttachment* textAttachment = [[ MMTextAttachment alloc ] initWithData:nil ofType:nil ] ;UIImage * smileImage = [ UIImage imageNamed:@"xiang.png" ] ; //my emoticon image named a.jpgtextAttachment.image = smileImage;NSMutableString *roadInfo = [NSMutableString stringWithString:@"关于农村宅基地改革的问题,目前确实有一个试点方案,已经编制完成了,但还没有最后批准。”23日上午,一位不愿具名的国土资源部内部人士向记者证实,国土资源部已经初步编制完成了农村宅基地制度改革试点方案www.baidu.com。\r所谓农村宅基地,是农村的农户或个人用作住宅基地而占有、利用本集体所有的土地。包括已经建设房屋、建过房屋或者决定用于建造房屋的土地,已经建设房屋的土地、建过房屋但已无上盖物或不能居住的土地以及准备建房用的规划地三种类型。\r据悉,按照目前已经基本成型的“试点方案”,农村宅基地改革试点工作将按照“分类处理”原则的进行。所谓分类,即“城乡建设用地扩展边界外的传统农区”和“城乡建设用地扩展边界内”两类,对于这两类不同情况,将以不同方式贯彻“一户一宅”的原则。"];textView.text = roadInfo;NSDictionary *attrs = @{ NSFontAttributeName:[UIFont systemFontOfSize:18] };NSMutableAttributedString * string = [[ NSMutableAttributedString alloc ] initWithString:roadInfo attributes:attrs ] ;NSAttributedString * textAttachmentString = [ NSAttributedString attributedStringWithAttachment:textAttachment ] ;[ string insertAttributedString:textAttachmentString atIndex:20] ;textView.attributedText = string ;
}
(void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated.
}
@end
转载地址:http://nfoao.baihongyu.com/