Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/120.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何水平对齐文本&;在UITextView中垂直显示?_Ios_Uitextview - Fatal编程技术网

Ios 如何水平对齐文本&;在UITextView中垂直显示?

Ios 如何水平对齐文本&;在UITextView中垂直显示?,ios,uitextview,Ios,Uitextview,如何在UITextView中水平和垂直对齐文本? 我希望将UITextView中的文本与水平对齐和垂直对齐对齐。 有什么定制方式吗? 请帮帮我……据我所知,没有内置的方法来垂直对齐UITextView。但是,通过更新contentOffset可以获得垂直居中的文本: [textView setTextAlignment:NSTextAlignmentCenter]; - (void)viewWillAppear:(BOOL)animated { [super viewWillAppea

如何在UITextView中水平和垂直对齐文本? 我希望将UITextView中的文本与水平对齐和垂直对齐对齐。 有什么定制方式吗?
请帮帮我……

据我所知,没有内置的方法来垂直对齐
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};
}
[textView setTextAlignment:NSTextAlignmentCenter];
-(无效)视图将显示:(BOOL)动画{
[超级视图将显示:动画];
[textView addObserver:self-forKeyPath:@“contentSize”选项:(NSKeyValueObservingOptionNew)上下文:NULL];
}
-(无效)视图将消失:(BOOL)已设置动画{
[超级视图将消失:动画];
[textView removeObserver:self-forKeyPath:@“contentSize”];
}
-(void)observeValueForKeyPath:(NSString*)对象的键路径:(id)对象更改:(NSDictionary*)更改上下文:(void*)上下文
{
UITextView*tv=对象;
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};
}

当您想要更改水平和垂直对齐时,您应该提供您想要执行的操作的上下文

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


UITextview中的文本有一个名为textalignment的属性,您可以将该属性设置为NSTextAlignmentLeft、NSTEXTAlignmentJustized,正如建议的那样,您也可以调整容器的内容偏移属性,但我发现调整框架更为直接

使用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)

}
override func observeValueForKeyPath(键路径:String,of对象对象:AnyObject,change:[NSObject:AnyObject],上下文:UnsafeMutablePointer){
var topCorrect:CGFloat=(textField.frame.height-textField.contentSize.height);
topCorrect=topCorrect<0.0?0.0:topCorrect/2
textField.contentOffset=CGPoint(x:0,y:-topCorrect)
}

文本字段连接到视图中的实际文本字段。

这是一个快速的解决方案,现在有所不同,因为内容插入和偏移略有变化。此解决方案适用于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
}
///强制UITextView中的文本始终居中。
重写func observeValueForKeyPath(键路径:String?,对象对象的类型:AnyObject?,更改:[String:AnyObject]?,上下文:UnsafeMutablePointer){
将textView=对象设为!UITextView
var topCorrect=(textView.bounds.size.height-textView.contentSize.height*textView.zoomScale)/2
topCorrect=topCorrect<0.0?0.0:topCorrect;
textView.contentInset.top=topCorrect
}

这段代码到哪里去了?(ViewController.m?@Cindeselia按照当前编写代码的方式,您需要将其添加到文本视图所在的位置。请注意,只有在选中文本视图的“可选”标志时,更改contentOffset似乎才起作用。该标志正常工作,但不知怎的,它无法与展开段一起工作。。。知道为什么吗?这在IOS 9/Swift 2上对我来说非常有效,谢谢!这是可行的,但如果我有多个UITextField,它只适用于其中一个不在Xcode 8中工作的UITextField。有人知道如何做到这一点吗?有人用iOS 10、Swift 3和Xcode 8做到了这一点吗?我在iOS 10、Swift 3和Xcode 8上工作得很好。(需要轻微的语法更改)