Question

I've created a label via storyboard. The order is like this: View -> Scroll View -> Bunch of labels.

Here is the text output I want to achieve: 2 December <30 days left>

the <> enclosed text is in smaller letters an in a different UI Label.

Since the month name is dynamic and the width changes I've tried using CGRectMake to alter the position of the <> enclosed text. But it isn't working. I tried changing the parameters of the function but the label stays put. It doesn't move from its initial position set in storyboard.

label_days_left.frame = CGRectMake(50, 10, 100, 99);

EDIT:

- (void) alignDaysLeft{
    //code to align the label just after the match date
    CGFloat dateLabelWidth = [label_date.text sizeWithFont:label_date.font].width;
    CGFloat dateLabelHeight = [label_date.text sizeWithFont:label_date.font].height;
    CGFloat someGap = 0;
    CGFloat daysLeftX = label_date.frame.origin.x + dateLabelWidth + someGap;
    CGFloat daysLeftY = label_date.frame.origin.y;
    [label_date sizeToFit];
    //label_days_left.frame = CGRectMake(daysLeftX, daysLeftY, 100, dateLabelHeight);
    label_days_left.frame = CGRectMake(50, 10, 100, 99);
    label_days_left.text = [NSString stringWithFormat:@"lolo"];
    NSLog(@"date width - %f", daysLeftX);

}

label_date is the variable which contains the month and day. label_days_left is the label variable which I am trying to place next to the month.

EDIT 2: Frames

EDIT 3: I created a new project. Dragged a label on the view. Created an outlet. Synthesized it.

And used the following code to move it -

[labia setFrame:CGRectMake(0, 100, 5, 99)];

It didn't work. What mistake am I doing?

Was it helpful?

Solution

Do you have AutoLayout turned on? If so then that might be your problem, as you are not supposed to worry about setting the frame with AutoLayout, and instead set constraints.

OTHER TIPS

NSString *someString = [NSString stringWithFormat:@"%@",label_date.text]
UIFont *yourFont = [UIFont systemFontOfSize:22.0];
CGSize stringBoundingBox = [someString sizeWithFont:yourFont];
[label_days_left setFrame:CGRectMake(label_days_left.frame.origin.x, 10, stringBoundingBox.width, 99)];
[label_days_left setText:someString];

Pls try this once....

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