2010-01-10 14 views

Trả lời

2

Thuộc tính này chỉ khả dụng trên UITextFields. Để thực hiện việc này trong UITextView, bạn phải liên tục xem văn bản và điều chỉnh kích thước phông chữ theo cách thủ công khi thay đổi.

+0

Ah, có bất kỳ mã mẫu nào ở đó thực hiện việc này không? Tôi nhìn quanh và không thể tìm thấy gì cả. Cám ơn rất nhiều! –

+0

Tôi không biết bất kỳ mã mẫu nào; Tôi đã không bao giờ nghe nói về bất cứ ai cố gắng làm điều này trước đây. –

8

Điều này, trong danh mục trên UITextView, nên thực hiện thủ thuật. Đối với lý do tại sao bạn cần fudgefactor, xem this post

#define kMaxFieldHeight 9999 

-(BOOL)sizeFontToFit:(NSString*)aString minSize:(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 = [aString sizeWithFont:self.font constrainedToSize:tallerSize lineBreakMode:UILineBreakModeWordWrap]; 

while (stringSize.height >= self.frame.size.height) 
     { 
     if (fontSize <= aMinFontSize) // it just won't fit 
      return NO; 

     fontSize -= 1.0; 
     self.font = [self.font fontWithSize:fontSize]; 
     tallerSize = CGSizeMake(self.frame.size.width-fudgeFactor,kMaxFieldHeight); 
     stringSize = [aString sizeWithFont:self.font constrainedToSize:tallerSize lineBreakMode:UILineBreakModeWordWrap]; 
     } 

return YES; 
} 
+2

trong iOS> 7.0 bạn cần thay đổi UILineBreakModeWordWrap thành NSLineBreakByWordWrapping – ignotusverum

+0

chỉ tò mò, làm thế nào để bạn biết "fudgeFactor = 16.0"? Tôi tin rằng giá trị này phải nhỏ hơn khi kích thước văn bản nhỏ hơn – Jacky

5

Hãy thử điều này, nó đơn giản hơn rất nhiều:

while (self.contentSize.height > self.frame.size.height) 
{ 
    self.font = [self.font fontWithSize:self.font.pointSize -1]; 
} 
+0

+1, nó hoạt động tốt – NAZIK

+3

Không hoạt động trong iOS7. –

+2

@BradMoore, bạn cần thêm một dòng nữa: [textView layoutSubviews]; – Jacky

6

cách tiếp cận tương tự để trả lời Arie Litovsky nhưng mà không phụ classing (hoặc sử dụng một loại) và không sử dụng contentSize không trả lại chiều cao chính xác của văn bản được hiển thị cho tôi. Thử nghiệm trên iOS 7:

while (((CGSize) [_textView sizeThatFits:_textView.frame.size]).height > _textView.frame.size.height) { 
    _textView.font = [_textView.font fontWithSize:_textView.font.pointSize-1]; 
} 

Cách tiếp cận là tiếp tục giảm kích thước phông chữ cho đến khi văn bản vừa vặn trong khung của chế độ xem văn bản.

Nếu bạn đang đi để sử dụng trong sản xuất bạn vẫn sẽ cần phải:

  • Xử lý trường hợp ngay cả với cỡ chữ nhỏ nhất-thể văn bản vẫn sẽ không phù hợp.
  • Sử dụng cách tiếp cận tương tự để tăng kích thước phông chữ nếu bạn cũng muốn chia tỷ lệ văn bản lên để vừa với khung.
+0

Cách tiếp cận nhanh để xử lý trường hợp phông chữ "tăng": http://pastie.org/8975356 – Anil

2

Đây là mã mẫu của tôi. Về cơ bản, tôi kiểm tra kích thước mong đợi để biết kích thước phông chữ cần tăng hay giảm. Bạn cần phải thêm UITextViewDelegate đến lớp học của bạn để chắc chắn rằng nó hoạt động

