如何在UITextView中水平垂直对齐文本?

6
如何在UITextView中水平和垂直对齐文本?
我想要在UITextView中进行水平和垂直对齐文本。
是否有任何自定义方式?
请帮忙解决。
4个回答

26

这是一个使用Swift的解决方案,由于内容插入和偏移量略微改变,因此现在与以前不同。该解决方案可在Xcode 7 beta 6中使用。

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    textView.addObserver(self, forKeyPath: "contentSize", options: NSKeyValueObservingOptions.New, context: nil)
}

override func viewWillDisappear(animated: Bool) {
    super.viewWillDisappear(animated)
    textView.removeObserver(self, forKeyPath: "contentSize")
}

苹果已经改变了内容偏移和插入的工作方式,现在需要使用这个稍微修改过的解决方案来设置内容插入的顶部,而不是偏移量。

/// Force the text in a UITextView to always center itself.
override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
    let textView = object as! UITextView
    var topCorrect = (textView.bounds.size.height - textView.contentSize.height * textView.zoomScale) / 2
    topCorrect = topCorrect < 0.0 ? 0.0 : topCorrect;
    textView.contentInset.top = topCorrect
}

1
这对我来说非常有效,适用于IOS 9 / Swift 2,谢谢! - Kitson
这个可以工作,但如果我有多个UITextField,它只适用于其中一个。 - Ashish Musale
2
Xcode 8中无法工作。有人有任何想法如何解决吗? - George Asda
有人在iOS 10、Swift 3和Xcode 8上实现了这个吗? - gaskbr
在我的iOS 10、Swift 3、Xcode 8上运行良好(需要进行轻微的语法更改)。 - David Schmoecker
在Swift 4.2中,重写函数observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?)。其中,let textView = object as! UITextView,var topCorrect = (textView.bounds.size.height - textView.contentSize.height * textView.zoomScale) / 2,topCorrect = topCorrect < 0.0 ? 0.0 : topCorrect,textView.contentInset.top = topCorrect。 - iOSDev

7
据我所知,UITextView没有内置的垂直对齐方法。但是,您可以通过更新contentOffset来使文本垂直居中:
[textView setTextAlignment:NSTextAlignmentCenter];

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [textView addObserver:self forKeyPath:@"contentSize" options:(NSKeyValueObservingOptionNew) context:NULL];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [textView removeObserver:self forKeyPath:@"contentSize"];
}

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

这段代码放在哪里?(ViewController.m文件中?) - Erika Electra
@Cindeselia 目前代码的写法,你需要将它添加到你的文本视图所在的位置。 - Gad
请注意,仅在选中文本视图的“可选”标志已选中的情况下更改contentOffset似乎有效。 - Maiaux

5

使用Swift垂直居中:

在viewDidLoad方法中:

textField.addObserver(self, forKeyPath: "contentSize", options: NSKeyValueObservingOptions.New, context: nil)

然后在你的视图控制器中的其他位置:

override func observeValueForKeyPath(keyPath: String, ofObject object: AnyObject, change: [NSObject : AnyObject], context: UnsafeMutablePointer<Void>) {

    var topCorrect : CGFloat = (textField.frame.height - textField.contentSize.height);
    topCorrect = topCorrect < 0.0 ? 0.0 : topCorrect / 2
    textField.contentOffset = CGPoint(x: 0, y: -topCorrect)

}

textField是与视图中的实际文本字段连接起来的。


它可以正常工作,但是在使用取消展开(segue)时却无法正常工作...你有什么想法吗? - SKYnine

0

当你想要改变水平和垂直对齐时,你应该提供上下文说明你的意图。

UITextview有一个容器和一个框架属性,你可以将其与另一个UITextview/Imageview等对齐。框架属性有一个原点和一个大小,你可以修改它们来对齐你的视图。

UITextview中的文本有一个称为textalignment的属性,你可以将其设置为NSTextAlignmentLeft、NSTextAlignmentJustified。建议你也可以调整容器的内容偏移量属性,但我发现调整框架更加直观。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接