NSAttributedString,并富文本,正则表达式,超链接,水印

重载NSAttachment传送门

正则表达式传送门

码:

#import "ViewController.h"

#import "CustomTextAttachment.h"


//NSAttributedString.h 中文本属性key的说明

/*

 NSFontAttributeName                设置字体属性,默认值:字体:Helvetica(Neue)字号:12

 NSForegroundColorAttributeName     设置字体颜色,取值为 UIColor对象,默认值为黑色

 NSBackgroundColorAttributeName     设置字体所在区域背景颜色,取值为 UIColor对象,默认值为nil,透明色

 NSLigatureAttributeName            设置连体属性,取值为NSNumber对象(整数)0表示没有连体字符,1 表示使用默认的连体字符

 NSKernAttributeName                设定字符间距,取值为 NSNumber对象(整数),正值间距加宽,负值间距变窄

 NSStrikethroughStyleAttributeName  设置删除线,取值为 NSNumber对象(整数)

 NSStrikethroughColorAttributeName  设置删除线颜色,取值为 UIColor对象,默认值为黑色

 NSUnderlineStyleAttributeName      设置下划线,取值为 NSNumber对象(整数),枚举常量 NSUnderlineStyle中的值,与删除线类似

 NSUnderlineColorAttributeName      设置下划线颜色,取值为 UIColor对象,默认值为黑色

 NSStrokeWidthAttributeName         设置笔画宽度,取值为 NSNumber对象(整数),负值填充效果,正值中空效果

 NSStrokeColorAttributeName         填充部分颜色,不是字体颜色,取值为 UIColor对象

 NSShadowAttributeName              设置阴影属性,取值为 NSShadow对象

 NSTextEffectAttributeName          设置文本特殊效果,取值为 NSString对象,目前只有图版印刷效果可用:

 NSBaselineOffsetAttributeName      设置基线偏移值,取值为 NSNumberfloat,正值上偏,负值下偏

 NSObliquenessAttributeName         设置字形倾斜度,取值为 NSNumberfloat,正值右倾,负值左倾

 NSExpansionAttributeName           设置文本横向拉伸属性,取值为 NSNumberfloat,正值横向拉伸文本,负值横向压缩文本

 NSWritingDirectionAttributeName    设置文字书写方向,从左向右书写或者从右向左书写

 NSVerticalGlyphFormAttributeName   设置文字排版方向,取值为 NSNumber对象(整数)0表示横排文本,1 表示竖排文本

 NSLinkAttributeName                设置链接属性,点击后调用浏览器打开指定URL地址

 NSAttachmentAttributeName          设置文本附件,取值为NSTextAttachment对象,常用于文字图片混排

 NSParagraphStyleAttributeName      设置文本段落排版格式,取值为 NSParagraphStyle对象

 */


@interface ViewController (){

    

    NSMutableAttributedString *_mAS;

}

@property (weak, nonatomic) IBOutletUILabel *label;

@property (weak, nonatomic) IBOutletUITextView *textView;


@property(nonatomic,copy)NSString *str;




@end


@implementation ViewController


- (void)viewDidLoad {

    [superviewDidLoad];


    _str=@"阿里巴巴集团宣布,阿里旗下天猫,https://www.taobao.com,将与湖南卫视联手\r\n1110日晚推出『天猫201511狂欢夜』[吃惊]4小时全球互动直播,http://www.sina.com.cn,著名导演冯小刚将坐镇此次晚会的总导演。用晚会的形式过双11,无论对天猫和湖南卫视而言尚属首次。从目前对外透露的情况来看,这场11春晚将融合综艺内容、明星游戏、移动购物于一体,消费者可以通过电视、网络、手机等平台,实现边看边玩边买,http://www.baidu.com,201511狂欢宰";

    

    

    [self_labelTest];

    

    [self_textViewTest];


//    NSDictionary *dic=@{NSFontAttributeName:[UIFont systemFontOfSize:15]};

  

}

