简体   繁体   中英

Swift - Centering the UITextView programmatically

I've added a UITextView to my view programmatically, but I can't figure out how to set the text in the vertical middle of the screen.

My code looks like this:

var textView: UITextView?
textView = UITextView(frame: view.bounds)

if let theTextView = textView{

    let blurEffect = UIBlurEffect(style: .Dark)
    let blurView = UIVisualEffectView(effect: blurEffect)
    blurView.frame.size = CGSize(width: self.view.frame.width, height: self.view.frame.height)
    blurView.center = view.center
    blurView.tag = 1

    theTextView.text = NSString(contentsOfFile: NSTemporaryDirectory() + "\(fileRef).txt", encoding: NSUTF8StringEncoding, error: nil) as String
    theTextView.font = UIFont.systemFontOfSize(16)
    theTextView.tag = 2
    theTextView.textColor = UIColor.whiteColor()
    theTextView.insertSubview(blurView, atIndex: 0)
    theTextView.textAlignment = NSTextAlignment.Center
    view.addSubview(theTextView)
}

Any suggestions on how to do this would be appreciated.

EDIT: just fixed how you have to unwrap your optional, load a String and append a path component and there is no need to use self.

var textView: UITextView?
textView = UITextView(frame: UIScreen.mainScreen().bounds)
if let textView = textView {
    let verticalCenter = NSLayoutConstraint(item: textView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0.0)
    let blurView = UIVisualEffectView(effect: UIBlurEffect(style: .Dark))
    blurView.frame.size = CGSize(width: UIScreen.mainScreen().bounds.width, height: UIScreen.mainScreen().bounds.height)
    blurView.center = view.center
    blurView.tag = 1
    textView.font = UIFont.systemFontOfSize(16)
    textView.tag = 2
    textView.text = String(contentsOfURL:  NSURL(fileURLWithPath: NSTemporaryDirectory().stringByAppendingPathComponent("\(fileRef).txt"))!, encoding: NSUTF8StringEncoding, error: nil)
    textView.textColor = UIColor.whiteColor()
    textView.insertSubview(blurView, atIndex: 0)
    textView.textAlignment = .Center
    view.addSubview(textView)
    view.addConstraint(verticalCenter)
}

You can use Autolayout for this by adding the following constraint:

let verticalCenter = NSLayoutConstraint(item: textView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0.0)
self.view.addConstraint(verticalCenter)

This adds a constraint that centers your textView horizontally within self.view . This is a lot easier to do in Interface Builder and Storyboard though.

If you don't want to use constraints (to support iOS 5 and lower), you can use autoresizingMasks for the textView like this:

textView.autoresizingMask = UIViewAutoresizing.FlexibleBottomMargin | UIViewAutoresizing.FlexibleTopMargin

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM