gronzzz
gronzzz

Reputation: 615

text format trouble objective-c

need to make effect, that text is interrupted by another text/uiimageview, but cannot understand how does it works. For example i need to make interface similar to ios7 status bar where operator name such a "Oper ..." + icon + time. So i cannot do this right way

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

What i need: how must to be

what i have: what i have

Upvotes: 0

Views: 236

Answers (1)

dmitri
dmitri

Reputation: 3294

I would suggest to use autolayout rather than trying to calculate everything yourself. You may use XIB and set constraints in there or you may do everything programmatically. Here is a sample code that will get you started. It creates controls and sets constraints programatically; it doesn't use 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];
}

When adding views to a layout in code iOS will attempt to convert the autosizing mask for that view to auto layout constraints. Those auto-generated constraints will conflict with any constraints added within the application code. It is essential to turn the translation off:

setTranslatesAutoresizingMaskIntoConstraints:NO

The result of the code is:

enter image description here

Upvotes: 1

Related Questions