0

I am creating one application, where i want to set frame of custom UIView according to the text, then i am adding UILabel over the UIView. But sometimes i am not getting correct frame at run time using my below code :

strImageBottomText = @"The next-generation entertainment for first web view just bottom of slide show image.";

CGSize maximumSize = CGSizeMake(320, 9999);

CGFloat cellHeight = 0;

CGSize titleLabelHeightSize = CGSizeZero;


if ([strImageBottomText respondsToSelector: @selector(boundingRectWithSize:options:attributes:context:)] == YES) {
    titleLabelHeightSize = [strImageBottomText boundingRectWithSize: maximumSize options: NSStringDrawingUsesLineFragmentOrigin
                                                          attributes: @{ NSFontAttributeName: [UIFont fontWithName:@"Cabin-SemiBold" size:15.0f] } context: nil].size;
} else {
    titleLabelHeightSize = [strImageBottomText sizeWithFont: [UIFont fontWithName:@"Cabin-SemiBold" size:15.0f]
                                           constrainedToSize:maximumSize
                                               lineBreakMode:NSLineBreakByWordWrapping];
}

  cellHeight += titleLabelHeightSize.height > 24 ? titleLabelHeightSize.height : 24;

    CGRect frame = CGRectMake(0, slideshow.frame.size.height, 320, cellHeight + 30);
    UIView *customView = [[UIView alloc] initWithFrame:frame];
    customView.backgroundColor = [UIColor colorWithRed:0/255.0 green:151/255.0 blue:207/255.0 alpha:1.0];

    UILabel *sectionTitle = [[UILabel alloc] init];

    sectionTitle.frame = CGRectMake(0, 0, 280, cellHeight);
    sectionTitle.text = strImageBottomText;
    sectionTitle.lineBreakMode = NSLineBreakByWordWrapping;
    sectionTitle.textAlignment = NSTextAlignmentCenter;
    sectionTitle.numberOfLines = 0;
    [sectionTitle sizeToFit];

    sectionTitle.font = [UIFont fontWithName:@"Cabin-SemiBold" size:15.0f];
    sectionTitle.backgroundColor = [UIColor clearColor];
    sectionTitle.textColor = [UIColor whiteColor];

    [customView addSubview:sectionTitle];

    [self.view addSubview:customView]; 

I want my output as below screenshot :

enter image description here

Can you please help me that where i am doing wrong in my code? Thanks

rmaddy
  • 314,917
  • 42
  • 532
  • 579
user2786
  • 656
  • 4
  • 13
  • 35
  • Review this link : 'http://stackoverflow.com/questions/25093407/uibutton-image-position-depends-on-the-frame-of-titlelabel/25093922#25093922' It will help you. – VRAwesome Aug 14 '14 at 10:50
  • Why dont you use `UITextView` to display text. it would be easy to set its frame according to content – Mohit Aug 14 '14 at 10:50
  • Thanks @Mohit Popat It is possible with UITextView, but i want to know my mistake in my code, i have tried alot to set frame correctly. – user2786 Aug 14 '14 at 10:53
  • Bcos of left and right padding i am using UIView and UILabel. Can you pls help me to achieve this. Thanks – user2786 Aug 14 '14 at 10:56
  • 1
    @user2786 remove this line `[sectionTitle sizeToFit];` – Yogesh Suthar Aug 14 '14 at 10:56
  • Thanks @YogeshSuthar for your response.. please give me some time i am checking. – user2786 Aug 14 '14 at 11:06
  • It's working now after removing [sectionTitle sizeToFit]; line from my code. Thanks alot @YogeshSuthar for your help :) – user2786 Aug 14 '14 at 11:13
  • Probably it might be working fine by removing [sectionTitle sizeToFit]; line but I am finding small mistake in the above code.. maximumSize is defined with 320 px width but label width is 280px.. I feel both should be same so that boundingRectWithSize: method will return you exact height. This will hit you if you have long text to display. – Srivathsa Aug 14 '14 at 12:19

0 Answers0