简体   繁体   English

iOS:CGRectMake不会更改UILabel的位置

[英]iOS: CGRectMake doesn't change position of a UILabel

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> 这是我要实现的文本输出:12月2日<还剩30天>

the <> enclosed text is in smaller letters an in a different UI Label. 包含的<>文本用较小的字母表示,并且位于其他UI标签中。

Since the month name is dynamic and the width changes I've tried using CGRectMake to alter the position of the <> enclosed text. 由于月份名称是动态的,并且宽度已更改,因此我尝试使用CGRectMake更改<>包含文本的位置。 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_date是包含月份和日期的变量。 label_days_left is the label variable which I am trying to place next to the month. label_days_left是我要放在月份旁边的标签变量。

EDIT 2: 编辑2: 镜框

EDIT 3: I created a new project. 编辑3:我创建了一个新项目。 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? 我在做什么错?

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. 如果是这样,那可能是您的问题,因为您不必担心使用自动版面设置来设置框架,而是设置约束。

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.... 请尝试一次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM