get the width of NSAttributedString #334

Closed
shishirui opened this Issue Mar 10, 2013 · 10 comments

Projects

None yet

3 participants

@shishirui

Thank you for your excellent project!

I have a UIImageView(bubble view) in root view. the UIImageView has a DTAttributedLabel as its subview.
The DTAttributedLabel could be one line or multi-line。When it is one line, I want to display the bubble view's size just fit to the NSAttributedString.

so, I need to set width of the bubble view, and I need to know the real width of the NSAttributedString. How could I do that?

Thank you!

aa

@odrobnik
Collaborator

I will add a method next week to get the minimum size needed to fit a layout frame's text.

@shishirui

Thank you! Waiting for your good news!

@odrobnik
Collaborator

@shishirui Please try out the latest stuff in 9ca69be.

To achieve the desired effect I think you would:

  • Initialize a DTAttributedLabel with the maximum width
  • set the attributedText
  • call sizeToFit

If the text is long enough to break on a line then the width will be close to the original width, but never more. If the text is short then the width will be reduced to fit.

Right now this causes two layout passes, but for short texts like you would have in a label the performance hit should not be noticeable.

@shishirui

@Cocoanetics
I have try the lastest version of your code. It is works well for me. Thank you for your hard work!

The method sizeToFit can resize the DTAttributedLabel to the suitable width, but not height. It is enough to me!

Thanks again.

@odrobnik
Collaborator

@shishirui actually it should also shrink to the minimum height. I need to change that.

@shishirui

@Cocoanetics Wow... If it can shrink to the minimum height, it well be perfect!

@odrobnik odrobnik added a commit that referenced this issue Mar 11, 2013
@odrobnik odrobnik adjusted intrinsicContentFrame to calculate height also to fit
previously it didn't adjust the height to be as small as possible #334
f18b857
@odrobnik
Collaborator

I made a change that should cause a sizeToFit on DTAttributedLabel to also shrink to minimum height.

@odrobnik
Collaborator

@shishirui please confirm that it now shrinks the height and if you are happy close the issue.

@shishirui

@Cocoanetics It can shrinks the height now. So wonderful! Thank you so much!

@shishirui shishirui closed this Mar 12, 2013
@odrobnik odrobnik was assigned Mar 23, 2013
@wenzhaot

I try to use this, But the problem is how do I calculate the height in advance and not initailzed a DTAttributedLabel

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment