1.计算文字长度

 

   NSString* str = @"你好";

 const float kFontSize = 18.f;

    NSStringDrawingOptions options =  NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading;

    CGRect rect = [str boundingRectWithSize:CGSizeMake(MAXFLOAT,MAXFLOAT) options:options attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:kFontSize]} context:nil];

    CGFloat realHeight = ceilf(rect.size.height);

2.如果限制文字的宽度或高度,则将CGSizeMake(MAXFLOAT,MAXFLOAT)中的相应参数修改成具体的值

相关文章:

  • 2022-12-23
  • 2021-04-22
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-06-25
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案