2010-05-04 187 views

回答

7

試試這個:

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

    [ textView setFont: [ UIFont systemFontOfSize: newSize ]]; 
} 
11

與接受答案的問題是,你必須猜測人物的需要來填充字段中的數字(字符串的長度),並從字體的不同而不同字體。像這樣的東西,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 
+0

我非常喜歡這種方法。如果任何人有使用它的問題,這是我做的:'if [![mainContent sizeFontToFitMinSize:12.0 maxSize:20.0]){NSLog(@「content does not fit!」); }' – dchakarov 2012-03-16 13:16:15

0

Swift 4實現靈感來自@Jane Sales的回答。

計算可用寬度和高度時,我們還必須考慮可能的垂直和水平邊距(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) 
    } 

}