-(void)_labelTest{//简单的label只有添加手势能实现点击跳转

    

    _label.userInteractionEnabled=YES;//开启交互性

    

    UITapGestureRecognizer *tapG=[[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(toURL)];

    [_labeladdGestureRecognizer:tapG];//添加单击手势,在手势方法里面跳转


    _label.numberOfLines=0;

    _label.text=_str;

    {//此法无效,并不能响应定点点击

//    NSMutableAttributedString *mAS=[[NSMutableAttributedString alloc]initWithString:_str];

//    

//    [mAS addAttributes:@{

//                        NSLinkAttributeName:@"http://www.baidu.com"

//                        }range:NSMakeRange(0, 4)];

//    

//    

//    _label.attributedText=mAS;

    }

    

}

-(void)toURL{

    NSString *path=@"http://www.baidu.com";

    

    [[UIApplicationsharedApplication]openURL:[NSURLURLWithString:path]];

    

}


-(void)_textViewTest{

    

    _textView.editable=NO;

    

    _mAS=[[NSMutableAttributedStringalloc]initWithString:_str];

    

    [_mASaddAttributes:@{

                        NSLinkAttributeName:@"http://www.baidu.com"

                        }range:NSMakeRange(0,4)];

    

    [_mASaddAttributes:@{

                         NSForegroundColorAttributeName:[UIColorredColor]

                        }range:NSMakeRange(0,13)];

    //设置超链接颜色,默认蓝色。上方的方法改不掉超级链接的颜色!!

    _textView.linkTextAttributes =@{NSForegroundColorAttributeName : [UIColorpurpleColor]};

    

    [self_regex];

    

    [self_insertAtt];

 

    _textView.attributedText=_mAS;

    

    _textView.delegate=self;

    

    

    

}

#pragma mark-TextViewDelegate

- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRangeNS_AVAILABLE_IOS(7_0);{

    

    NSLog(@"%@",URL.absoluteString);

    return YES;

}

- (BOOL)textView:(UITextView *)textView shouldInteractWithTextAttachment:(NSTextAttachment *)textAttachment inRange:(NSRange)characterRangeNS_AVAILABLE_IOS(7_0);{

    

   NSLog(@"%@",textAttachment.image);

    

   NSLog(@"_______range%ld,%ld",characterRange.length,characterRange.location);

    

    

    

    return YES;

}


//实现,图文混排,富文本  1.Text Kit(OC)  2.CoreText(C函数)实现复杂

#pragma mark-1./

-(void)_insertAtt{

    

    

//    2.改变附件大小,子类化NSTextAttachment,覆写方法

   /*

     - (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);//返回的就是附件的大小

     }

     */

    

    CustomTextAttachment *tA=[[CustomTextAttachmentalloc]init];//附件

    

    

//    1.改变附件大小,直接操作于image.例:

   UIImage *image=[UIImageimageNamed:@"Aha.gif"];

    

//    UIImage *imageScale=[self scaleImage:image toScale:.3];

//    

//    tA.image=imageScale;

    

    tA.image=image;


    NSAttributedString *aAS=[NSAttributedStringattributedStringWithAttachment:tA];//根据附件生成一个属性文本

    

    [_mASinsertAttributedString:aAS atIndex:4];

    

}



//iOS Foundation框架中的正则表达式功能、、、也有第三方的框架可以用

-(void)_regex{

    

    NSRegularExpression *regular=[NSRegularExpressionregularExpressionWithPattern:@"http(s)?://([a-zA-Z0-9._-]+(/)?)*"options:NSRegularExpressionCaseInsensitiveerror:nil];


//     NSString *regex = [NSString stringWithFormat:@"(@[\\w-]{2,30})|(#[^#]+#)|(http(s)?://([a-zA-Z0-9._-]+(/)?)*)"];//多条件用拼接字符串,逻辑符拼接条件,此例!

//    NSRegularExpression *regular2=[NSRegularExpression regularExpressionWithPattern:regex options:NSRegularExpressionCaseInsensitive error:nil];

    

    NSArray *arr=[regularmatchesInString:_stroptions:NSMatchingReportProgressrange:NSMakeRange(0,_str.length)];

    

   for (NSTextCheckingResult *resultin arr) {

       NSRange range=result.range;//匹配的结果的range

        

        

       NSString *resultStr=[_strsubstringWithRange:range];

       NSLog(@"%@",resultStr);

        

//        NSInteger location1=range.location+1;

//        NSInteger location2=range.location+range.length+1;

//        

//        NSRange rangeNew=[_str rangeOfString:resultStr];

        

//这里!!Xcodebug,可能会导致出现的网址覆盖颜色的范围的不对的情况,一旦改范围直接蹦,坑货,浪费我半小时,浪费我半小时,浪费我半小时,重要的说3遍!

        [_mASaddAttributes:@{

                            NSLinkAttributeName:resultStr

                            }range:range];

        

    }

}


//1.等比率缩放

- (UIImage *)scaleImage:(UIImage *)image toScale:(float)scale

{

   UIGraphicsBeginImageContext(CGSizeMake(image.size.width * scale, image.size.height * scale));

//    CGContextRef context=UIGraphicsGetCurrentContext();

    

   [imagedrawInRect:CGRectMake(0,0, image.size.width * scale, image.size.height * scale)];

    

    UIImage *scaleImage =UIGraphicsGetImageFromCurrentImageContext();

    

    UIGraphicsEndImageContext();

                                

   return scaleImage;

                                

    }

//附:水印图片绘制

/*

#import "MaskView.h"


@implementation MaskView


+ (UIImage *)getWaterMaskImage:(UIImage *)image text:(NSString *)text {

    

    //1.建立一个新的上下文

    UIGraphicsBeginImageContext(image.size);

    

    //2。绘制图片

    [image drawInRect:CGRectMake(0, 0,image.size.width, image.size.height)];

    

    //3.设置文字的位置

    CGRect textFrame = CGRectMake(0, image.size.height - 30, image.size.width, 30);

    

    

    NSDictionary *attr = @{NSFontAttributeName :[UIFont systemFontOfSize:20]};

    //4.将文字绘制上

    [text drawInRect:textFrame withAttributes:attr];

    

    //5.上下文中的图片

    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

    

    

    return newImage;

}

*/




@end



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值