iOS UItextViewコンテンツ表示不全

1627 ワード

UItextViewをUILabelとして使用する必要がある場合(UItextViewに付属のコピー、貼り付け、選択機能、電話、メールボックス、リンクなどを自動的に認識するため)は、UItextViewのいくつかのプロパティを無効にするだけです.
textView.editable = NO;//編集不可scrollEnabled = NO;//スクロール不可
それから文字の大きさを計算してUItextViewのframeを設定して、私のここはxibの中のtextViewで、
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:notification.content]; NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init]; [paragraphStyle setLineSpacing:5];
NSRange range = NSMakeRange(0,notification.content.length);

[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:range];
[attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHex:0x333333] range:range];
[attributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15.0] range:range];

self.textView.attributedText = nil;
self.textView.attributedText = attributedString;

CGSize size = [self.textView.attributedText boundingRectWithSize:CGSizeMake(ScreenWidth-2*16, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin context:nil].size;

self.textViewH.constant = size.height;

この方法でUILabelで使用しても問題はありませんが、テキストの表示が不完全な場合や、計算された高さが文字の実際の高さよりも大きい場合がありますので、さらにプロパティを設定する必要があります.
[self.textView setContentInset:UIEdgeInsetsMake(-10, -5, -15, -5)];//  UITextView    
[self.textView setTextAlignment:NSTextAlignmentLeft];//      
self.textView.layoutManager.allowsNonContiguousLayout = NO;
self.textView.scrollEnabled = YES;  //    scrollEnabled=NO,           ,       YES, textView       ,