10
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Swiftでtextarea

Last updated at Posted at 2014-12-29

xcodeで開発するiOSのUI部品でテキストエリアに対応するものが見当たらなかったので実装してみた

import UIKit

public class UITextArea: UITextView {

    var placeholder: String!
    var placeholderColor: UIColor!
    lazy var placeholderLabel: UILabel = UILabel()
    var tipFontSize: CGFloat = 14

    required public init(coder: NSCoder) {
        super.init(coder: coder)
        self.layer.borderColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.1).CGColor
        self.layer.borderWidth = 0.8
        self.layer.masksToBounds = true
        self.layer.cornerRadius = 8.0
    }
    
    override public func awakeFromNib() {
        super.awakeFromNib()
        if (self.placeholder == nil) {
            self.placeholder = ""
        }
        if (self.placeholderColor == nil) {
            self.placeholderColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.2)
        }
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "textDidChanged:", name: UITextViewTextDidChangeNotification, object: nil)
    }
    
    public func textDidChanged(notification: NSNotification!) {
        
        if self.placeholder!.utf16Count == 0 {
            return
        }
        
        if self.text.utf16Count == 0 {
            self.viewWithTag(999)?.alpha = 1.0
        } else {
            self.viewWithTag(999)?.alpha = 0.0
        }
    }
    
    func setText(text: String) {
        super.text = text
        self.textDidChanged(nil)
    }
    
    override public func drawRect(rect: CGRect) {
        if 0 < self.placeholder!.utf16Count {
            self.placeholderLabel.frame = CGRect(x: 8, y: 6, width: self.bounds.size.width - 16, height: 0)
            self.placeholderLabel.text = self.placeholder
            self.placeholderLabel.tag = 999
            self.placeholderLabel.alpha = 0
            self.placeholderLabel.textColor = self.placeholderColor
            self.placeholderLabel.backgroundColor = UIColor.clearColor()
            self.placeholderLabel.font = UIFont.systemFontOfSize(self.tipFontSize)
            self.placeholderLabel.lineBreakMode = NSLineBreakMode.ByWordWrapping
            self.placeholderLabel.numberOfLines = 0
            self.addSubview(self.placeholderLabel)
        }
        self.placeholderLabel.sizeToFit()
        self.sendSubviewToBack(self.placeholderLabel)
        
        if self.text.utf16Count == 0 && 0 < self.placeholder!.utf16Count {
            self.viewWithTag(999)?.alpha = 1.0
        }
        
        super.drawRect(rect)
    }

    deinit {
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
}
10
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?