【问题标题】:UILabel does not autoshrink text after assigning NSAttributedString分配 NSAttributedString 后,UILabel 不会自动收缩文本
【发布时间】:2014-02-11 09:13:48
【问题描述】:

我有一个宽度有限的标签,我需要它来自动调整字体大小以适应文本。 因为我需要给文本加下划线,所以我给这个标签分配了一个属性字符串:

[_commentsLabel setAttributedText:[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%d comments", [comments count]] attributes:@{NSUnderlineStyleAttributeName : @(NSUnderlineStyleSingle)}]];

如您所见,cmets 的数量将定义文本的长度。但是由于某种原因,文本并没有缩小。最小字体比例设置为 0.1 并选中 Tighten Letter Spacing。

我认为它可能与我正在使用的自定义字体有关,但即使使用系统默认字体,文本也会被剪裁。

【问题讨论】:

    标签: ios uilabel nsattributedstring shrink


    【解决方案1】:

    我用循环来做到这一点:

    while TheWidthOfSuperview < MyLabel.intrinsicContentSize.width {
    
        MyLabel.font = MyLabel.font.withSize(MyLabel.font.pointSize - 1)
    
    }
    

    【讨论】:

      【解决方案2】:

      AttributeStrings 有自己的字体大小。手动执行此操作。

      构成属性字符串的所有属性字符串必须有一个 NSFontAttributeName。

      func updateLabelSizeIfNeeded() {
          let maxScale: CGFloat = 0.65
          let bounding = self.label.attributedText!.boundingRectWithSize(CGSizeMake(CGFloat.infinity, CGFloat.infinity), options: [], context: nil)
          if bounding.size.width > self.bounds.size.width*maxScale {
              let scaleFactor = (self.bounds.size.width * maxScale) / bounding.size.width
      
              label.transform = CGAffineTransformMakeScale(scaleFactor, scaleFactor)
          } else {
              label.transform = CGAffineTransformIdentity
          }
      }
      

      【讨论】:

      • 这应该是公认的答案。非常感谢!
      【解决方案3】:

      我会尝试设置标签属性

      @property(nonatomic) BOOL adjustsFontSizeToFitWidth
      

      选择“是”,看看是否能解决问题。如果没有,请告诉我。我在不同的情况下遇到了问题,但我最终使用了一些代码来手动更改大小。

      这是我用来手动更改字体大小的代码。我不确定您的问题是什么,但这最终解决了我的问题。只需在设置标签文本时调用此方法,然后自己设置字体大小即可。

          - (CGFloat)requiredFontSizeForLabel:(UILabel *)label
      {
          if (!label) {
              return kFontSize;
          }
          CGFloat originalFontSize = kFontSize;
      
          UIFont* font = label.font;
          CGFloat fontSize = originalFontSize;
      
          BOOL found = NO;
          do
          {
              if( font.pointSize != fontSize )
              {
                  font = [font fontWithSize: fontSize];
              }
              if([self wouldThisFont:font workForThisLabel:label])
              {
                  found = YES;
                  break;
              }
      
              fontSize -= 0.5;
              if( fontSize < (label.minimumScaleFactor * label.font.pointSize))
              {
                  break;
              }
      
          } while( TRUE );
      
          return( fontSize );
      }
      
          - (BOOL) wouldThisFont:(UIFont *)testFont workForThisLabel:(UILabel *)testLabel {
          NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:testFont, NSFontAttributeName, nil];
          NSAttributedString *as = [[NSAttributedString alloc] initWithString:testLabel.text attributes:attributes];
          CGRect bounds = [as boundingRectWithSize:CGSizeMake(CGRectGetWidth(testLabel.frame), CGFLOAT_MAX) options:(NSStringDrawingUsesLineFragmentOrigin) context:nil];
          BOOL itWorks = [self doesThisSize:bounds.size fitInThisSize:testLabel.bounds.size];
          return itWorks;
      }
      
              - (BOOL)doesThisSize:(CGSize)aa fitInThisSize:(CGSize)bb 
          {
              if ( aa.width > bb.width ) return NO;
              if ( aa.height > bb.height ) return NO;
              return YES;
          }
      

      Source for code found here

      【讨论】:

      • 我试过了,没有成功。似乎是 NSAttributedStrings 的问题。它适用于常规 NSStrings。
      • @Guilherme 这是我在缩放不适合我时使用的代码。也许它可以帮助你。
      • 我没有对此进行测试,但它似乎确实可以完成这项工作。我结束了根据 cmets 的数量手动设置字体,它并不太复杂。如果没有人对属性字符串不收缩的原因提出更好的答案/解释,我将标记为正确。
      猜你喜欢
      • 2014-10-01
      • 2013-01-25
      • 1970-01-01
      • 1970-01-01
      • 2016-12-28
      • 2012-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多