31 Mar 2017

Printing text in Collection View cell & Cells Size as Per Given String Text...


Printing text in Collection View cell & Cells Size as Per Given String Text...

Storyboard:-

    


Result:-



#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"

@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",@"C++",@"Java",@"Oracle",@".Net",@"hadoop",@"Objective-C",@"MS Office",@"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 spelunca iacet.", nil];
    
    [_collectionView setDataSource:self];
    [_collectionView setDelegate:self];

}

- (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
{
    
//   CGSize calCulateSizze =[(NSString*)[_languagesArray objectAtIndex:indexPath.row] sizeWithAttributes:NULL];
   
   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);
   
//   calCulateSizze.width = calCulateSizze.width+30;
//   calCulateSizze.height = calCulateSizze.height + 20;
   
   
   r.size.width = r.size.width+35;
   r.size.height = r.size.height+20;
   return r.size;
}


@end




#import <UIKit/UIKit.h>

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

@end



#import "CollectionViewCellCustom.h"

@implementation CollectionViewCellCustom

@end



No comments:

Post a Comment

Recent Posts

Codable demo

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