Stop two UILabel from overlapping each other - objective-c

- (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.

Related

Updating the width of NSTextField based on length of string

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;

UILabel text gets cut off

I'm trying to dynamicly set label size. It works in a strange way, i get some of the text cut off.
I first set my label text and then try to resize it like this way.
_switch2Label.text = #"Call on alarm, there will be no call if other user of alarm system will recieve an alarm call and confirm (answer) it by pressing 0#";
_switch2Label.numberOfLines = 0;
[self newFrame:_switch2Label];
- (void) newFrame:(UILabel *) label
{
CGSize maxSize = self.view.bounds.size;
maxSize.width = maxSize.width - 30;
CGSize labelSize = [label.text sizeWithFont:label.font constrainedToSize:maxSize lineBreakMode:label.lineBreakMode];
CGRect newFrame = label.frame;
newFrame.size.height = labelSize.height;
label.frame = newFrame;
}
I only get three lines of text, while five is needed for this label. Maybe anyone could see my mistake here? If I add more text to label it gets shown, yet still about two lines of the label text gets cutt off.
I have changed your method...Please check it..it may help you..
- (void) newFrame:(UILabel *) label
{
CGSize constraint = CGSizeMake(300, 1000.0f);
CGSize size_txt_overview1 = [label.text sizeWithFont:[UIFont fontWithName:#"Arial Rounded MT Bold" size:15] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
label.frame = CGRectMake(20,20, size_txt_overview1.width, size_txt_overview1.height+15);
}
_switch2Label.text = #"Call on alarm, there will be no call if other user of alarm system will recieve an alarm call and confirm (answer) it by pressing 0#,";
_switch2Label.numberOfLines = 0;
[self newFrame:_switch2Label];
- (void) newFrame:(UILabel *) label
{
CGSize maximumSize = CGSizeMake(label.frame.size.width, 10000);
//maxSize.width = maxSize.width - 30;
CGSize labelSize = [label.text sizeWithFont:label.font constrainedToSize:maximumSize lineBreakMode:UILineBreakModeWordWrap];
CGRect newFrame = label.frame;
newFrame.size.height = labelSize.height;
label.frame = newFrame;
}
Use this code blocks , may help u.
Why programatically resizing the label? Is this something you cannot do in IB or using autorezizeMask?
The label's constraint size isn't getting calculated as you intend, currently, your code is constraining the label height to the view's bound's height. Changing your maxSize instance to:
CGSize maxSize = CGSizeMake(self.view.bounds.size.width - 30, MAXFLOAT);
CGSize labelSize = ...
Doing so will ensure that the constraint is not bound by your view bounds. You may also want to consider setting the clipsToBounds property of your view if you want the label to be able to extend past your view's bounds.

Scrollview resize content issue

I have an issue with scroll view. I have implemnted two font resize buttons to increase or decrease text font size for the text view embedded in the scroll view. But after decreasing font size, I calculate the scroll view content and reize it to adapt to the new text view height. But every time I modify text font, it scrolls down for every step.
Here is the code:
- (IBAction)decreaseText:(id)sender {
int MyTextSizeMin = 8;
int MyTextSizeMax = 20;
// disable buttons when they're out of the range.
BOOL smallerEnabled = textFontSize > MyTextSizeMin;
BOOL biggerEnabled = textFontSize < MyTextSizeMax;
[self.decreaseText setEnabled:smallerEnabled];
[self.increaseText setEnabled:biggerEnabled];
height =0;
CGFloat secondLabelsize = texto.font.pointSize;
[texto setFont:[UIFont systemFontOfSize:(secondLabelsize-1)]];
CGRect frame = texto.frame;
frame.size.height = texto.contentSize.height;
self.texto.frame = frame;
height +=texto.contentSize.height;
height += self.imagen.image.size.height;
height += self.fecha.frame.size.height;
height += self.titulo.frame.size.height;
height += 200;
self.scrollView.contentSize=CGSizeMake(300.0,height);
[self.scrollView setNeedsDisplay];
}
How can I disable this undesired scroll?
Many thanks!
Try it may it help full for you.
[scrollView scrollRectToVisible:CGRect animated:BOOL];

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.

Scrolling view like app details screen

I am trying to figure out the best way to create a view like the app details screen in the AppStore app. I want a thumbnail and text content under it that all scrolls if the content is too long. Is this done in a tableview or a scrollview?
I've made one in a scrollview. I calculated the size of each element's frame from this method:
- (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size lineBreakMode:(UILineBreakMode)lineBreakMode
I kept a running total of the y size by adding it on after each label. At the end, if the scroll view was over a certain size (the length of my page) I gave that size to the scroll view, adding a little on the end so it wouldn't bump against the bottom.
Here's some code:
int currentYPos;
CGSize maximumSize = CGSizeMake(300, 9999);
[scrollView setCanCancelContentTouches:NO];
scrollView.indicatorStyle = UIScrollViewIndicatorStyleDefault;
scrollView.clipsToBounds = YES;
scrollView.scrollEnabled = YES;
scrollView.pagingEnabled = NO;
// set the title frame size
self.titleLabel.text = self.title;
CGSize titleSize = [self.titleLabel.text sizeWithFont:self.titleLabel.font
constrainedToSize:maximumSize
lineBreakMode:self.titleLabel.lineBreakMode];
currentYPos = titleSize.height + 20;
CGRect titleFrame = CGRectMake(10, 0, 300, currentYPos);
self.titleLabel.frame = titleFrame;
Note that many of the titleLabel properties were set on a label in IB.