iOS7 UITextView contentsize.alternatywa wysokości

[2]}przenoszę jedną z aplikacji z iOS 6.1 na iOS 7. Używam układu, w którym jest UITextView, który ma szerokość fix, ale jego wysokość jest oparta na jego contentsize. Dla iOS 6.1 sprawdzanie zawartości.wysokość i ustawienie jej jako wysokości ramki widoku tekstowego wystarczyło, ale nie działa to na iOS 7.

Jak mogę utworzyć UITextView o stałej szerokości, ale dynamicznej wysokości w oparciu o wyświetlany tekst?

Uwaga: tworzę te widoki z kodu, a nie z Interface Builder.

Author: Bartłomiej Semańczyk, 2013-09-26

9 answers

Za pomocą poniższego kodu możesz zmienić wysokość UITextView w zależności od stałej szerokości (działa na iOS 7 i poprzedniej wersji):

- (CGFloat)textViewHeightForAttributedText:(NSAttributedString *)text andWidth:(CGFloat)width
{
    UITextView *textView = [[UITextView alloc] init];
    [textView setAttributedText:text];
    CGSize size = [textView sizeThatFits:CGSizeMake(width, FLT_MAX)];
    return size.height;
}

dzięki tej funkcji, będziesz wziąć NSAttributedString i stałą szerokość, aby zwrócić wymaganą wysokość.

Jeśli chcesz obliczyć ramkę z tekstu z określoną czcionką, musisz użyć następującego kodu:

- (CGSize)text:(NSString *)text sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size
{
    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0"))
    {
        CGRect frame = [text boundingRectWithSize:size
                                          options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading)
                                       attributes:@{NSFontAttributeName:font}
                                          context:nil];
        return frame.size;
    }
    else
    {
        return [text sizeWithFont:font constrainedToSize:size];
    }
}

możesz dodać SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO na swoim prefiksie.plik pch w Twoim projekcie as:

#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)

Można również zastąpić poprzedni test SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) przez:

if ([text respondsToSelector:@selector(boundingRectWithSize:options:attributes:context:)])‌
 177
Author: Jordan Montel,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-02-07 17:31:52

To działało dla mnie dla iOS6 i 7:

CGSize textViewSize = [self.myTextView sizeThatFits:CGSizeMake(self.myTextView.frame.size.width, FLT_MAX)];
    self.myTextView.height = textViewSize.height;
 42
Author: Ana,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-10-09 13:19:29

W iOS7, UITextView używa NSLayoutManager do układania tekstu:

// If YES, then the layout manager may perform glyph generation and layout for a given portion of the text, without having glyphs or layout for preceding portions.  The default is NO.  Turning this setting on will significantly alter which portions of the text will have glyph generation or layout performed when a given generation-causing method is invoked.  It also gives significant performance benefits, especially for large documents.
@property(NS_NONATOMIC_IOSONLY) BOOL allowsNonContiguousLayout;

Disable allowsNonContiguousLayout to fix contentSize:

textView.layoutManager.allowsNonContiguousLayout = NO;
 20
Author: nova,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-02-27 11:02:21

Użyj tej małej funkcji

-(CGSize) getContentSize:(UITextView*) myTextView{
    return [myTextView sizeThatFits:CGSizeMake(myTextView.frame.size.width, FLT_MAX)];
}
 15
Author: Blake Hamilton,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-10-13 00:37:55

Moje ostateczne rozwiązanie opiera się na HotJard, ale zawiera zarówno górne, jak i dolne wstawki kontenera tekstowego zamiast używania 2 * fabs (textView.contentInset.top): {]}

- (CGFloat)textViewHeight:(UITextView *)textView
{
    return ceilf([textView.layoutManager usedRectForTextContainer:textView.textContainer].size.height +
                 textView.textContainerInset.top +
                 textView.textContainerInset.bottom);
}
 3
Author: xZenon,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-10-07 10:01:35

Istnieją prostsze rozwiązania, przy użyciu tej metody:

+(void)adjustTextViewHeightToContent:(UITextView *)textView;
{
    if([[UIDevice currentDevice].systemVersion floatValue] >= 7.0f){
        textView.height = [textView.layoutManager usedRectForTextContainer:textView.textContainer].size.height+2*fabs(textView.contentInset.top);
    }else{
        textView.height = textView.contentSize.height;
    }
}

UPD : działa tylko do wyświetlania tekstu (isEditable = NO)

 2
Author: HotJard,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-10-03 11:08:21
   _textView.textContainerInset = UIEdgeInsetsZero;
   _textView.textContainer.lineFragmentPadding = 0;

Just don ' t forget the lineFragmentPadding

 1
Author: ChaoWang,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-08-08 03:25:54

Proste rozwiązanie- textView.isScrollEnabled = false działa idealnie w innym widoku przewijania lub komórce tableView z UITableViewAutomaticDimension

 0
Author: Kirill Serebriakov,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-11-28 10:37:54

@Ana 's, @Blake Hamilton' s solution inswift .

var contentHeight: CGFloat = textView.sizeThatFits(textView.frame.size).height

Dobrą rzeczą dla mnie było to, że to również zwraca poprawne contentSize, gdy {[2] } jest ustawione na false. Ustawienie na false zwraca rozmiar ramki widoku tekstowego zamiast rozmiaru zawartości.

 0
Author: BEm,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-09-16 09:40:34