Question

I have problem with calculating rect from given string.

Here are my strings:

_name = @"Any veryveryveryvery key here:";
_value = @"Any very very very very long value";

and my layoutSubviews method:

- (void)layoutSubviews {
    [super layoutSubviews];

    CGRect rect = self.contentView.bounds;
    CGFloat verticalMargin = 5.0f;

    //calculate rect for given name string:
    CGFloat halfScreen = CGRectGetMidX(rect); //160points in portrait

    //_nameLabel:
    CGRect nameRect =
    [_name boundingRectWithSize:CGSizeMake(halfScreen, CGRectGetHeight(rect))
                        options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading)
                     attributes:@{NSFontAttributeName: [UIFont fontWithName:kHelveticaFont size:_nameLabel.font.pointSize]}
                        context:nil];

    //name rect = origin=(x=0, y=0) size=(width=147.53174, height=34.5) - WHY width = 147.53174 NOT halfScreen = 160points?

    [_nameLabel setFrame:CGRectMake(0.0f,
                                    verticalMargin,
                                    ceilf(CGRectGetWidth(nameRect)),
                                    CGRectGetHeight(rect) - verticalMargin * 2)];

    //calculate rect for given value string:
    //_valueLabel:
    CGFloat maxWidth = CGRectGetWidth(rect) - CGRectGetMaxX(_nameLabel.frame); //172points in portrait
    CGRect valueRect =
    [_value boundingRectWithSize:CGSizeMake(maxWidth, CGRectGetHeight(rect))
                         options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading)
                      attributes:@{NSFontAttributeName: [UIFont fontWithName:kHelveticaFont size: _valueLabel.font.pointSize]}
                         context:nil];
    //valueRect = origin=(x=0, y=0) size=(width=160.03418, height=34.5) - WHY width = 160.03418 NOT maxWidth = 172 points?

    [_valueLabel setFrame:CGRectMake(CGRectGetMaxX(_nameLabel.frame),
                                     verticalMargin,
                                     ceilf(CGRectGetWidth(valueRect)),
                                     CGRectGetHeight(rect) - verticalMargin * 2)];

}

And result:

enter image description here

Yellow background is half Width of Screen (160 points). Green background is another 160 points. I want to have _nameLabel with max size equal to half screen width. Anyone has ANY idea how to calculate it properly? Question are also commented in code. Thank you in advance for help.

NOTE: I'm developing an app only for iOS7.

EDIT: Note, that if _name will be much shorter, for example "key", i want to have _valueLabel next to _nameLabel like this (and it works as it should):

enter image description here

Was it helpful?

Solution

I solved my problem. Actually I dived deep in apple documentation and the problem was generated by this argument:

NSStringDrawingUsesLineFragmentOrigin // which means: The specified origin is the line fragment origin, not the base line origin

in line:

options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading)

Thats why method:

 (CGRect)boundingRectWithSize:options:attributes:context:

gave me wrong rect dimensions. Now everything works as should.

OTHER TIPS

_valueLabel.text = _value;
CGSize fitSize = [_valueLabel sizeThatFits:CGSizeMake(FLT_MAX, 18.0)];
CGRect newFrame = _valueLabel.frame;
newFrame.size.width = if fitSize.width > 160.0 ? 160.0 : fitSize.width
_valueLabel.frame = newFrame;

Obviously your code has a few more calculations to move the second UITextField over but hence use the same fitSize above to calculate the origin of the second UITextField plus your margin. Hope that helps get you your issue solved.

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