8/25.Lable文字适应,UITableVIew两种方法/UICollectionView

来源:互联网 发布:张无忌 知乎 编辑:程序博客网 时间:2024/06/05 07:29

frame适应文字(若只设置宽自适应,没有设置高自适应,字体有可能显示不全)
- 方法一:

 CGSize mySize=[_secondLabel.text sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:19],NSFontAttributeName, nil]];    _secondLabel.frame=CGRectMake(0, 0, mySize.width, mySize.height);

方法二:

 CGFloat secW=[second boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, 30) options:NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18]} context:nil].size.width;  CGFloat secH=[second boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, 30) options:NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18]} context:nil].size.height;   _secondLabel.frame=CGRectMake(0, 0, secW, secH);

文字适应frame

_secondLabel.adjustsFontSizeToFitWidth=YES;
  • UItableVIew
 - (void)createTableView{       _tbView=[[UITableView alloc] initWithFrame:CGRectMake(0, 0, WINSIZE1.size.width, WINSIZE1.size.height-64-49) style:UITableViewStylePlain];    _tbView.delegate=self;    _tbView.dataSource=self;    [_tbView registerNib:[UINib nibWithNibName:@"ForumCell" bundle:nil] forCellReuseIdentifier:@"ForumCellId"];  }-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    static NSString*cellId=@"ForumCellId";    ForumCell*cell=[tableView dequeueReusableCellWithIdentifier:cellId];     ForumModel*model=_dataArray[indexPath.row];      [cell config:model];     return cell;  }//方法2: - (void)createTableView{       _tbView=[[UITableView alloc] initWithFrame:CGRectMake(0, 0, WINSIZE1.size.width, WINSIZE1.size.height-64-49) style:UITableViewStylePlain];    _tbView.delegate=self;    _tbView.dataSource=self;} - (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    static NSString*cellId=@"cellId";    OneTableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:cellId];    if (cell==nil)    {        cell=[[[NSBundle mainBundle] loadNibNamed:@"OneTableViewCell" owner:nil options:nil] lastObject];    }    return cell;}
  • UICollectionView
- (void)createCollectionView{    //网格视图对象    //UICollectionView:UIScrollView    /*     第一个参数:位置     第二个参数:布局对象     */    CGRect frame = CGRectMake(0, 20, 375, 667-20);    //布局对象是一个UICollectionViewLayout类型的对象    //由于我们是规则的布局,可以直接使用UICollectionViewFlowLayout对象    //UICollectionViewFlowLayout继承于UICollectionViewLayout    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];    //1.上下左右的间距    /*     UIEdgeInsets:top(上面的间距)、left(左边的间距)、bottom(下面的间距)、right(右边的间距)     *///    layout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);//    //2.cell的大小//    layout.itemSize = CGSizeMake(180, 100);//    //3.横向间距//    layout.minimumInteritemSpacing = 5;//    //4.纵向间距//    layout.minimumLineSpacing = 10;    layout.sectionInset = UIEdgeInsetsMake(6, 5, 5, 5);    layout.itemSize = CGSizeMake(118, 257);    //3.横向间距    layout.minimumInteritemSpacing = 5;    //4.纵向间距    layout.minimumLineSpacing = 20;    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:frame collectionViewLayout:layout];    //设置代理    collectionView.delegate = self;    //设置数据源代理    collectionView.dataSource = self;    //设置白色背景    collectionView.backgroundColor = [UIColor whiteColor];    //注册cell的类型    //代码的方式注册cell的类型,表示创建cell的时候用这个类型来创建    /*     第一个参数:cell的类型     第二参数:重用标志     */    [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:kCellReuseId];     //注册cell(xib方式)    //第一个参数:xib的对象(UINib类型)    //第二个参数:重用标志  /*  UINib *nib = [UINib nibWithNibName:@"DataCell" bundle:nil];    [collectionView registerNib:nib forCellWithReuseIdentifier:kCellReuseId];    */    //添加到父视图    [self.view addSubview:collectionView];}- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{    //重用方式    //从重用队列里面获取    /*     第一个参数:重用id     第二个参数:cell的位置     */    //UITableView->NSIndexPath:section、row    //UICollectionView->NSIndexPath:section、item    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCellReuseId forIndexPath:indexPath];    //不需要再创建,从dequeueReusableCell方法里面移动能够获取到    //设置背景颜色    cell.backgroundColor = [UIColor grayColor];    //移除之前的子视图    for (UIView *sub in cell.contentView.subviews) {        [sub removeFromSuperview];    }    //显示文字    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 30, 180, 40)];    label.textAlignment = NSTextAlignmentCenter;    //获取文字    NSString *str = _dataArray[indexPath.item];    label.text = str;    //添加到父视图    [cell.contentView addSubview:label];    return cell;}(1)初始化UILabel *aLabel=[[UILabel alloc]initWithFrame:CGRectMake(50, 50, 100, 50)];(2)文字内容//位置默认是靠左的[aLabel setText:@"hello"];//设置字体颜色aLabel.textColor=[UIColor blueColor];aLabel.textColor=[UIColor redColor];//设置字体大小aLabel.font=[UIFont systemFontOfSize:12.4];//修改字体的字体和大小aLabel.font=[UIFont fontWithName:@"Arial Rounded MT Bold" size:36.0];//设置背景颜色aLabel.backgroundColor=[UIColor redColor];//清空背景颜色aLabel.backgroundColor=[UIColor clearColor];//设置对齐方式aLabel.textAlignment = UITextAlignmentLeft;//文字靠左aLabel.textAlignment = UITextAlignmentCenter;//文字居中aLabel.textAlignment = UITextAlignmentRight;//文字靠右//设置字体大小是否适应label宽度aLabel.adjustsFontSizeToFitWidth=YES;//是YES时,这个属性就来控制文本基线的行为在定义里面允许有以下格式显示:    typedef enum {          UIBaselineAdjustmentAlignBaselines,   //默认值文本最上端与label中间线对齐       UIBaselineAdjustmentAlignCenters,   //text中间与label中间线对齐     UIBaselineAdjustmentNone,    //text最低端与label中间线对齐 } UIBaselineAdjustment;    //设置是否是高亮aLabel.highlighted=YES;//高亮颜色aLabel.highlightedTextColor=[UIColor redColor];//设置阴影颜色aLabel.shadowColor=[UIColor blueColor];//阴影偏移量aLabel.shadowOffset=CGSizeMake(0.5, 0.5);//是否能和用户交互aLabel.userInteractionEnabled=YES;//文字是否可变,默认值是YESaLabel.enabled=YES;//设置文字过长时的显示格式aLabel.lineBreakMode = UILineBreakModeMiddleTruncation;//截去中间aLabel.lineBreakMode =UILineBreakModeTailTruncation,//截去尾部aLabel.lineBreakMode =UILineBreakModeHeadTruncation;//截去头部aLabel.lineBreakMode=UILineBreakModeCharacterWrap;//保留整个字符aLabel.lineBreakMode=UILineBreakModeClip;//截去多余部分在定义里面允许有以下格式显示: typedef enum { UILineBreakModeWordWrap = 0, // UILineBreakModeCharacterWrap,  UILineBreakModeClip,//截去多余部分  UILineBreakModeHeadTruncation,//截去头部  UILineBreakModeTailTruncation,//截去尾部  UILineBreakModeMiddleTruncation,//截去中间} UILineBreakMode;
0 0