2013-09-30 75 views
0

需要生效,該文本被另一個文本/ uiimageview中斷,但無法理解它是如何工作的。例如,我需要使界面類似於ios7狀態欄,其中操作員名稱如「Oper ...」+圖標+時間。所以我不能做這種正確的方式文本格式問題objective-c

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]; 

我需要什麼: how must to be

我有什麼: what i have

+0

我建議你使用自動佈局,而不是做佈局自己。 – dmitri

+0

不幸的是,我不能使用此xib和自動佈局 – gronzzz

+0

xib不是必需的。您可以編程方式創建和設置約束。 – dmitri

回答

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 

代碼的結果是:把翻譯關重要的是

enter image description here

+0

非常感謝您的回答!不錯的變體,但我已經寫了我的alghoritm來計算元素之間的距離,所以它適用於每個位置,當需要裁剪操作員標籤時,這可能不是很好的練習,但像一個閃耀的作品) – gronzzz