{"action":"created","issue":{"url":"https://api.github.com/repos/QMUI/QMUI_iOS/issues/377","repository_url":"https://api.github.com/repos/QMUI/QMUI_iOS","labels_url":"https://api.github.com/repos/QMUI/QMUI_iOS/issues/377/labels{/name}","comments_url":"https://api.github.com/repos/QMUI/QMUI_iOS/issues/377/comments","events_url":"https://api.github.com/repos/QMUI/QMUI_iOS/issues/377/events","html_url":"https://github.com/QMUI/QMUI_iOS/issues/377","id":347582825,"node_id":"MDU6SXNzdWUzNDc1ODI4MjU=","number":377,"title":"QMUITextView 字数限制导致的 crash","user":{"login":"ziecho","id":16473618,"node_id":"MDQ6VXNlcjE2NDczNjE4","avatar_url":"https://avatars2.githubusercontent.com/u/16473618?v=4","gravatar_id":"","url":"https://api.github.com/users/ziecho","html_url":"https://github.com/ziecho","followers_url":"https://api.github.com/users/ziecho/followers","following_url":"https://api.github.com/users/ziecho/following{/other_user}","gists_url":"https://api.github.com/users/ziecho/gists{/gist_id}","starred_url":"https://api.github.com/users/ziecho/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ziecho/subscriptions","organizations_url":"https://api.github.com/users/ziecho/orgs","repos_url":"https://api.github.com/users/ziecho/repos","events_url":"https://api.github.com/users/ziecho/events{/privacy}","received_events_url":"https://api.github.com/users/ziecho/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2018-08-04T03:34:51Z","updated_at":"2018-08-04T14:03:05Z","closed_at":null,"author_association":"NONE","body":"### 请填写运行环境\r\n\r\n- [x] 设备：iPhone X\r\n- [x] 系统：`iOS 12`\r\n- [x] Xcode 版本：`9.3`\r\n- [x] QMUI iOS 版本：`2.7.4`\r\n\r\n### 请描述具体问题\r\n操作步骤\r\n1、Demo 中 QMUITextView，输入一打段文字，达到最大字数限制（ Demo 中是 100 ）\r\n2、随便复制几个文字，把光标移动到最后，长按，粘贴。（此时因为达到最大字数了，所以是不能粘贴的）\r\n3、摇晃手机，使系统弹出「撤销正在键入」的 alert，点击「撤销」就 crash 掉了\r\n\r\n\r\n#### crash 原因\r\n\r\n系统撤销的时候，实际上是对用户上次 copy 的 range 进行删除，而之前因为对 text 进行裁剪，导致了这段 range 越界后 crash。\r\n\r\n\r\n#### 修复方法\r\n\r\n在\r\n```objc\r\n- (BOOL)textView:(QMUITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text { \r\n```\r\n这个方法中添加\r\n```objc\r\n    BOOL isDeleting = range.length > 0 && text.length <= 0;\r\n    if (isDeleting && range.location + range.length > textView.text.length) {\r\n        return NO;\r\n    }\r\n```"},"comment":{"url":"https://api.github.com/repos/QMUI/QMUI_iOS/issues/comments/410451663","html_url":"https://github.com/QMUI/QMUI_iOS/issues/377#issuecomment-410451663","issue_url":"https://api.github.com/repos/QMUI/QMUI_iOS/issues/377","id":410451663,"node_id":"MDEyOklzc3VlQ29tbWVudDQxMDQ1MTY2Mw==","user":{"login":"MoLice","id":1190261,"node_id":"MDQ6VXNlcjExOTAyNjE=","avatar_url":"https://avatars2.githubusercontent.com/u/1190261?v=4","gravatar_id":"","url":"https://api.github.com/users/MoLice","html_url":"https://github.com/MoLice","followers_url":"https://api.github.com/users/MoLice/followers","following_url":"https://api.github.com/users/MoLice/following{/other_user}","gists_url":"https://api.github.com/users/MoLice/gists{/gist_id}","starred_url":"https://api.github.com/users/MoLice/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/MoLice/subscriptions","organizations_url":"https://api.github.com/users/MoLice/orgs","repos_url":"https://api.github.com/users/MoLice/repos","events_url":"https://api.github.com/users/MoLice/events{/privacy}","received_events_url":"https://api.github.com/users/MoLice/received_events","type":"User","site_admin":false},"created_at":"2018-08-04T14:03:05Z","updated_at":"2018-08-04T14:03:05Z","author_association":"MEMBER","body":"确实，感谢反馈，这种操作路径较为少见，QMUITextView 内部将会对这种情况做保护。\r\n\r\n另外，猜测这可能是系统撤销输入的 bug，因为当我们在 `[UITextViewDelegate textView:shouldChangeTextInRange:replacementText:]` 方法里 `return NO` 时，该次输入就不应该被记录，因为 `return NO` 的语义本就代表着阻止输入。但系统的摇一摇撤销输入看起来并没有兼容这种情况，导致传进来一个本就不存在的 range，导致出现这样的问题。"}}
{"actor":{"display_login":"MoLice"}}