美文网首页
iOS UIlabel添加照片 UITextView 文字/照片

iOS UIlabel添加照片 UITextView 文字/照片

作者: smile_frank | 来源:发表于2019-03-07 23:19 被阅读0次

    iOS UIlabel添加照片 UITextView 文字/照片点击事件

    根据产品需求需要文本的行末添加一个图标或者图片,并可实现部分文字的点击和照片的点击事件

    效果图

    方案1:添加的图片是iconfont图标。iconfont,从字面上就能理解它就是字体,让开发者像使用字体一样使用图标。

        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50, 160, 280, 40)];
        [self.view addSubview:label];
        label.font = [UIFont fontWithName:@"iconfont" size:15];//设置label的字体
        label.text = @"这是用label显示的iconfont  \U0000e60c";
    

    如果不显示,请检查一下iconfont的资源文件是否在工程目录下。

    检查资源文件 plist文件配置

    方案2:通过 NSMutableAttributedString 在文本末尾添加一个照片

        UILabel *titleLabel = [[UILabel alloc]init];
        titleLabel.frame = CGRectMake(10, 300, 100, 200);
        titleLabel.backgroundColor = [UIColor redColor];
        [self.view addSubview:titleLabel];
    
        NSTextAttachment *attachment = [[NSTextAttachment alloc]init];
        attachment.image = [UIImage imageNamed:@"eys"];
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:@"我的后面跟着一个照片"];
        [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0, 10)];
        [attributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20] range:NSMakeRange(0, 10)];
        NSAttributedString *attachmentString = [NSAttributedString attributedStringWithAttachment:attachment];
        [attributedString insertAttributedString:attachmentString atIndex:10];
        titleLabel.attributedText = attributedString;
        titleLabel.numberOfLines = 0;
        [titleLabel sizeToFit];
    

    UITextView 文字/照片点击事件

     NSMutableAttributedString *attributeString2 = [[NSMutableAttributedString alloc]initWithString:@"哈哈哈哈哈哈哈哈,你还没有中奖,赶快去参加活动"];
        [attributeString2 addAttribute:NSLinkAttributeName value:@"click://" range:[[attributeString2 string]rangeOfString:@"参加活动"]];
        NSTextAttachment *attachment2 = [[NSTextAttachment alloc]init];
        attachment2.image = [UIImage imageNamed:@"eys"];
        attachment2.bounds = CGRectMake(0, 0, 15, 15);
        NSAttributedString *attachmentString2 = [NSAttributedString attributedStringWithAttachment:attachment2];
        NSMutableAttributedString *imgAttribute = [[NSMutableAttributedString alloc]initWithAttributedString:attachmentString2];
        [imgAttribute addAttribute:NSLinkAttributeName value:@"IMGURL://" range:NSMakeRange(0, imgAttribute.length)];
        [attributeString2 insertAttributedString:imgAttribute atIndex:[attributeString2 string].length];
    
        UITextView *textView  = [[UITextView alloc]initWithFrame:CGRectMake(20,400 , 200, 220)];
        [self.view addSubview:textView];
        textView.attributedText = attributeString2;
        textView.linkTextAttributes = @{NSForegroundColorAttributeName:[UIColor blueColor],NSUnderlineColorAttributeName:[UIColor grayColor]};
        textView.font = [UIFont systemFontOfSize:18];
        textView.textAlignment = NSTextAlignmentLeft;
        textView.backgroundColor = [UIColor brownColor];
        textView.delegate = self;
        textView.editable = NO;
        textView.scrollEnabled = NO;
        [self.view addSubview:textView];
        self.curentView = textView;
    

    UItextView的代理方法

    -(BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange interaction:(UITextItemInteraction)interaction {
        
        NSLog(@"打印获取到的url的字符串为----%@",[URL scheme]);
        
        if ([[URL scheme] isEqualToString:@"click"]) {
    
            NSLog(@"执行了文字的点击事件");
            UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"文字被点击了" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"我知道了", nil];
            [alertView show];
            
            return NO;
            
        }else if ([[URL scheme] isEqualToString:@"IMGURL"]) {
            
            UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"照片被点击了" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"我知道了", nil];
            [alertView show];
            NSLog(@"执行了点击末尾的照片");
            
        }
        return YES;
     
    }
    
    

    相关文章

      网友评论

          本文标题:iOS UIlabel添加照片 UITextView 文字/照片

          本文链接:https://www.haomeiwen.com/subject/xyikpqtx.html