- (void)updateTextFont:(UITextView *)textView 
{ 
    // Only run if has text, otherwise it will make infinity loop 
    if (textView.text.length == 0 || CGSizeEqualToSize(textView.bounds.size, CGSizeZero)) return; 

    /* 
    - Update textView font size 
    If expectHeight > textViewHeight => descrease font size n point until it reach textViewHeight 
    If expectHeight < textViewHeight => inscrease font size n point until it reach textViewHeight 
    */ 
    CGSize textViewSize = textView.frame.size; 
    CGFloat fixedWidth = textViewSize.width; 
    CGSize expectSize = [textView sizeThatFits:CGSizeMake(fixedWidth, MAXFLOAT)]; 

    UIFont *expectFont = textView.font; 
    if (expectSize.height > textViewSize.height) { 
     while ([textView sizeThatFits:CGSizeMake(fixedWidth, MAXFLOAT)].height > textViewSize.height) { 
      expectFont = [textView.font fontWithSize:(textView.font.pointSize-1)]; 
      textView.font = expectFont; 
     } 
    } else { 
     while ([textView sizeThatFits:CGSizeMake(fixedWidth, MAXFLOAT)].height < textViewSize.height) { 
      expectFont = textView.font; 
      textView.font = [textView.font fontWithSize:(textView.font.pointSize+1)]; 
     } 
     textView.font = expectFont; 
    } 
} 
+0

Tôi tò mò về điều khoản khác đang thực hiện ở đây. Bạn đặt 'expectFont' thành' textView'' 'font' trước khi sửa đổi nó ... sau đó bạn đặt nó trở về' font' chưa sửa đổi? Đó có phải là để ngăn chặn việc nhảy lên và xuống của kích thước văn bản không? –

+0

@StephenPaul trong mệnh đề else, nếu while không chạy, 'textView.font' không thay đổi (vì' expectFont' thực sự là 'textView.font' gốc). Nhưng nếu trong khi chạy ít nhất 1 lần, 'expectFont' được thay đổi và chúng ta cần cập nhật nó sau khi kết thúc vòng lặp. Tôi có thể làm một lá cờ để xác định trong khi chạy hay không, nhưng tôi đã không. Thay vào đó, tôi nhóm nó thành dòng cuối cùng 'textView.font = expectFont' – nahung89

+0

nếu vòng lặp while chạy ít nhất 1 lần, bạn lưu trữ phông chữ hiện tại trong một biến 'expectFont'. Sau đó, bạn tăng phông chữ của 'textView' lên một điểm, nhưng sau đó khi vòng lặp while đó thoát ra, bạn sẽ thấy phông chữ' textView' quay lại với nó là ... –

10

tôi đã chuyển đổi dementiazz's answer để Swift:

func updateTextFont() { 
    if (textView.text.isEmpty || CGSizeEqualToSize(textView.bounds.size, CGSizeZero)) { 
     return; 
    } 

    let textViewSize = textView.frame.size; 
    let fixedWidth = textViewSize.width; 
    let expectSize = textView.sizeThatFits(CGSizeMake(fixedWidth, CGFloat(MAXFLOAT))); 

    var expectFont = textView.font; 
    if (expectSize.height > textViewSize.height) { 
     while (textView.sizeThatFits(CGSizeMake(fixedWidth, CGFloat(MAXFLOAT))).height > textViewSize.height) { 
      expectFont = textView.font!.fontWithSize(textView.font!.pointSize - 1) 
      textView.font = expectFont 
     } 
    } 
    else { 
     while (textView.sizeThatFits(CGSizeMake(fixedWidth, CGFloat(MAXFLOAT))).height < textViewSize.height) { 
      expectFont = textView.font; 
      textView.font = textView.font!.fontWithSize(textView.font!.pointSize + 1) 
     } 
     textView.font = expectFont; 
    } 
} 
+0

Ngoài ra, bạn có thể thêm kích thước phông chữ tối thiểu và tối đa để tránh các kích thước không mong muốn. (quá lớn hoặc quá nhỏ) Tôi vừa thêm điều kiện "&& (textView.font! .pointSize> CGFloat (minSize))" vào trong khi – RubenVot

+0

@RubenVot, khi nào chúng ta nên sử dụng chức năng này? – Shyam

0

Một phiên bản cập nhật của mã @Arie Litovsky của. Điều này làm việc trong iOS7 và sau đó và kéo dài kích thước phông chữ cả lên và xuống để văn bản phù hợp.

- (void) stretchFontToFit:(UITextView*)textView 
{ 

    while(textView.contentSize.height < textView.frame.size.height) 
    { 
     textView.font = [textView.font fontWithSize:textView.font.pointSize +1]; 
     [textView layoutIfNeeded]; 
    } 

    while(textView.contentSize.height > textView.frame.size.height) 
    { 
     textView.font = [textView.font fontWithSize:textView.font.pointSize -1]; 
     [textView layoutIfNeeded]; 
    } 
} 
0

