UITextView 与底部对齐

发布于 2024-12-02 00:13:25 字数 64 浏览 0 评论 0原文

我想知道如何设置 UITextView 以使里面的文本与底部对齐?就像,如果你开始打字,你总是会在最下面一行打字?

I wondered how to set the UITextView so that the text inside aligns to the bottom? Like, if you start typing, you will always type in the lowest row?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

旧时模样 2024-12-09 00:13:25

我不相信有一种内置方法可以垂直对齐 UITextView 中的文本,但您应该能够使用 contentOffset 属性UITextView 类来完成此任务。有关更多详细信息,请参阅此博客文章:

http://imagineric.ericd.net/2011/03/10/ios-vertical-aligning-text-in-a-uitextview/

更新:自上述链接似乎已失效,我使用 Wayback Machine 检索 存档副本 并粘贴了下面文章的文本。

iOS 中开箱即用的 UITextView(多行文本)的默认和预期行为是将文本在其容器中的左上角对齐。大多数时候这都很有效。但是,我最近需要将文本在控件内垂直居中或将所有文本对齐到控件的底部。

您可以向控件添加一个观察者,当其内容大小发生变化(设置文本)时,触发一个方法。然后,该方法可以处理文本在控件中的实际定位方式。如果进行对齐没有意义,它将默认为控件的正常行为(顶部垂直对齐)。

我们开始:

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

您可以决定要使用哪种类型,或者刷新该方法以获取您要使用的垂直对齐类型的参数。这工作得很好,如果这些属性一开始就内置到控件中那就太好了。

如果你能使用它,那就尽情享受吧。

I don't believe there is a built-in way to vertically align the text in a UITextView, but you should be able to use the contentOffset property of the UITextView class to accomplish this. See this blog post for more details:

http://imagineric.ericd.net/2011/03/10/ios-vertical-aligning-text-in-a-uitextview/

Update: since the above link appears to be dead, I used the Wayback Machine to retrieve an archived copy and have pasted the text of the article below.

The default and expected behavior of a UITextView (multi-line text) out of the box in iOS is to align the text top left in it’s container. That works well most of the time. However I recently had need to either center the text vertically within the control or have all the text align to the bottom of the control.

You can add an observer to the control and when it’s content size changes (text is set), fire a method. The method can then handle how the text is actually positioned in the control. If it doesn’t make sense to do the alignment, it will default to the normal behavior of the control (top vertical alignment).

Here we go:

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

You can decide which you’d like to use, or flush the method out to take an argument for which type of vertical alignment you’d like to use. This works quite well and it would have been nice if the properties were built into the control to begin with.

If you can use it, enjoy.

岁月静好 2024-12-09 00:13:25

一种方法可能是使用多行 UILabel,然后使用如下方法:

http://texnological.blogspot.com/2011/08/how-to-do-vertical-alignment-for.html

感谢作者!

One method could be to use a multi-line UILabel then use a method like the following:

http://texnological.blogspot.com/2011/08/how-to-do-vertical-alignment-for.html

Credit to the author!

请爱~陌生人 2024-12-09 00:13:25

使用自动布局对我有用:

1)布局您的 UITextView ,以便高度足够大以容纳单行文本,并且底部固定在屏幕底部(或文本视图下方的某个位置) 。

2) 在视图控制器中保留对文本视图高度的 NSLayoutConstraint 的引用。

3) 将视图控制器设置为 UITextViewDelegate

4) 输入文本时调整文本视图的高度并强制布局。例如:

- (void)textViewDidChange:(UITextView *)textView;
{
    self.textViewHeightConstraint.constant = textView.contentSize.height;
    [self.textView layoutIfNeeded];
}

Using autolayout worked for me:

1) Layout your UITextView so that the height is large enough for a single line of text and the bottom is pinned to the bottom of the screen (or something underneath the text view).

2) Keep a reference in your view controller to the NSLayoutConstraint for the text view height.

3) Setup your view controller to be a UITextViewDelegate

4) adjust the height of the text view when text is entered and force a layout. For example:

- (void)textViewDidChange:(UITextView *)textView;
{
    self.textViewHeightConstraint.constant = textView.contentSize.height;
    [self.textView layoutIfNeeded];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文