2010-05-04 51 views

Trả lời

7

Hãy thử điều này:

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

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

Vấn đề với câu trả lời được chấp nhận là bạn phải đoán số ký tự (chiều dài của string) cần thiết để điền vào lĩnh vực này, và khác với phông chữ phông chữ. Một cái gì đó như thế này, một thể loại trên UITextView, nên làm việc.

#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

Tôi thực sự thích cách tiếp cận này. Nếu bất cứ ai có một vấn đề bằng cách sử dụng nó, đây là những gì tôi làm: 'if (! [MainContent sizeFontToFitMinSize: 12.0 maxSize: 20.0]) {NSLog (@" nội dung không phù hợp! "); } ' – dchakarov

0

Thực hiện Swift 4 lấy cảm hứng từ câu trả lời của @Jane Sales.

Khi tính chiều rộng và chiều cao có sẵn, chúng tôi cũng phải tính đến các lề có thể theo chiều dọc và ngang có thể (textContainerInsettextContainer.lineFragmentPadding).

Dưới đây là một lời giải thích tốt hơn về cách lề làm việc trên UITextView: https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/TextUILayer/Tasks/SetTextMargins.html

Nếu độ xem văn bản có thể thay đổi kích thước, sau đó chúng ta cũng phải buộc một bố cục vì vậy chúng tôi có thể tính toán kích thước phông chữ dựa trên kích thước xem văn bản lớn nhất có thể. Trong trường hợp này chỉ có chiều cao được xem xét (bố trí chỉ khi chiều cao văn bản yêu cầu lớn hơn chiều cao ban đầu có sẵn).

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) 
    } 

} 
Các vấn đề liên quan