简体   繁体   中英

TextView Font Size Auto Adjust depending on Device Screen

I'm limiting the amount of characters depending on the textview size. I'm allowing only allowing 2 lines and the width varies depending on the device. How can I have my fonts auto adjust depending on the device without hardcoding it?

func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {

    // Combine the new text with the old
    let combinedText = (textView.text as NSString).stringByReplacingCharactersInRange(range, withString: text)

    // Create attributed version of the text
    let attributedText = NSMutableAttributedString(string: combinedText)
    attributedText.addAttribute(NSFontAttributeName, value: textView.font!, range: NSMakeRange(0, attributedText.length))

    // Get the padding of the text container
    let padding = textView.textContainer.lineFragmentPadding

    // Create a bounding rect size by subtracting the padding
    // from both sides and allowing for unlimited length
    let boundingSize = CGSizeMake(textView.frame.size.width - padding * 2, CGFloat.max)

    // Get the bounding rect of the attributed text in the
    // given frame
    let boundingRect = attributedText.boundingRectWithSize(boundingSize, options: NSStringDrawingOptions.UsesLineFragmentOrigin, context: nil)

    // Compare the boundingRect plus the top and bottom padding
    // to the text view height; if the new bounding height would be
    // less than or equal to the text view height, append the text
    if (boundingRect.size.height + padding * 1 <= textView.frame.size.height){
        return true
    }
    else {
        return false
    }

My Code above works on limiting the amount of characters depending on the textview width, However when displaying text written from a 4s on an iphone 6 the text gets truncated because the width on an iphone 4s is smaller than an iphone 6

You could use something like:

let screenSize: CGRect = UIScreen.mainScreen().bounds
let screenWidth = screenSize.width
let screenHeight = screenSize.height

To get the device screen size

Try this:

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

    float fontSizeMin = 1;
    float fontSize = textView.font.pointSize;
    UIFont *font = self.textView.font;

    CGSize tallerSize = CGSizeMake(textView.contentSize.width, self.view.frame.size.height);
    CGSize stringSize = [textView.text boundingRectWithSize:tallerSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : font} context:nil].size;

    while (stringSize.height >= textView.frame.size.height) {

        if (fontSize <= fontSizeMin) {
            textView.font = font;
            return;
        }

        fontSize -= 1.0;
        font = [font fontWithSize:fontSize];
        stringSize = [textView.text boundingRectWithSize:tallerSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : font} context:nil].size;

    }

    textView.font = font;
}

You can use this in viewDidLoad and shouldChangeTextInRange for update the size when text change. I convert to Swift but I not test it:

func resizeFontTextView(textView: UITextView) {
    var fontSizeMin: Float = 1
    var fontSize: Float = textView.font.pointSize
    var font: UIFont = self.textView.font
    var tallerSize: CGSize = CGSizeMake(textView.contentSize.width, self.view.frame.size.height)
    var stringSize: CGSize = textView.text.boundingRectWithSize(tallerSize, options: NSStringDrawingUsesLineFragmentOrigin, attributes: [NSFontAttributeName: font], context: nil).size
    while stringSize.height >= textView.frame.size.height {
        if fontSize <= fontSizeMin {
            textView.font = font
            return
        }
        fontSize -= 1.0
        font = font.fontWithSize(fontSize)
        stringSize = textView.text.boundingRectWithSize(tallerSize, options: NSStringDrawingUsesLineFragmentOrigin, attributes: [NSFontAttributeName: font], context: nil).size
    }
    textView.font = font
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM