画面全体を埋める大きなテキストの中央にテキストを垂直に配置したいUITextView
ので、テキストが少ない場合、たとえばいくつかの単語が高さによって中央に配置されるようにします。これは、テキストを中央に配置すること (IB にあるプロパティ) に関する問題ではなく、テキストが短い場合にテキストを の真ん中に垂直に配置することに関する問題なので、 に空白の領域はありません。これはできますか?前もって感謝します!UITextView
UITextView
19 に答える
KVO を使用したくない場合は、このコードを次のような関数にエクスポートしてオフセットを手動で調整することもできます。
-(void)adjustContentSize:(UITextView*)tv{
CGFloat deadSpace = ([tv bounds].size.height - [tv contentSize].height);
CGFloat inset = MAX(0, deadSpace/2.0);
tv.contentInset = UIEdgeInsetsMake(inset, tv.contentInset.left, inset, tv.contentInset.right);
}
そしてそれを呼び出す
-(void)textViewDidChange:(UITextView *)textView{
[self adjustContentSize:textView];
}
コード内のテキストを編集するたびに。コントローラーをデリゲートとして設定することを忘れないでください
スウィフト 3 バージョン:
func adjustContentSize(tv: UITextView){
let deadSpace = tv.bounds.size.height - tv.contentSize.height
let inset = max(0, deadSpace/2.0)
tv.contentInset = UIEdgeInsetsMake(inset, tv.contentInset.left, inset, tv.contentInset.right)
}
func textViewDidChange(_ textView: UITextView) {
self.adjustContentSize(tv: textView)
}
func alignTextVerticalInTextView(textView :UITextView) {
let size = textView.sizeThatFits(CGSizeMake(CGRectGetWidth(textView.bounds), CGFloat(MAXFLOAT)))
var topoffset = (textView.bounds.size.height - size.height * textView.zoomScale) / 2.0
topoffset = topoffset < 0.0 ? 0.0 : topoffset
textView.contentOffset = CGPointMake(0, -topoffset)
}
lineFragmentPadding
自動レイアウトで使用し、 andtextContainerInset
をゼロに設定して使用しているテキストビューがあります。私の状況では、上記の解決策はどれも機能しませんでした。しかし、これは私にとってはうまくいきます。iOS 9 でテスト済み
@interface VerticallyCenteredTextView : UITextView
@end
@implementation VerticallyCenteredTextView
-(void)layoutSubviews{
[self recenter];
}
-(void)recenter{
// using self.contentSize doesn't work correctly, have to calculate content size
CGSize contentSize = [self sizeThatFits:CGSizeMake(self.bounds.size.width, CGFLOAT_MAX)];
CGFloat topCorrection = (self.bounds.size.height - contentSize.height * self.zoomScale) / 2.0;
self.contentOffset = CGPointMake(0, -topCorrection);
}
@end
私もこの問題を抱えており、UITableViewCell
with で解決しましたUITextView
。カスタムUITableViewCell
サブクラス プロパティでメソッドを作成しましたstatusTextView
。
- (void)centerTextInTextView
{
CGFloat topCorrect = ([self.statusTextView bounds].size.height - [self.statusTextView contentSize].height * [self.statusTextView zoomScale])/2.0;
topCorrect = ( topCorrect < 0.0 ? 0.0 : topCorrect );
self.statusTextView.contentOffset = (CGPoint){ .x = 0, .y = -topCorrect };
メソッドでこのメソッドを呼び出します。
- (void)textViewDidBeginEditing:(UITextView *)textView
- (void)textViewDidEndEditing:(UITextView *)textView
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
この解決策は問題なく機能しました。試してみてください。
自動レイアウト ソリューション:
- UITextView のコンテナーとして機能する UIView を作成します。
- 次の制約を追加します。
- TextView: 先頭のスペースを次の位置に揃えます: コンテナー
- TextView: 末尾のスペースを次の場所に揃えます: コンテナー
- TextView: センター Y を次の位置に揃えます: コンテナ
- TextView: 等しい高さ: コンテナー、関係: ≤</li>
You can try below code, no observer mandatorily required. observer throws error sometimes when view deallocates. You can keep this code in viewDidLoad, viewWillAppear or in viewDidAppear anywhere.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
dispatch_async(dispatch_get_main_queue(), ^(void) {
UITextView *tv = txtviewDesc;
CGFloat topCorrect = ([tv bounds].size.height - [tv contentSize].height * [tv zoomScale])/2.0;
topCorrect = ( topCorrect < 0.0 ? 0.0 : topCorrect );
tv.contentOffset = (CGPoint){.x = 0, .y = -topCorrect};
});
});
私はこのようにしました: まず、UITextView を UIView に埋め込みました (これは mac OS でも機能するはずです)。次に、外部 UIView の 4 つの側面すべてをコンテナの側面に固定し、UITextView と同様または等しい形状とサイズにしました。したがって、UITextView 用の適切なコンテナーがありました。次に、UITextView の左右の境界線を UIView の両側に固定し、UITextView に高さを与えました。最後に、UITextView を UIView の垂直方向の中央に配置しました。Bingo :) UITextView が UIView の垂直方向の中央に配置されるようになったため、UITextView 内のテキストも垂直方向の中央に配置されます。
UITextView+VerticalAlignment.h
// UITextView+VerticalAlignment.h
// (c) The Internet 2015
#import <UIKit/UIKit.h>
@interface UITextView (VerticalAlignment)
- (void)alignToVerticalCenter;
- (void)disableAlignment;
@end
UITextView+VerticalAlignment.m
#import "UITextView+VerticalAlignment.h"
@implementation UITextView (VerticalAlignment)
- (void)alignToVerticalCenter {
[self addObserver:self forKeyPath:@"contentSize" options:NSKeyValueObservingOptionNew context:NULL];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
UITextView *tv = object;
CGFloat topCorrect = ([tv bounds].size.height - [tv contentSize].height * [tv zoomScale])/2.0;
topCorrect = ( topCorrect < 0.0 ? 0.0 : topCorrect );
tv.contentOffset = (CGPoint){.x = 0, .y = -topCorrect};
}
- (void)disableAlignment {
[self removeObserver:self forKeyPath:@"contentSize"];
}
@end
RubyMotion での iOS10 のソリューション:
class VerticallyCenteredTextView < UITextView
def init
super
end
def layoutSubviews
self.recenter
end
def recenter
contentSize = self.sizeThatFits(CGSizeMake(self.bounds.size.width, Float::MAX))
topCorrection = (self.bounds.size.height - contentSize.height * self.zoomScale) / 2.0;
topCorrection = 0 if topCorrection < 0
self.contentInset = UIEdgeInsetsMake(topCorrection, 0, 0, 0)
end
end