Skip to content

cannot get the right character position of UILabel in TruncateTail mode #6

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

homekuanyi
Copy link

Hi, thanks for your great work.

I found the layoutManager will not auto-wrap attributedString when lineBreakMode is TruncateTail. So we must reset the lineBreakMode of attributedString.

fix: @selector(characterIndexAtPoint:) return the right index of UILabel in Truncate* mode when auto-wrap.

I hope you would accept it.

@AliSoftware
Copy link
Owner

It seems strange that when the lineBreakMode is one of the *Truncate* ones, you actually change it to WordWrap in the inner AttributedString, and not *Truncate*.

  • Maybe indeed layoutManager doesn't auto-wrap with those modes (is it an Apple bug, should we file a radar?),
  • but changing it to Word Wrap probably won't work (it may seem to work in the example you took in the XIB, but the result would mostly depend on the text content and the words it contains; e.g. if you have very long words in the beginning of your text, then a lot of small words in the end, and your label is set to TruncateHead, then the computation of text positions based on Word Wrap won't match the text displayed as TruncateHead

So in practice I'm not sure that really fixes the issue (if it seems so, it's probably because you were lucky by testing it with an example text that leads to close-enough results, which won't be the case for text with very-uneven repartition of word lengths)

@homekuanyi
Copy link
Author

thanks for answer.

Actually i was trying out to find a way to remove the lineBreakMode property of attributedString at the first time, but i failed to do it. i'm not familiar with NSAttributedString, if you know how to remove, tell me please.

Since apple doesn't give any api of UILabel's layoutManager, i don't know it's a bug or not. And Truncate mode will not certainly wrap right?

There is two lineBreakMode Settings, one is TextContainer, and attributedString the other. There must be only one lineBreakMode works. For my testing, i think UILabel choose TextContainer one, on the other hand our own layoutManager choose attributedString one. So by resetting attributedString's lineBreakMode (or remove it or ignore it, how? ), our own layoutManager will use TextContainer's lineBreakMode. Truncate mode wraps like WordWrap does, so i reset it and everything works ok in my "lucky" testing.

I'm very sorry that i cannot give enough testings in this pull request, but i have applied it in my new project, if any things looks strange i will give a feedback.

@homekuanyi
Copy link
Author

I think layoutManager uses attributedString to calculate its line number ( if attributedString truncates there is only one line ), then uses TextContainer to do lineBreak, maxNumberOfLine and other display works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants