ios 实现艾特功能

iOS实现@功能,有问题欢迎大家指教




第一步 判断什么时候 需要判断最后一个字符是@

NSString *lang = [[UITextInputModecurrentInputMode]primaryLanguage];

        

 if([langisEqualToString:@"zh-Hans"]){//简体中文输入,包括简体拼音,健体五笔,简体手写

            

   UITextRange *selectedRange = [self.textViewmarkedTextRange];

            

   UITextPosition *position = [self.textViewpositionFromPosition:selectedRange.startoffset:0];

    if (!position){//非高亮

         [selfconfig:x];

    }

  }else{//中文输入法以外

     [selfconfig:x];

 }




- (void)config:(NSString *)text

{

    if (!text.length) {

        return;

    }

    NSString *lastString = [textsubstringFromIndex:text.length-1];

    NSString *textString = [textsubstringToIndex:text.length-1];//@之外的字符

    if ([lastStringisEqualToString:@"@"]) {

        @weakify(self);

        [[selfgetText]subscribeNext:^(id x) {

            @strongify(self);

            NSMutableAttributedString *attribute = [[NSMutableAttributedStringalloc]initWithAttributedString:self.textView.attributedText];

            [attribute deleteCharactersInRange:NSMakeRange(textString.length,1)];

            NSTextAttachment *imageText = [[NSTextAttachmentalloc]init];

            imageText.image = [selfgetImage:[NSStringstringWithFormat:@"@%@",x]];

            NSAttributedString * attStr = [NSAttributedStringattributedStringWithAttachment:imageText];

//            [attribute insertAttributedString:attStr atIndex:textString.length];

            [attribute appendAttributedString:attStr];

            dispatch_async(dispatch_get_main_queue(), ^{

                

                [self.textViewsetAttributedText:attribute];;

            });//@@@@  @@@

        }];

    }

}

//这一步主要是为了获取@后的字符,我这是写的一个测试例子

- (RACSignal *)getText

{

    return [RACSignalstartEagerlyWithScheduler:[RACSchedulerscheduler]block:^(id<RACSubscriber> subscriber) {

        [subscriber sendNext:@"李三"];

        [subscriber sendCompleted];

    }];

}


- (UIImage *)getImage:(NSString *)name

{

    UIColor *color = [UIColorclearColor];

    CGRect rect =CGRectMake(0.0f,0.0f,50,13);

    UIGraphicsBeginImageContext(rect.size);

    CGContextRef context =UIGraphicsGetCurrentContext();

    CGContextSetFillColorWithColor(context, [colorCGColor]);

    CGContextFillRect(context, rect);

    UIImage *img =UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    

    NSString *headerName = name;

    UIImage *headerimg = [selfimageToAddText:imgwithText:headerName];

    

    return headerimg;

}

//把文字绘制到图片上

- (UIImage *)imageToAddText:(UIImage *)img withText:(NSString *)text

{

    //1.获取上下文

    UIGraphicsBeginImageContext(img.size);

    //2.绘制图片

    [img drawInRect:CGRectMake(0,0, img.size.width,13)];

    //3.绘制文字

    //    CGRect rect = CGRectMake(0,(img.size.height-45)/2, img.size.width, 25);

    CGRect rect =CGRectMake(0,0, img.size.width,13);

    NSMutableParagraphStyle *style = [[NSMutableParagraphStyledefaultParagraphStyle]mutableCopy];

    style.alignment =NSTextAlignmentCenter;

    //文字的属性

    NSDictionary *dic =@{NSFontAttributeName:[UIFontsystemFontOfSize:12],NSParagraphStyleAttributeName:style,NSForegroundColorAttributeName:[UIColorredColor]};

    //将文字绘制上去

    [text drawInRect:rectwithAttributes:dic];

    //4.获取绘制到得图片

    UIImage *watermarkImg =UIGraphicsGetImageFromCurrentImageContext();

    //5.结束图片的绘制

    UIGraphicsEndImageContext();

    

    return watermarkImg;

}


有问题,欢迎评论指教,谢谢大家的支持!!!



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值