¿Hay alguna forma de reducir el tamaño de fuente en una UITextView si hay demasiado texto? Similar al UILabel?Redimensionar tamaño de fuente en UITextView
Respuesta
Prueba esto:
NSInteger lengthThreshold = 200;
if([ textView.text length ] > lengthThreshold) {
NSInteger newSize = ... //calculate new size based on length
[ textView setFont: [ UIFont systemFontOfSize: newSize ]];
}
El problema con la respuesta aceptada es que usted tiene que adivinar el número de caracteres (longitud de la cadena) que se necesita para llenar el campo, y que difiere de fuente a fuente. Algo así, una categoría en UITextView, debería funcionar.
#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
Swift 4 implementación inspirada en la respuesta de @Jane Sales.
Al calcular el ancho y la altura disponibles, también debemos tener en cuenta los posibles márgenes verticales y horizontales (textContainerInset
y textContainer.lineFragmentPadding
).
Aquí hay una mejor explicación de cómo los márgenes de trabajo sobre UITextView
: https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/TextUILayer/Tasks/SetTextMargins.html
Si la vista de texto puede cambiar el tamaño, entonces también debemos forzar una disposición para que podamos calcular el tamaño de la fuente basado en el mayor tamaño posible ver texto. En este caso, solo se considera la altura (diseños solamente si la altura del texto requerido es mayor que la altura disponible original).
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)
}
}
- 1. Cómo cambiar el tamaño de fuente de uitextview
- 2. ¿Cómo puedo establecer el tamaño de fuente de UITextView?
- 3. Cambiar el tamaño de la fuente para llenar UITextView?
- 4. Redimensionar UIImage en UIImageView
- 5. Redimensionar VideoView
- 6. ¿Cómo obtener UILabel (UITextView) tamaño de letra ajustado automáticamente?
- 7. Redimensionar/recortar/rellenar una imagen en un tamaño fijo
- 8. Redimensionar el tamaño de un UIViewController según su contenedor
- 9. jQuery - Retirar Redimensionar Funcionalidad
- 10. redimensionar imágenes base64
- 11. ImageMagick PNG Redimensionar aumenta el tamaño del archivo
- 12. Redimensionar imagen PNG
- 13. Cómo desencadenar jquery Redimensionar el tamaño mediante programación?
- 14. Cómo redimensionar automáticamente el tamaño de la fuente para que se ajuste al alto/ancho del cuadro de texto?
- 15. Aumente el tamaño de letra de los caracteres emoji en una UITextView en iOS 5.x
- 16. Redimensionar ventanas de Cacao proporcionalmente
- 17. Tinymce redimensionar solo
- 18. Redimensionar TextBox En Runtime en WPF
- 19. WinForms DataGridView tamaño de fuente
- 20. WPF tamaño de fuente global
- 21. iOS: ¿se está animando el cambio de tamaño de texto en UILabel o UITextView?
- 22. UITextField mínimo tamaño de fuente
- 23. CSS, tamaño de fuente relativo
- 24. TinyMCE tamaño de fuente predeterminado
- 25. Tamaño de fuente medio en css
- 26. ¿Cómo cambiar tamaño de fuente en direct.label?
- 27. establecer el tamaño de fuente en jquery
- 28. Tamaño de fuente demasiado grande en Opera
- 29. CLI ImageMagick redimensionar (solo downscale)
- 30. Redimensionar la imagen con Recortar
Me gusta mucho este enfoque. Si alguien tiene un problema al usarlo, esto es lo que hago: 'if (! [MainContent sizeFontToFitMinSize: 12.0 maxSize: 20.0]) {NSLog (@" el contenido no encaja! "); } ' – dchakarov