【问题标题】:Resize font size in UITextView在 UITextView 中调整字体大小
【发布时间】:2010-05-04 01:13:34
【问题描述】:

如果文本过多,有没有办法缩小 UITextView 中的字体大小?类似于 UILabel?

【问题讨论】:

    标签: iphone objective-c cocoa-touch ipad uitextview


    【解决方案1】:

    接受答案的问题在于,您必须猜测填充字段所需的字符数(字符串的长度),并且因字体而异。这样的东西,UITextView 上的一个类别,应该可以工作。

    #import "UITextView+Size.h"
    
    #define kMaxFieldHeight 1000
    
    @implementation UITextView (Size)
    
    -(BOOL)sizeFontToFitMinSize:(float)aMinFontSize maxSize:(float)aMaxFontSize {
    
    float fudgeFactor = 16.0;
    float fontSize = aMaxFontSize;
    
    self.font = [self.font fontWithSize:fontSize];
    
    CGSize tallerSize = CGSizeMake(self.frame.size.width-fudgeFactor,kMaxFieldHeight);
    CGSize stringSize = [self.text sizeWithFont:self.font constrainedToSize:tallerSize lineBreakMode:UILineBreakModeWordWrap];
    
    while (stringSize.height >= self.frame.size.height) {
    
        if (fontSize <= aMinFontSize) // it just won't fit, ever
            return NO;
    
        fontSize -= 1.0;
        self.font = [self.font fontWithSize:fontSize];
        tallerSize = CGSizeMake(self.frame.size.width-fudgeFactor,kMaxFieldHeight);
        stringSize = [self.text sizeWithFont:self.font constrainedToSize:tallerSize lineBreakMode:UILineBreakModeWordWrap];
    }
    
    return YES; 
    }
    
    @end
    

    【讨论】:

    • 我真的很喜欢这种方法。如果有人在使用它时遇到问题,我会这样做:if (![mainContent sizeFontToFitMinSize:12.0 maxSize:20.0]) { NSLog(@"content doesn't fit!"); }
    【解决方案2】:

    试试这个:

    NSInteger lengthThreshold = 200;
    if( [ textView.text length ] > lengthThreshold ) {
        NSInteger newSize = ... //calculate new size based on length
    
        [ textView setFont: [ UIFont systemFontOfSize: newSize ]];
    }
    

    【讨论】:

      【解决方案3】:

      受@Jane Sales 的回答启发的 Swift 4 实现。

      在计算可用宽度和高度时,我们还必须考虑可能的垂直和水平边距(textContainerInsettextContainer.lineFragmentPadding)。

      下面是对UITextView 上边距如何工作的更好解释:https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/TextUILayer/Tasks/SetTextMargins.html

      如果文本视图可以调整大小,那么我们还必须强制布局,以便我们可以根据最大可能的文本视图大小来计算字体大小。在这种情况下,仅考虑高度(仅当所需文本高度大于原始可用高度时才进行布局)。

      import UIKit
      
      extension UITextView {
      
          func adjustFontToFitText(minimumScale: CGFloat) {
              guard let font = font else {
                  return
              }
      
              let scale = max(0.0, min(1.0, minimumScale))
              let minimumFontSize = font.pointSize * scale
              adjustFontToFitText(minimumFontSize: minimumFontSize)
          }
      
          func adjustFontToFitText(minimumFontSize: CGFloat) {
              guard let font = font, minimumFontSize > 0.0 else {
                  return
              }
      
              let minimumSize = floor(minimumFontSize)
              var fontSize = font.pointSize
      
              let availableWidth = bounds.width - (textContainerInset.left + textContainerInset.right) - (2 * textContainer.lineFragmentPadding)
              var availableHeight = bounds.height - (textContainerInset.top + textContainerInset.bottom)
      
              let boundingSize = CGSize(width: availableWidth, height: CGFloat.greatestFiniteMagnitude)
              var height = text.boundingRect(with: boundingSize, options: .usesLineFragmentOrigin, attributes: [.font: font], context: nil).height
      
              if height > availableHeight {
                  // If text view can vertically resize than we want to get the maximum possible height
                  sizeToFit()
                  layoutIfNeeded()
                  availableHeight = bounds.height - (textContainerInset.top + textContainerInset.bottom)
              }
      
              while height >= availableHeight {
                  guard fontSize > minimumSize else {
                      break
                  }
      
                  fontSize -= 1.0
                  let newFont = font.withSize(fontSize)
                  height = text.boundingRect(with: boundingSize, options: .usesLineFragmentOrigin, attributes: [.font: newFont], context: nil).height
              }
      
              self.font = font.withSize(fontSize)
          }
      
      }
      

      【讨论】:

        猜你喜欢
        • 2011-01-03
        • 1970-01-01
        • 2011-04-09
        • 1970-01-01
        • 1970-01-01
        • 2010-10-18
        • 1970-01-01
        • 2019-01-17
        • 1970-01-01
        相关资源
        最近更新 更多