Updating the width of NSTextField based on length of string - objective-c

I want to make a label that change its size depending on the size of the string value that it will show. Currently I am doing this:
[tfScroll setStringValue:strScoller];
[tfScroll sizeToFit];
However this is not working. What am I missing?

If your tfScroll is NSTextField:
CGRect frame = tfScroll.frame;
frame.size.width = tfScroll.attributedStringValue.size.width+somepoints;//(somepoints=8)
tfScroll.frame = frame;
I did not find NSTextfield.contentSize.

If you trying to resize the fields's height, this may works:
CGRect frame = tfScroll.frame;
frame.size.height = tfScroll.contentSize.height;
tfScroll.frame = frame;

Related

Objective c uilabel width based on text length

I want to set the width of the uilabel based on the text length and then on top of that make it a little bit wider. The text might be different so I cannot just predefine the width of the uilabel. My approach to this is to get the width of the text and then add another 10.0 to it but it doesn't work.
CGSize tSize = [[self.label1 text] sizeWithAttributes:#{NSFontAttributeName:[self.label1 font]}];
CGFloat tWidth = tSize.width + 10.0;
self.label1.frame = CGRectMake(self.label1.frame.origin.x, self.label1.frame.origin.y, tWidth, self.label1.frame.size.height);
self.label1.backgroundColor = [UIColor blueColor];
Any advice is much appreciated.
You might want to use [yourLabel sizeToFit]; or [yourLabel sizeThatFits: <#your target size#>];
sizeToFit resizes your label within it's current frame.size according to it's current text , font and the numberOfLines you provided.
Then you can change the size
[yourLabel sizeToFit];
CGRect rect = yourLabel.frame;
rect.size.width += 10;
yourLabel.frame = rect;
sizeThatFits:(CGSize)size doesn't resize your label but it returns the size that fits the size you pass as parameter.
sizeToFit uses sizeThatFits:(CGSize)size internally.
You can change size by doing this
CGSize exampleSize = (CGSize){300, 300};
CGSize fittingSize = [yourLabel sizeThatFits: exampleSize];
CGRect rect = yourLabel.frame;
rect.size.width = fittingSize.width;
rect.size.height = fittingSize.height;
yourLabel.frame = rect;
This should be working fine even if you are working with yourLabel.attributedText

How to change the width of label once after its frame has been set? and to get the width of NSString

hope you are doing good.
I am setting a label and adjusting its frame ..
UILabel *mylabel=[[UILabel alloc]init];
mylabel.Text=#"This is my label";
mylabel.frame=CGRectMake(10,10,200,21);
now if I change the Label's text from any string set by me like this
NSString *mystring=#"This is my first string that is to be assigned to my label dynamilcally";
mylabel.Text=mystring;
mylabel.frame.size.width=mystring.length;
but nothing happens. The width of the label remains 200, as I set while at the time of initialisation.
This will also need to get the width of the mystring . How to get that?
Thank in anticipation for helping me a lot.
[myLabel sizeToFit] if you need it on one line. For multiple line labels you can use NSString's sizeWithFont:constrainedToSize:lineBreakMode: function to get size of your string and use it to modify your label's height & width.
e.g. code that gives you height for multiline label with fixed width of 200px:
CGSize textSize = {
200.0, // limit width
20000.0 // and height of text area
};
CGSize size = [descriptionString sizeWithFont:[UIFont systemFontOfSize:17.0] constrainedToSize:textSize lineBreakMode:UILineBreakModeWordWrap];
CGFloat height = size.height < 36? 36: size.height; // lower bound for height
CGRect labelFrame = [myLabel frame];
labelFrame.size.height = height;
[myLabel setFrame:labelFrame];
[myLabel setText:descriptionString];

What's the right way to dynamically give height to a UIScrollView depending on the content within?

Here my dilemma. I have 4 elements inside a UIScrollView.
1. Top most element is a UILabel that I give height dynamically depending upon the amount of content in it.
2. Second is a fixed height UILabel that I give position dynamically depending upon the height given to the upper UILabel
3. Third element is a UIImageView that again I have to give position dynamically depending upon the height given to the topmost UILabel
4. The fourth is a UIWebView, to which I gave both, height & position dynamically. (Height depending upon the content in it.. and position again depending on the height of topmost UILabel)
Finally, I dynamically give height to my UIScrollView to accomodate all of the above elements.
Here is the code I use in - (void)webViewDidFinishLoad:(UIWebView *)webView to accomplish all of the above.
//Adjust height of top-most UILabel
CGSize maximumLabelSize = CGSizeMake(300,9999);
CGSize expectedLabelSize = [item.label1 sizeWithFont:label1.font constrainedToSize:maximumLabelSize lineBreakMode:label1.lineBreakMode];
CGRect newFrame = label1.frame;
newFrame.size.height = 0;
newFrame.size.height = expectedLabelSize.height;
label1.frame = newFrame;
//Adjust position of second UILlabel
CGRect labelPosition = label2.frame;
labelPosition.size.height = 20;
labelPosition.origin.y = expectedLabelSize.height +14;
label2.frame = labelPosition;
//Add UIImageView and adjust it's position
UIImageView *image;
image = [[UIImageView alloc] initWithFrame:CGRectMake(0, expectedLabelSize.height +41, 320, 2)];
image.image = [UIImage imageNamed:#"image.png"];
[scrollView addSubview:image];
[image release];
//Adjust UIWebView height and position
CGRect frame = webView.frame;
frame.size.height = 0;
frame.origin.y = expectedLabelSize.height +48;
webView.frame = frame;
CGSize fittingSize = [webView sizeThatFits:CGSizeZero];
frame.size = fittingSize;
webView.frame = frame;
//Adjust Scrollview height
scrollView.contentSize = CGSizeMake(320, fittingSize.height +expectedLabelSize.height +48);
Finally, my problem is that when I first load this view, everything but the scrollview get's proper height & position. But, if I go back one view & open this view again, the scrollview has the desired height.
Any ideas what I might be doing wrong here?
My guess is that the scrollView variable isn't yet initialized when this first runs. Try setting a breakpoint somewhere in this code and checking if scrollView has a value or if it's just 0x00000000.

Stop two UILabel from overlapping each other

- (void)viewDidLoad
{
[super viewDidLoad];
objectiveLabel.text = objectstring;
objectiveLabel.lineBreakMode = UILineBreakModeWordWrap;
objectiveLabel.numberOfLines = 0;
[objectiveLabel sizeToFit];
vocabularyLabel.text = vocabularystring;
vocabularyLabel.lineBreakMode = UILineBreakModeWordWrap;
vocabularyLabel.numberOfLines = 0;
[vocabularyLabel sizeToFit];
}
Can Someone point me to the right direction on how to get Label 1 to push or move Label 2 instead of overlapping?
If you want to "push or move" a UILabel, you just set the frames accordingly. So look at objectiveLabel.frame, add the objectiveLabel.frame.origin.x plus objectiveLabel.frame.size.width, and that's the minimum of what you should set vocabularyLabel.frame.origin.x to. If you want vocabularyLabel to adjust for the width of not only the objectiveLabel frame width, but actually to tweak according to how objectstring will be rendered in objectiveLabel, you calculate the size of objectiveLabel's width by:
CGSize size1 = [objectstring sizeWithFont:objectiveLabel.font
constrainedToSize:objectiveLabel.frame.size
lineBreakMode:objectiveLabel.contentMode];
CGRect frame2 = vocabularyLabel.frame;
frame2.origin.x = objectiveLabel.origin.x + size1.width;
vocabularyLabel.frame = frame2;
This won't factor in the minimumFontSize of the labels, though. But if the minimumFontSize is the same size as the font size, then you should be golden.
What you want is called Auto Layout.

How to set size of UIScrollView frame programmatically?

I am newbie so my question can be really obvious, but I didn't find any solution so far.
I did in IB UIScrollView and connected it with File's Owner.
I would like to set now the frame size of my UIScrollView (named ScrollView).
const CGFloat BoardWidth = 320;
const CGFloat BoardHeight = 400;
//I tried this way but 'Expression is assignable' - said Xcode
ScrollView.frame.size.width = BoardWidth;
ScrollView.frame.size.height = BoardWidth;
So how can I the easiest set own sizes of ScrollView frame?
CGRect scrollFrame = CGRrectMake(x, y, w, h);
ScrollView.frame = scrollFrame;
Or if you need only to change the width and height;
CGRect scrollFrame;
scrollFrame.origin = ScrollView.frame.origin;
scrollFrame.size = CGSizeMake(w, h);
ScrollView.frame = scrollFrame;
Edit keeping the center unchanged:
CGRect scrollFrame;
CGFloat newX = scrollView.frame.origin.x + (scrollView.frame.size.width - w) / 2;
CGFloat newY = scrollView.frame.origin.y + (scrollView.frame.size.height - y) / 2;
scrollFrame.origin = CGPointMake(newX, newY);
scrollFrame.size = CGSizeMake(w, h);
scrollView.frame = scrollFrame;
you need to do this, for example.
ScrollView.frame = CGRectMake(0, 0, BoardWidth, BoardHeight);
Once done you need also to set contentSize property. It's the area that 'extends' behind the frame of your UIScrollView.
If you want to scroll only horizontally:
ScrollView.contentSize = CGSizeMake(w * BoardWidth, BoardHeight);
If you want to scroll only vertically:
ScrollView.contentSize = CGSizeMake(BoardWidth, h * BoardHeight);
If you want to scroll both horizontally and vertically:
ScrollView.contentSize = CGSizeMake(w * BoardWidth, h * BoardHeight);
where w and h are generic values to increment your width and/or your height.
If you want have an horizontal (for example) discrete scroll (e.g. you have some pages with the same dimension of your screen).
ScrollView.pagingEnabled = YES;
ScrollView.contentSize = CGSizeMake(p * BoardWidth, BoardHeight);
where p is the number of pages you want to display.
Hope it helps.
You can do it as below,
scrollView.frame = CGRectMake(xOrigin,yOrigin,width,height);
If you are looking for content size of scrollview then it as below,
scrollView.contentSize = CGSizeMake(width,height);
CGRect Frame = ScrollView.frame
Frame.frame.size.width = BoardWidth;
Frame.frame.size.width = BoardWidth;
ScrollView.frame = Frame;
Since frame is a structure..you can't set the frame of an object directly.. You will have to make a Cgrect and then manipulate it.
using contentSize property
ScrollView.contentSize = CGSizeMake(320, 520);
it needs to be bigger than the frame so it actually scrolls.