73

画面全体を埋める大きなテキストの中央にテキストを垂直に配置したいUITextViewので、テキストが少ない場合、たとえばいくつかの単語が高さによって中央に配置されるようにします。これは、テキストを中央に配置すること (IB にあるプロパティ) に関する問題ではなく、テキストが短い場合にテキストを の真ん中に垂直に配置することに関する問題なので、 に空白の領域はありません。これはできますか?前もって感謝します!UITextView UITextView

4

19 に答える 19

26

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)
}
于 2015-05-06T20:39:20.707 に答える
11

制約のみを使用して直接設定できます。

以下のように、制約でテキストを垂直方向と水平方向に揃えるために追加した 3 つの制約があります。

ここに画像の説明を入力

  1. 高さを 0 にして、より大きな制約を追加します
  2. 親の制約に垂直方向の位置合わせを追加します
  3. 親制約に水平方向の位置合わせを追加

ここに画像の説明を入力

于 2017-08-10T13:06:25.547 に答える
6
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)
}
于 2015-08-14T19:42:49.013 に答える
6

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
于 2016-02-05T17:06:12.070 に答える
5

私もこの問題を抱えており、UITableViewCellwith で解決しました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

この解決策は問題なく機能しました。試してみてください。

于 2013-09-24T07:59:51.597 に答える
2

自動レイアウト ソリューション:

  1. UITextView のコンテナーとして機能する UIView を作成します。
  2. 次の制約を追加します。
    • TextView: 先頭のスペースを次の位置に揃えます: コンテナー
    • TextView: 末尾のスペースを次の場所に揃えます: コンテナー
    • TextView: センター Y を次の位置に揃えます: コンテナ
    • TextView: 等しい高さ: コンテナー、関係: ≤</li>
于 2015-03-08T10:33:10.277 に答える
1

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};
    });
});
于 2015-07-27T12:23:00.423 に答える
0

私はこのようにしました: まず、UITextView を UIView に埋め込みました (これは mac OS でも機能するはずです)。次に、外部 UIView の 4 つの側面すべてをコンテナの側面に固定し、UITextView と同様または等しい形状とサイズにしました。したがって、UITextView 用の適切なコンテナーがありました。次に、UITextView の左右の境界線を UIView の両側に固定し、UITextView に高さを与えました。最後に、UITextView を UIView の垂直方向の中央に配置しました。Bingo :) UITextView が UIView の垂直方向の中央に配置されるようになったため、UITextView 内のテキストも垂直方向の中央に配置されます。

于 2016-09-28T06:18:13.533 に答える
-1

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
于 2015-05-18T21:02:47.867 に答える
-4

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
于 2016-09-20T19:35:43.830 に答える