0

需要生效,该文本被另一个文本/uiimageview 打断,但无法理解它是如何工作的。例如,我需要制作类似于 ios7 状态栏的界面,其中操作员名称为“Oper ...”+图标+时间。所以我不能这样做

operatorName = [self getOperatorName];
    UILabel *operatorLabel = [[UILabel alloc] init];

    operatorLabel.backgroundColor = [UIColor clearColor];
    operatorLabel.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:kStatusBarFontOperatorSize];
    operatorLabel.textColor = kStatusBarTextColor;
    operatorLabel.shadowOffset = CGSizeMake(0.f, -0.6);
    operatorLabel.shadowColor = kStatusBarTextShadow;
    operatorLabel.adjustsFontSizeToFitWidth = YES;
    operatorLabel.text = operatorName;

    operatorLabel.frame = CGRectMake(0.0, 0.0, operatorStrSize.width, operatorStrSize.height);
    [operatorLabel sizeToFit];


    /* connection type */
    UIImageView *conImgView = [[UIImageView alloc] initWithImage:conImg];


    /* time in status bar */
    time = [self getStatusBarTime];
    UILabel *statusBarLabel = [[UILabel alloc] init];

    statusBarLabel.font = [UIFont fontWithName:@"HelveticaNeue-Medium" size:kStatusBarFontSize];
    statusBarLabel.textColor = kStatusBarTextColor;

    statusBarLabel.adjustsFontSizeToFitWidth = YES;
    statusBarLabel.text = time;


    int maxDistance = imgView.frame.size.width/2 - timeStrSize.width/2;

    int connectionPower = 44;
    double delimiter = 0;
    NSString *cName = [self returnChoosenConnectionName];
    if ([cName isEqualToString:@"Wi-Fi"]) {
        delimiter = 6.5;
    } else {
        delimiter = 9.5;
    }
    int fullLine = connectionPower + operatorLabel.frame.size.width + delimiter + conImgView.frame.size.width;


    if (fullLine > maxDistance) {

        // need to interrupt  text in operator name, but how ?

    } else {

        // all good placed
        x = 44.0;
        operatorLabel.frame = CGRectMake(x, 3.0, operatorStrSize.width  , operatorStrSize.height);
        [operatorLabel sizeToFit];


        NSString *cName = [self returnChoosenConnectionName];
        if ([cName isEqualToString:@"Wi-Fi"]) {
            x += operatorLabel.frame.size.width + 6.5;
        } else {
            x += operatorLabel.frame.size.width + 9.5;
        }

        conImgView.frame = CGRectMake(x, 0.0, conImgView.frame.size.width, conImgView.frame.size.height);

        [imgView addSubview:operatorLabel];
        [imgView addSubview:conImgView];
    }

    statusBarLabel.frame = CGRectMake(imgView.frame.size.width/2 - timeStrSize.width/2, 2.5, timeStrSize.width , timeStrSize.height);
    [imgView addSubview:statusBarLabel];

我需要的: 必须如何

我有的: 我有的

4

1 回答 1

1

我建议使用自动布局而不是尝试自己计算所有内容。您可以使用 XIB 并在其中设置约束,或者您可以以编程方式完成所有操作。这是一个示例代码,可以帮助您入门。它以编程方式创建控件并设置约束;它不使用 XIB。

- (void)viewDidLoad
{
    [super viewDidLoad];

    /*
     Uncomment this if you would like to translate your subviews vertically.

     UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 40, self.view.frame.size.width, 50)];
     [self.view addSubview:view];
     UIView *superview = view;
     */

    UIView *superview = self.view;

    UILabel *label1 = [[UILabel alloc] init];
    label1.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:14.0];
    label1.text = @"MTS";
    [label1 sizeToFit];
    [label1 setTranslatesAutoresizingMaskIntoConstraints:NO];


    UILabel *label2 = [[UILabel alloc] init];
    label2.font = [UIFont fontWithName:@"HelveticaNeue-Medium" size:14.0];
    label2.text = @"1:22 PM";
    [label2 sizeToFit];
    [label2 setTranslatesAutoresizingMaskIntoConstraints:NO];

    UIImageView *image1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"strength.png"]];
    UIImageView *image2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"carrier.png"]];
    [image1 setTranslatesAutoresizingMaskIntoConstraints:NO];
    [image2 setTranslatesAutoresizingMaskIntoConstraints:NO];
    [image1 sizeToFit];
    [image2 sizeToFit];

    [superview addSubview:image1];
    [superview addSubview:label1];
    [superview addSubview:image2];
    [superview addSubview:label2];

    NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(image1, image2, label1, label2);
    NSString *format = [NSString stringWithFormat:@"|-[image1(<=%f)]-[label1]-[image2(<=%f)]-[label2(>=20)]-|",
                        image1.frame.size.width, image2.frame.size.width];
    NSArray *constraints;
    constraints = [NSLayoutConstraint constraintsWithVisualFormat:format
                                                          options:NSLayoutFormatAlignAllCenterY
                                                          metrics:nil
                                                            views:viewsDictionary];

    [self.view addConstraints:constraints];
}

在代码中向布局添加视图时,iOS 将尝试将该视图的自动调整大小掩码转换为自动布局约束。这些自动生成的约束将与应用程序代码中添加的任何约束冲突。必须关闭翻译:

setTranslatesAutoresizingMaskIntoConstraints:NO

代码的结果是:

在此处输入图像描述

于 2013-10-02T01:58:53.703 回答