1

The code is below. What ends up happening is it's as if I used leftPadding instead of rightPadding and a string or long integer will run of the screen to the right. What I want is for the string is pad itself on the right and it's length extends to the left..

- (void)constructTimeStampLabel {
    CGFloat rightPadding = 250.f;
    CGFloat topPadding = -30.f;
    CGRect frame = CGRectMake(rightPadding,
                              topPadding,
                              floorf(CGRectGetWidth(__informationView.frame)),
                              CGRectGetHeight(__informationView.frame) - topPadding);
    _timeStampLabel = [[UILabel alloc] initWithFrame:frame];
    _timeStampLabel.text = [NSString stringWithFormat:@"Sent on %@", _feedItem.timeStamp];
    _timeStampLabel.textColor = [UIColor whiteColor];
    [_timeStampLabel setFont:[UIFont systemFontOfSize:12]];
    [__informationView addSubview:_timeStampLabel];
}
David Berry
  • 40,941
  • 12
  • 84
  • 95

1 Answers1

1

try setting the alignment to the right.

_timeStampLabel.textAlignment = NSTextAlignmentRight;
Rob
  • 1,025
  • 2
  • 10
  • 27
  • That works but now I'm getting this issue where the text sort of vertically centers itself instead of dropping down a line.. see links: http://imgur.com/wA1Kmzj,XtK6bUt http://imgur.com/XtK6bUt – user3661956 May 22 '14 at 18:09
  • that's a little more complex then. UILabels always vertically align to the middle, you can't set that to align to the top. What I do in that case is use NSString's boundingRectWithSize:options:attributes:context to get the size the text will take up. then use that size and your fixed origin, and that'll seem like it's top-aligned. See my next comment for code. – Rob May 22 '14 at 18:21
  • CGRect frame = [_timeStampLabel.text boundingRectWithSize:CGSizeMake(_timeStampLabel.frame.size.width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : _timeStampLabel.font} context:nil]; frame.origin = _timeStampLabel.frame.origin; _timeStampLabel.frame = frame; – Rob May 22 '14 at 18:23