ios - UiTextView - 仅添加一行时的位置

标签 ios objective-c cocoa-touch cocoa

当我的 UiTextView 添加 2 行或更多行时,它看起来不错,但当用户只输入一行时,文本停留在 UiTextView 的中间。我怎样才能强制它留在 UiTextView 的左上角?

最佳答案

您可以查看此博客 ( iOS: Vertical aligning text in a UITextView ),其中解释了如何进行居中和底部垂直对齐。使用类似的逻辑并在您的案例中将 contentOffset y 参数设置为零以使其顶部对齐。

这是博客中的代码,

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

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
   UITextView *tv = object;
   //Center vertical alignment
   //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};

   //Bottom vertical alignment
   CGFloat topCorrect = ([tv bounds].size.height - [tv contentSize].height);
    topCorrect = (topCorrect <0.0 ? 0.0 : topCorrect);
    tv.contentOffset = (CGPoint){.x = 0, .y = -topCorrect};
}

关于ios - UiTextView - 仅添加一行时的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14408668/

相关文章:

ios - IPA 设置创建失败

ios - 我无法在ios uiwebview中的phoneGap中加载JQuery

ios - 被拒绝的应用程序,因为应用程序在尝试下载其中一项应用程序内购买时崩溃

ios - NSNumberFormatter stringFromNumber返回1,000,000,000用于输入999999999

cocoa-touch - 具有自定义 “scale-from-center” ModalTransitionStyle 的 PresentViewController

ios - 在 Objective C 中调用同一个 View "N"次

iphone - UIView和内存管理

ios - 尺寸等级和加号电话

iphone - NSTimeZone timeZoneWithName : works not properly

objective-c - 您如何创建交互式快速查看插件缩略图?