Question

I am trying to re-size a label according to text in it (or max width I am allowing i.e.108) and place a button just next to it, code below is actually giving satisfying results while debugging but width of label is not changing accordingly. Is there any special settings to do in an xib or any thing else please help Iam.

        NSDictionary *attrib=@{NSFontAttributeName:locFeedcell.lbl_location.font};
        CGFloat width=[locFeedcell.lbl_location.text sizeWithAttributes:attrib].width;
        CGRect newLabelframe=locFeedcell.lbl_location.frame;
        CGRect newButtonFrame=locFeedcell.btn_map.frame;
        if (width>=108.00f) {
            newLabelframe.size.width=108.00;
        } else {
            newLabelframe.size.width=width;
            newButtonFrame.origin.x=locFeedcell.lbl_location.bounds.origin.x+width+4.00;
        }
        locFeedcell.lbl_location.frame=newLabelframe;
        locFeedcell.btn_map.frame=newButtonFrame; 

here is snapshot of xib file.

here is snapshot of xib file.

Was it helpful?

Solution

Update: Instead of using locFeedcell.lbl_location.bounds.origin.x+width use locFeedcell.lbl_location.frame.origin.x+width. bounds.origin.x is always 0.

Maybe Auto Layout is enabled in your .xib file? Check the file inspector tab in the utilities pane. If this doesn't help, can you post the values for the frame before and after you set them? Remember the memory address for your label and button and print their frames again after everything is laid out.

A quick tip: You can use CGRectGetMaxX(locFeedcell.lbl_location.frame) instead of locFeedcell.lbl_location.bounds.origin.x+width to get the right edge from the frame.

OTHER TIPS

I don't see any logic problem in your code. There are two things you may have a check:

  1. Change the maximum width to twice or more wider to make the change noticeable, so it can be easier verified;
  2. Make sure you are in UI thread when you set new frame to UI elements.

What about using Autolayout constraints?

https://developer.apple.com/library/ios/documentation/userexperience/conceptual/AutolayoutPG/Introduction/Introduction.html

You have 3 objects:

  1. was recently at --> let's call it lbl_recentlyAt
  2. lbl_location
  3. btn_map

You will need 3 constraints:

  1. Set the fix position of your recently add label:

|-X-[lbl_recentlyAt]

  1. Set the variable position of the location label and the button, let's say with 20p space

[lbl_recentlyAt]-20-[lbl_location]-20-[btn_map]

You may need to call setNeedsLayout method of your cell when the lbl_location width changes to update the layout: https://developer.apple.com/library/ios/documentation/uikit/reference/uiview_class/uiview/uiview.html#//apple_ref/occ/instm/UIView/setNeedsLayout

[cellView setNeedsLayout]

Hope this helps!

//use this for custom font

CGFloat width = [label.text sizeWithFont:[UIFont fontWithName:@"ChaparralPro-Bold" size:40 ]].width;

//use this for system font

CGFloat width = [label.text sizeWithFont:[UIFont systemFontOfSize:40 ]].width;

label.frame = CGRectMake(point.x, point.y, width,height);

//point.x, point.y -> origin for label;

//height -> your label height;

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top