ios 如何在 UITextView 中水平和垂直对齐文本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21669688/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to align text horizontally & vertically in UITextView?
提问by JKMania
How to align text horizontally & vertically in UITextView? I want align text in UITextView with horizontal alignment & vertical alignment. Is there any custom way? Please help me out.....
如何在 UITextView 中水平和垂直对齐文本?我想在 UITextView 中使用水平对齐和垂直对齐来对齐文本。有什么自定义方法吗?请帮帮我.....
回答by Hamer
This is a Swift solution and is different now since content insets and offsets have changed slightly. This solution works in Xcode 7 beta 6.
这是一个 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")
}
Apple has changed how content offsets and insets work this slightly modified solution is now required to set the top on the content inset instead of the offset.
Apple 已经改变了内容偏移和插入的工作方式,这个稍微修改的解决方案现在需要设置内容插入的顶部而不是偏移。
/// 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
}
回答by Gad
As far as I know there is no built in method for vertical alignment of a UITextView
. However, by updating the the contentOffset
you can get vertically centered text:
据我所知,没有用于垂直对齐的内置方法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};
}
回答by maxmzd
Vertically aligning middle with Swift:
与 Swift 垂直对齐中间:
In viewDidLoad:
在 viewDidLoad 中:
textField.addObserver(self, forKeyPath: "contentSize", options: NSKeyValueObservingOptions.New, context: nil)
Then somewhere else in your view controller:
然后在您的视图控制器中的其他地方:
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)
}
where textField is wired up to the actual text field in your view.
其中 textField 连接到视图中的实际文本字段。
回答by Paulo
You should provide the context on what you mean to do when you want to change the horizontal and vertical alignment,
当您想要更改水平和垂直对齐方式时,您应该提供有关您要执行的操作的上下文,
The UITextview has a container and a frame property that you can align with another UITextview/ Imageview, etc. The frame property has an origin and a size that you can modify to align your views.
UITextview 有一个容器和一个 frame 属性,您可以将其与另一个 UITextview/Imageview 等对齐。 frame 属性有一个原点和一个大小,您可以修改以对齐您的视图。
The text within the UITextview has a property called the textalignment which you can set to NSTextAlignmentLeft, NSTextAlignmentJustified, As suggested, you may also adjust the content offset property of the container but I find that adjusting the frame is more straight forward.
UITextview 中的文本有一个名为 textalignment 的属性,您可以将其设置为 NSTextAlignmentLeft、NSTextAlignmentJustified,根据建议,您也可以调整容器的内容偏移属性,但我发现调整框架更直接。