iOS UIlabel高度自适应总结。UITextView根据内容自动改变frame。

图片描述

图片描述

-(void)initUserInterface

{
    UILabel *label = [[UILabel alloc]init];
    label.numberOfLines = 0; // 需要把显示行数设置成无限制
    label.font = [UIFont systemFontOfSize:15];
    label.textAlignment = NSTextAlignmentCenter;
    label.text = @"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
    CGSize size =  [self sizeWithString:label.text font:label.font];
    label.bounds = CGRectMake(0, 0, size.width, size.height);
    label.center = self.view.center;
    [self.view addSubview:label];
     
}

// 定义成方法方便多个label调用 增加代码的复用性

-(CGSize)sizeWithString:(NSString *)string font:(UIFont *)font

{
    CGRect rect = [string boundingRectWithSize:CGSizeMake(320, 8000)//限制最大的宽度和高度
                                       options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesFontLeading  |NSStringDrawingUsesLineFragmentOrigin//采用换行模式
                                    attributes:@{NSFontAttributeName: font}//传人的字体字典
                                       context:nil];
     
    return rect.size;
}

方法二:

 CGRect contentRect = [NSString heightForString:badgeValue Size:contentSize Font:kValueFont];


//获取某固定文本的显示高度
+(CGRect)heightForString:(NSString*)str Size:(CGSize)size Font:(UIFont*)font
{
    return [NSString heightForString:str Size:size Font:font Lines:0];
}

+(CGRect)heightForString:(NSString*)str Size:(CGSize)size Font:(UIFont*)font Lines:(NSInteger)lines
{
    if (StringIsNullOrEmpty(str)) {
        return CGRectMake(0, 0, 0, 0);
    }
    static UILabel *lbtext;
    if (lbtext==nil) {
        lbtext    = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height)];
    }else{
        lbtext.frame=CGRectMake(0, 0, size.width, size.height);
    }
    lbtext.font=font;
    lbtext.text=str;
    lbtext.numberOfLines=lines;
    CGRect rect= [lbtext textRectForBounds:lbtext.frame limitedToNumberOfLines:lines];
    if(rect.size.height<0)
        rect.size.height=0;
    if (rect.size.width<0) {
        rect.size.width=0;
    }
    return rect;
}

/**

  • @return 根据字符串的的长度来计算UITextView的高度
    */

    +(float)heightForString:(NSString *)value fontSize:(float)fontSize andWidth:(float)width
    {

       float height = [[NSString stringWithFormat:@"%@\n",value] boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:fontSize],NSFontAttributeName, nil] context:nil].size.height;
       
       return height;

    }

/**

  • UITextView根据内容自动改变frame
    */

    • (void)textViewDidChange:(UITextView *)textView
      {

      [textView flashScrollIndicators];   // 闪动滚动条
      static CGFloat maxHeight = 130.0f;
      CGRect frame = textView.frame;
      CGSize constraintSize = CGSizeMake(frame.size.width, MAXFLOAT);
      CGSize size = [textView sizeThatFits:constraintSize];
      if (size.height >= maxHeight)
      {
          size.height = maxHeight;
          textView.scrollEnabled = YES;   // 允许滚动
      }
      else
      {
          textView.scrollEnabled = NO;    // 不允许滚动,当textview的大小足以容纳它的text的时候,需要设置scrollEnabed为NO,否则会出现光标乱滚动的情况
      }
      textView.frame = CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, size.height);

      }

你可能感兴趣的:(ios,uilabel,高度自适应)