Trong viewDidLoad:

textView.textContainer.lineFragmentPadding = 0; 
textView.textContainerInset = UIEdgeInsetsMake(0, 0, 0, 0); 

Bạn cần phải thêm UITextViewDelegate:

- (void)updateTextFont:(UITextView *)textView { 

    CGSize textViewSize = textView.frame.size; 
    CGSize sizeOfText = [textView.text sizeWithAttributes:@{NSFontAttributeName: textView.font}]; 

    CGFloat fontOfWidth = floorf(textView.font.pointSize/sizeOfText.height * textViewSize.height); 
    CGFloat fontOfHeight = floorf(textView.font.pointSize/sizeOfText.width * textViewSize.width); 

    textView.font = [textView.font fontWithSize:MIN(fontOfHeight, fontOfWidth)]; 
} 
1

tôi đã chuyển đổi câu trả lời dementiazz của & Matt Frear để Swift 3:

func updateTextFont() { 
     if (textView.text.isEmpty || textView.bounds.size.equalTo(CGSize.zero)) { 
      return 
     } 

    let textViewSize = textView.frame.size 
    let fixedWidth = textViewSize.width 
    let expectSize = textView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat(MAXFLOAT))) 

    var expectFont = textView.font 
    if (expectSize.height > textViewSize.height) { 
     while (textView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat(MAXFLOAT))).height > textViewSize.height) { 
      expectFont = textView.font!.withSize(textView.font!.pointSize - 1) 
      textView.font = expectFont 
     } 
    } 
    else { 
     while (textView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat(MAXFLOAT))).height < textViewSize.height) { 
      expectFont = textView.font 
      textView.font = textView.font!.withSize(textView.font!.pointSize + 1) 
     } 
     textView.font = expectFont 
    } 
} 
+0

Làm việc cho tôi. Cảm ơn! –

-1

U có thể dễ dàng làm được điều này bằng cách sử dụng

  • self.textview.font =

self.textview.font = UIFont (tên: self.textview.font .fontName,
kích thước: self.textview.frame. size.height/10)!

chia textview.frame.size.height khi một số liên tục dựa trên yêu cầu của bạn ..

1

cho nhanh 3

func updateTextFont(textView : UITextView) { 


    if (textView.text.isEmpty || textView.bounds.size.equalTo(CGSize.zero)) { 
     return; 
    } 

    let textViewSize = textView.frame.size; 
    let fixedWidth = textViewSize.width; 

    let expectSize = textView.sizeThatFits(CGSize(width : fixedWidth, height : CGFloat(MAXFLOAT))); 

    var expectFont = textView.font; 
    if (expectSize.height > textViewSize.height) { 
     while (textView.sizeThatFits(CGSize(width : fixedWidth, height : CGFloat(MAXFLOAT))).height > textViewSize.height) { 
      expectFont = textView.font!.withSize(textView.font!.pointSize - 1) 
      textView.font = expectFont 
     } 
    } 
    else { 
     while (textView.sizeThatFits(CGSize(width : fixedWidth,height : CGFloat(MAXFLOAT))).height < textViewSize.height) { 
      expectFont = textView.font; 
      textView.font = textView.font!.withSize(textView.font!.pointSize + 1) 
     } 
     textView.font = expectFont; 
    } 
} 
-2

Đây là một giải pháp cho Swift 4 (dựa trên Arie Litovsky câu trả lời), bạn có thể đặt mã này vào textViewDidChange(_ textView: UITextView) phương thức ủy quyền:

Chia tỷ lệ phông chữ xuống:

while (textView.contentSize.height > textView.frame.size.height) { 
     guard let fontName = textView.font?.fontName, let fontSize = textView.font?.pointSize else {return} 
     if fontSize < 12 { 
      return 
     } 
     textView.font = UIFont(name: fontName, size: fontSize - 1) 
     textView.layoutIfNeeded() 
    } 

Và nhân rộng phông chữ:

 while (textView.contentSize.height < textView.frame.size.height) { 
     guard let fontName = textView.font?.fontName, let fontSize = textView.font?.pointSize else {return} 
     if fontSize > 15 { 
      return 
     } 
     textView.font = UIFont(name: fontName, size: fontSize + 1) 
     textView.layoutIfNeeded() 
    } 

Bạn có thể thay đổi fontSize < 12fontSize > 15 để phù hợp với nhu cầu của bạn trong tối thiểu và kích thước phông chữ tối đa.