【问题标题】:UITextView aligning text vertically centerUITextView 垂直居中对齐文本
【发布时间】:2013-06-06 03:18:40
【问题描述】:

我想在UItextView 中垂直居中对齐文本。

我正在使用以下代码

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}

;

不知何故,这在 iOS 5 中不起作用,因为返回的 contentSize 与我在 iOS6 中得到的不同。

知道为什么 iOS 5 和 iOS 6 中相同 textView 的 contentSize 不同吗?

【问题讨论】:

标签: ios uitextview


【解决方案1】:

在视图加载时为 UITextView 的 contentSize 键值添加观察者:-

- (void) viewDidLoad {
  [textField addObserver:self forKeyPath:@"contentSize" options:(NSKeyValueObservingOptionNew) context:NULL];
  [super viewDidLoad];
}

每次 contentSize 值变化时调整 contentOffset :-

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

希望对你有帮助...

您可以参考

https://github.com/HansPinckaers/GrowingTextView

【讨论】:

  • 这不会使文本始终垂直居中,因为如果调整 UITextView 的大小(例如,当键盘出现时),那么 UITextView 中的内容将恢复为顶部对齐。
  • 除了作为 keypath 观察者的价值之外,这是一段很棒的代码。谢谢。
  • 我在 UITableView 中使用此代码时发生了崩溃。在 ios8 上运行良好,但在对齐完成后还需要移除观察者。将此行添加为observeValueForKeyPath 方法的最后一行:[tv removeObserver:self forKeyPath:@"contentSize"];
【解决方案2】:

在 iOS7 上的 observeValueForKeyPath 方法上试试这个:

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
    {
UITextView *tv = object;

CGFloat height = [tv bounds].size.height;
CGFloat contentheight;

if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7")) {
    contentheight = [tv sizeThatFits:CGSizeMake(tv.frame.size.width, FLT_MAX)].height;
    NSLog(@"iOS7; %f %f", height, contentheight);
}else{
    contentheight = [tv contentSize].height;
    NSLog(@"iOS6; %f %f", height, contentheight);
}

CGFloat topCorrect = height - contentheight;
topCorrect = (topCorrect <0.0 ? 0.0 : topCorrect);
tv.contentOffset = (CGPoint){.x = 0, .y = -topCorrect};
}

待定义:

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

【讨论】:

    【解决方案3】:

    我已经修改了 Arpit 的解决方案,使其可以适应扩展的 textview contentView

    static NSString *const kContentOffsetKeyPath = @"contentOffset";
    
    -(void)viewDidLoad {
         [self.replyTextView addObserver:self
                             forKeyPath:kContentOffsetKeyPath
                                options:(NSKeyValueObservingOptionNew)
                                context:NULL];
    }
    
    - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
        if ([keyPath isEqualToString:kContentOffsetKeyPath]) {
            // To keep scrolling to bottom while typing and text content view is larger than maximum limit
            if (textView.contentSize.height > singleLineHeight) {
                UITextView *textView = object;
                CGFloat topCorrect = ([textView bounds].size.height - [textView contentSize].height * [textView zoomScale]) / 2.0;
                CGFloat fineTune = -3.0;
                topCorrect = (topCorrect < fineTune ? fineTune : topCorrect);
                // To avoid recursion
                [textView removeObserver:self forKeyPath:kContentOffsetKeyPath];
                textView.contentOffset = (CGPoint){.x = 0, .y = -topCorrect};
                // add observer back
                [textView addObserver:self
                           forKeyPath:kContentOffsetKeyPath
                              options:(NSKeyValueObservingOptionNew)
                              context:NULL];
            }
        }
    }
    
    - (void)dealloc {
        [self.replyTextView removeObserver:self forKeyPath:kContentOffsetKeyPath];
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-26
      • 1970-01-01
      • 2013-05-25
      • 2012-09-17
      • 1970-01-01
      • 2023-03-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多