Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

QMUITextView 在使用 placeholder 的情况下调用 sizeToFit 会出现 NaN #1258

Closed
MoLice opened this issue Jun 9, 2021 · 1 comment
Closed
Labels

Comments

@MoLice
Copy link
Collaborator

MoLice commented Jun 9, 2021

QMUITextView *textView = [[QMUITextView alloc] init];
textView.placeholder = @"abc";
[textView sizeToFit];

crash 信息:
image

crash 原因:
image
image

  1. QMUITextView 里对 sizeThatFits: 做了处理,width 为 0 时会把 width 改为 CGFLOAT_MAX,以便后续的计算能得到“内容的完整宽度”,而如果你在外面调用 sizeToFit,那么传进来 sizeThatFits: 的宽度就为0,于是被改为 CGFLOAT_MAX。
  2. 发现此时正在用 placeholder,于是用这个 CGFLOAT_MAX 的 size 去计算 placeholder 的宽高。
  3. limitWidth 这里没区分当前是否为 CGFLOAT_MAX,所以减去其他间距后得到的 limitWidth 依然是 MAX 值。
  4. 将 MAX 拿去 setFrame:,就会导致各种计算异常,最终出现 NaN。

注意,由于 UIView (QMUI) 里对 setFrame: 带有 NaN 的值,Release 下会做保护,改为 0,因此发布的正式包不会出现这种问题,Debug 时会通过 QMUIConsole 或 NSAssert 体现。

由于这里用 limitWidth 也不是很妥当,所以这里改为用 placeholderLabel sizeThatFits 返回的 size.width 作为最终宽度,从而避免这个问题。

其他信息

  • QMUI 版本: [4.2.3]
@MoLice MoLice added the bug label Jun 9, 2021
@MoLice
Copy link
Collaborator Author

MoLice commented Aug 31, 2021

已发布 4.3.0 修复该问题。

@MoLice MoLice closed this as completed Aug 31, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant