5 Apr 2017

Perfect Working Code with layoutAttributesForElementsInRect: For Printing text in Collection View cell & Cells Size as Per Given String Text ...



Perfect Working Code with layoutAttributesForElementsInRect: For Printing text in Collection View cell & Cells Size as Per Given String Text ...

Storyboard:-

  


#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UICollectionViewDataSource, UICollectionViewDelegate>

@property (strong, nonatomic) IBOutlet UICollectionView *collectionView;
@property (strong, nonatomic) NSArray *languagesArray;

@end


#import "ViewController.h"
#import "CollectionViewCellCustom.h"
#import "NDCollectionViewFlowLayout.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    _languagesArray = [[NSArray alloc]initWithObjects:@"C",@"Casdasd++",@"Java",@"Oasdasdracle",@".Net sdfgfdg",@"hadoopasdasdasd",@"Objective-C",@"Csadadadadsasdad",@"C++",@"Java",@"Oracle",@".Net sdfgfdg",@"hadoop",@"Objective-C",@"MS Office hdfggg",@"Software Developer",@"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nec lapathi suavitatem acupenseri Galloni Laelius anteponebat, sed suavitatem ipsam neglegebat; Summus dolor plures dies manere non potest? Tu vero, inquam, ducas licet, si sequetur; Illa videamus, quae a te de amicitia dicta sunt. Piso, familiaris noster, et alia multa et hoc loco Stoicos irridebat: Quid enim? At iam decimum annum in iacet.",@"hjeklasdaasssDDD",@"SHFJKSDF eghrtthyther",@"FGDSGSGHSGDSTERT",@"FGHDFHFHG ryhtu ",@"FGHDFHFHG1",@"DGRTHRTHRTJTYJTJT", @"FGHDFHFHG2",@"C",@"Casdasd++",@"Java",@"Oasdasdracle etjtyj",@".Net sdfgfdg",@"hadoopasdasdasd",@"Objective-C",@"Csadadadadsasdad",@"C++",@"Java",@"Oracle",@".Net sdfgfdg",@"hadoop",@"Objective-C",@"MS Office hdfggg",@"Software Developer",@"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nec lapathi suavitatem acupenseri Galloni Laelius anteponebat, sed suavitatem ipsam neglegebat; Summus dolor plures dies manere non potest? Tu vero, inquam, ducas licet, si sequetur; Illa videamus, quae a te de amicitia dicta sunt. Piso, familiaris noster, et alia multa et hoc loco Stoicos irridebat: Quid enim? At iam decimum annum in iacet.",@"hadoopasdasdasd",@"Objective-C",@"Csadadadadsasdad",@"C++",@"Java",@"Oracle",@".Net sdfgfdg",@"hadoop",@"Objective-C",@"MS Office hdfggg",@"Software Developer", nil];
    
    [_collectionView setDataSource:self];
    [_collectionView setDelegate:self];
    
    NDCollectionViewFlowLayout *objNDCollectionViewFlowLayout = [[NDCollectionViewFlowLayout alloc] init];
    [objNDCollectionViewFlowLayout layoutAttributesForElementsInRect:_collectionView.frame];
    
    [_collectionView setCollectionViewLayout:objNDCollectionViewFlowLayout];
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return _languagesArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    CollectionViewCellCustom *cell = [_collectionView dequeueReusableCellWithReuseIdentifier:@"CollectionViewCellCustom" forIndexPath:indexPath];
    
    cell.lblData.text = [_languagesArray objectAtIndex:indexPath.row];
    cell.lblData.textAlignment = 1;
  
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
   NSString *text = [_languagesArray objectAtIndex:indexPath.row];

   CGRect r = [text boundingRectWithSize:CGSizeMake(collectionView.frame.size.width-55, 0)
                                 options:NSStringDrawingUsesLineFragmentOrigin
                              attributes:@{NSFontAttributeName:[UIFont fontWithName:@"Helvetica-Bold" size:16.0]}
                                 context:nil];
   NSLog(@"%f     %f",r.size.width, r.size.height);
   
   r.size.width = r.size.width+35;
   r.size.height = r.size.height+20;
    
   return r.size;

}

@end



#import <UIKit/UIKit.h>

@interface NDCollectionViewFlowLayout : UICollectionViewFlowLayout
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect;
@end



#import "NDCollectionViewFlowLayout.h"

@implementation NDCollectionViewFlowLayout
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
    NSArray *attributes = [super  layoutAttributesForElementsInRect:rect];
    NSArray * answer = [[NSArray alloc] initWithArray:attributes copyItems:YES];
    
    if ([attributes count])
    {
    
    if ([attributes objectAtIndex:0]) {
        
        UICollectionViewLayoutAttributes *startingLayoutAttributes = answer[0];
        CGRect frame = startingLayoutAttributes.frame;
        frame.origin.x = 5;
        if (frame.origin.y == 0) {
            frame.origin.y = 5;
        }
        startingLayoutAttributes.frame = frame;
        
    }
 }
    
    
    
    for(int i = 1; i < [answer count]; ++i)
    {
        UICollectionViewLayoutAttributes *currentLayoutAttributes = answer[i];
        UICollectionViewLayoutAttributes *prevLayoutAttributes = answer[i - 1];
        NSInteger maximumSpacing = 5;
        
        NSInteger origin = CGRectGetMaxX(prevLayoutAttributes.frame);
        
        if(origin + maximumSpacing + currentLayoutAttributes.frame.size.width < self.collectionViewContentSize.width)
        {
            CGRect frame = currentLayoutAttributes.frame;
            frame.origin.x = origin + maximumSpacing;
            if (frame.origin.y == 0) {
                frame.origin.y = 5;
            }
            currentLayoutAttributes.frame = frame;
        }
        else
        {
            CGRect frame = currentLayoutAttributes.frame;
            frame.origin.x = 5;
            if (frame.origin.y == 0) {
                frame.origin.y = 5;
            }
            currentLayoutAttributes.frame = frame;
        }
    }

    return answer;
}
@end




#import <UIKit/UIKit.h>

@interface CollectionViewCellCustom : UICollectionViewCell
@property (weak, nonatomic) IBOutlet UILabel *lblData;

@end


#import "CollectionViewCellCustom.h"

@implementation CollectionViewCellCustom

@end


Result:-

 




No comments:

Post a Comment

Recent Posts

Codable demo

Link: https://www.dropbox.com/s/kw7c1kgv1628bh7/codableDemo.zip?dl=0