博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS微信朋友圈 评论点击姓名功能
阅读量:7239 次
发布时间:2019-06-29

本文共 1494 字,大约阅读时间需要 4 分钟。

可以使用PPLabel来实现这个功能,下载代码https://github.com/petrpavlik/PPLabel。

这个demo有两个小bug:

1、如果最后一个单词后面没有空格字符,那么不能点击。

修改办法:在ViewController中,有一个代理方法:

- (void)highlightWordContainingCharacterAtIndex:(CFIndex)charIndex 

将这个代理方法中的代码:

 if (end.location == NSNotFound) {

        end.location = string.length-1; //last word was selected

    }

修改成:

 if (end.location == NSNotFound) {

        end.location = string.length//last word was selected

    }

 

2、这个label中的最后一行无法点击

修改办法:

在PPlabel.m中,有以下代码:

- (CGRect)textRect {

    CGRect textRect = [self textRectForBounds:self.bounds limitedToNumberOfLines:0];

    textRect.origin.y = (self.bounds.size.height - textRect.size.height)/2;

    if (self.textAlignment == NSTextAlignmentCenter) {

        textRect.origin.x = (self.bounds.size.width - textRect.size.width)/2;

    }

    if (self.textAlignment == NSTextAlignmentRight) {

        textRect.origin.x = self.bounds.size.width - textRect.size.width;

    }

    

    return textRect;

}

将其修改为:

- (CGRect)textRect {

    CGRect textRect = [self textRectForBounds:self.bounds limitedToNumberOfLines:0];

    textRect.origin.y = (self.bounds.size.height - textRect.size.height)/2;

    textRect.size.height += 100;

    if (self.textAlignment == NSTextAlignmentCenter) {

        textRect.origin.x = (self.bounds.size.width - textRect.size.width)/2;

    }

    if (self.textAlignment == NSTextAlignmentRight) {

        textRect.origin.x = self.bounds.size.width - textRect.size.width;

    }

    

    return textRect;

}

为什么要+100?

因为用CoreText在计算点击的字符位置时,textRect大小造成最后一行无法统计,将高度增加一部分,可以避免在计算点击的字符位置时造成最后一行遗漏。

转载于:https://www.cnblogs.com/zengyanzhi/p/3862595.html

你可能感兴趣的文章
基于redis的缓存机制的思考和优化
查看>>
IBM DS 5300存储硬盘故障数据恢复详解
查看>>
企业生产环境不同业务,系统分区建议(自定义分区布局)
查看>>
使用Verilog实现FPGA双列电梯控制系统
查看>>
编写安装配置mail服务脚本
查看>>
<Power Shell>13 powershell三个实用特性和功能实例
查看>>
spring cloud使用Feign实现远程接口的调用
查看>>
Delphi 中使用 ADO 方法打开 MySQL5.0 数据库并避免汉字乱码
查看>>
定制bash命令行提示符
查看>>
DNS Bind详解
查看>>
你必须要知道的数据备份工具rsync
查看>>
oracle关于坏块的修复一
查看>>
lzg_ad:FBWF配置详解
查看>>
gitlab部署、配置更改、备份及恢复
查看>>
关于共享SQL——窥视解析
查看>>
linux和windows同步数据 cwrsync client to rsync server
查看>>
浅谈ListBox在Windows Phone 7 中的使用
查看>>
ECMAScript 6 -- 字符串的扩展
查看>>
android apk--程序发布前的准备
查看>>
现任明教教主CCNP Security Secure第四天第三部分
查看>>