2011-01-14 7 views
1

나는 사람들이 자주하고 싶어하기 때문에 쉬운 일이라고 생각 하겠지만, 나는 주위를 둘러 보았고 다른 접근법을 시도했지만 아무것도 작동하지 않는 것 같다.UITextView autoSize

내가하고 싶은 것은 두 줄의 Text가있는 UITextView를 만드는 것입니다.

텍스트가 너무 길어서 3 줄로 표시되는 경우 2 줄로 채울 때까지 글꼴을 자동 크기 조정하려고합니다.

개념적으로 필자는 텍스트 필드의 높이를 기준으로 두 줄에 맞을 때까지 텍스트를 축소하는 반복적 인 기능을 수행 할 계획 이었지만 그럴 수는 없었습니다.

모든 의견을 크게 환영합니다.

+0

편집 가능해야합니까, 아니면 UILabel이 작동합니까? –

+0

텍스트는 편집 할 수 없습니다. 기본적으로 2 줄의 UILabel을 찾고 있습니다. –

답변

2

다른 사람이이 문제를 가로 질러 온다, 나는이 여기에 대한 해답을 발견 :

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; 
3

UILabel의를 사용하여 다음과 같은 속성을 설정합니다 경우

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

작동하지 않는 것 같습니다. 텍스트가 하나 이상인 경우 자동 스크롤 할 수 있다고 생각하지 않습니다. –

0

를 내가 이 질문에 대해 조금 늦게 생각합니다. 그러나이 질문에 대한 Google의 결과가 더 적합하기 때문에 (복사 & 붙여 넣기) 어쩌면 25 위의 코드에서

}

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

다음과 같은 솔루션을 사용하면 텍스트 뷰에 설정할 그쪽으로 maxFontSize 변수입니다.

p.s. (BOOL) textViewShouldEndEditing : 편집이 끝나기 직전에 호출 된 uitextview의 위임 메서드 중 하나이며 키보드가 사임되므로 뷰 컨트롤러에 delegate protol도 적절하게 포함해야합니다.