TextView字体大小自动调整取决于设备屏幕

时间:2015-09-21 15:36:44

标签: ios swift

我根据文本视图大小限制字符数量。我只允许2行,宽度因设备而异。如何在不对其进行硬编码的情况下根据设备自动调整字体?

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
    }

上面的代码用于根据文本视图宽度限制字符数量。但是当在iphone 6上显示从4s写入的文本时,文本会被截断,因为iphone 4s上的宽度小于iphone 6

2 个答案:

答案 0 :(得分:0)

您可以使用以下内容:

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

获取设备屏幕尺寸

答案 1 :(得分:0)

试试这个:

- (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;
}

您可以在viewDidLoad和shouldChangeTextInRange中使用此功能,以便在文本更改时更新大小。 我转换为Swift但我没有测试它:

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
}