How to align text horizontally & vertically in UIT

2020-02-26 12:47发布

问题:

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.....

回答1:

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:

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


回答2:

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.

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.

/// 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
}


回答3:

Vertically aligning middle with Swift:

In 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.



回答4:

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.

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.