开发者

UITextView autoSize

I would think this would be an easy thing since people would want to do it often, but I've searched around and tried different approaches, but nothing seems to work.

All I want to do is create a UITextView that has two lines of Text.

If the text is too long, and goes into 3 lines, I want to autosize the font until it 开发者_如何学运维fits into 2 lines.

Conceptually, I was planning on doing a recursive function that keeps shrinking the text until it fits on two lines (based on the height of the text field), but I couldn't pull it off.

Any suggestions are greatly appreciated.


Use a UILabel and set the following properties:

adjustsFontSizeToFitWidth = YES;
minimumFontSize = [UIFont systemFontofSize: 10]; // or whatever suits your app
numberOfLines = 2; 


In case anyone else comes across this issue, I found the answer for this here:

http://www.11pixel.com/blog/28/resize-multi-line-text-to-fit-uilabel-on-iphone/

//Create a string with the text we want to display.
self.ourText = @"This is your variable-length string. Assign it any way you want!";

/* This is where we define the ideal font that the Label wants to use.
   Use the font you want to use and the largest font size you want to use. */
UIFont *font = [UIFont fontWithName:@"Marker Felt" size:28];

int i;
/* Time to calculate the needed font size.
   This for loop starts at the largest font size, and decreases by two point sizes (i=i-2)
   Until it either hits a size that will fit or hits the minimum size we want to allow (i > 10) */
for(i = 28; i > 10; i=i-2)
{
    // Set the new font size.
    font = [font fontWithSize:i];
    // You can log the size you're trying: NSLog(@"Trying size: %u", i);

    /* This step is important: We make a constraint box 
       using only the fixed WIDTH of the UILabel. The height will
       be checked later. */ 
    CGSize constraintSize = CGSizeMake(260.0f, MAXFLOAT);

    // This step checks how tall the label would be with the desired font.
    CGSize labelSize = [self.ourText sizeWithFont:font constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];

    /* Here is where you use the height requirement!
       Set the value in the if statement to the height of your UILabel
       If the label fits into your required height, it will break the loop
       and use that font size. */
    if(labelSize.height <= 180.0f)
        break;
}
// You can see what size the function is using by outputting: NSLog(@"Best size is: %u", i);

// Set the UILabel's font to the newly adjusted font.
msg.font = font;

// Put the text into the UILabel outlet variable.
msg.text = self.ourText;


i think its bit late for this question but since this comes as the top result in google for this query a more suitable (copy & paste) solution maybe following

- (BOOL)textViewShouldEndEditing:(UITextView *)textView{

if (textView.contentSize.height > textView.frame.size.height) {
  int fontIncrement = 1;
    while (textView.contentSize.height > textView.frame.size.height) {
        textView.font = [UIFont fontWithName:@"Copperplate" size:25.0 - fontIncrement];
        fontIncrement++;

    }

}
else {
    int fontIncrement = 1;
    while (textView.font.pointSize < 25.0) {
        textView.font = [UIFont fontWithName:@"Copperplate" size:8.0 + fontIncrement];
        fontIncrement++;

    }
}
return YES;

}

in the code above 25 is tha maxFontSize variable you want to set for the textview.

p.s. (BOOL)textViewShouldEndEditing: is one of the delegate methods of uitextview called just before editing is done and keyboard is resigned , hence you should also appropriately include delegate protol in your view controller.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