-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
188 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,4 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2015 Motokazu Sekine | ||
Copyright (c) 2015 CHEEBOW <[email protected]> | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
|
@@ -9,14 +7,13 @@ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
|
||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# | ||
# Be sure to run `pod lib lint PlaceholderTextView.podspec' to ensure this is a | ||
# valid spec and remove all comments before submitting the spec. | ||
# | ||
# Any lines starting with a # are optional, but encouraged | ||
# | ||
# To learn more about a Podspec see http://guides.cocoapods.org/syntax/podspec.html | ||
# | ||
|
||
Pod::Spec.new do |s| | ||
s.name = "PlaceholderTextView" | ||
s.version = "0.1.0" | ||
s.summary = "Add a placeholder to TextView." | ||
s.description = <<-DESC | ||
Add a placeholder to TextView. | ||
DESC | ||
s.homepage = "https://github.com/cheebow/PlaceholderTextView" | ||
s.license = 'MIT' | ||
s.author = { "CHEEBOW" => "[email protected]" } | ||
s.source = { :git => "https://github.com/cheebow/PlaceholderTextView.git", :tag => s.version.to_s } | ||
s.social_media_url = 'https://twitter.com/cheebow' | ||
|
||
s.platform = :ios, '8.0' | ||
s.requires_arc = true | ||
|
||
s.source_files = 'Pod/Classes/**/*' | ||
s.resource_bundles = { | ||
'PlaceholderTextView' => ['Pod/Assets/*.png'] | ||
} | ||
end |
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
// | ||
// PlaceholderTextView.swift | ||
// | ||
// Created by CHEEBOW on 2015/07/24. | ||
// Copyright (c) 2015年 CHEEBOW. All rights reserved. | ||
// | ||
|
||
import UIKit | ||
|
||
class PlaceholderTextView: UITextView { | ||
let PLACEHOLDER_LEFT_MARGIN: CGFloat = 4.0 | ||
let PLACEHOLDER_TOP_MARGIN: CGFloat = 8.0 | ||
|
||
var placeholderLabel: UILabel = UILabel() | ||
|
||
var _placeholderColor: UIColor = UIColor.lightGrayColor() | ||
var placeholderColor: UIColor { | ||
set { | ||
_placeholderColor = newValue | ||
self.placeholderLabel.textColor = self.placeholderColor | ||
} | ||
get { | ||
return _placeholderColor | ||
} | ||
} | ||
|
||
var _placeholder: String = "" | ||
var placeholder: String { | ||
set { | ||
_placeholder = newValue | ||
self.placeholderLabel.text = self.placeholder | ||
self.placeholderSizeToFit() | ||
} | ||
get { | ||
return _placeholder | ||
} | ||
} | ||
|
||
override var text: String! { | ||
set { | ||
super.text = newValue | ||
self.textChanged(nil) | ||
} | ||
get { | ||
return super.text | ||
} | ||
} | ||
|
||
override var font: UIFont! { | ||
set { | ||
super.font = newValue | ||
self.placeholderLabel.font = newValue | ||
self.placeholderSizeToFit() | ||
} | ||
get { | ||
return super.font | ||
} | ||
} | ||
|
||
private func placeholderSizeToFit() { | ||
self.placeholderLabel.frame = CGRectMake(PLACEHOLDER_LEFT_MARGIN, PLACEHOLDER_TOP_MARGIN, self.frame.width - PLACEHOLDER_LEFT_MARGIN * 2, 0.0) | ||
self.placeholderLabel.sizeToFit() | ||
} | ||
|
||
private func setup() { | ||
self.contentInset = UIEdgeInsetsMake(0, 0, 0, 0); | ||
self.font = UIFont.systemFontOfSize(12.0) | ||
|
||
self.placeholderLabel.lineBreakMode = NSLineBreakMode.ByWordWrapping | ||
self.placeholderLabel.numberOfLines = 0 | ||
self.placeholderLabel.font = self.font | ||
self.placeholderLabel.backgroundColor = UIColor.clearColor() | ||
self.placeholderLabel.alpha = 1.0 | ||
self.placeholderLabel.tag = 999 | ||
|
||
self.placeholderLabel.textColor = self.placeholderColor | ||
self.placeholderLabel.text = self.placeholder | ||
self.placeholderSizeToFit() | ||
self.addSubview(placeholderLabel) | ||
|
||
self.sendSubviewToBack(placeholderLabel) | ||
|
||
let center = NSNotificationCenter.defaultCenter() | ||
center.addObserver(self, selector: "textChanged:", name: UITextViewTextDidChangeNotification, object: nil) | ||
|
||
self.textChanged(nil) | ||
} | ||
|
||
required init(coder aDecoder: NSCoder) { | ||
super.init(coder: aDecoder) | ||
self.setup() | ||
} | ||
|
||
override init(frame: CGRect, textContainer: NSTextContainer?) { | ||
super.init(frame: frame, textContainer: textContainer) | ||
self.setup() | ||
} | ||
|
||
convenience init() { | ||
self.init(frame: CGRectZero, textContainer: nil) | ||
} | ||
|
||
convenience init(frame: CGRect) { | ||
self.init(frame: frame, textContainer: nil) | ||
} | ||
|
||
deinit { | ||
NSNotificationCenter.defaultCenter().removeObserver(self) | ||
} | ||
|
||
override func awakeFromNib() { | ||
super.awakeFromNib() | ||
|
||
self.setup() | ||
} | ||
|
||
func textChanged(notification:NSNotification?) { | ||
self.viewWithTag(999)?.alpha = self.text.isEmpty ? 1.0 : 0.0 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,14 @@ | ||
# PlaceholderTextView | ||
## Requirements | ||
|
||
- Swift 1.2+ | ||
[![CI Status](http://img.shields.io/travis/CHEEBOW/PlaceholderTextView.svg?style=flat)](https://travis-ci.org/CHEEBOW/PlaceholderTextView) | ||
[![Version](https://img.shields.io/cocoapods/v/PlaceholderTextView.svg?style=flat)](http://cocoapods.org/pods/PlaceholderTextView) | ||
[![License](https://img.shields.io/cocoapods/l/PlaceholderTextView.svg?style=flat)](http://cocoapods.org/pods/PlaceholderTextView) | ||
[![Platform](https://img.shields.io/cocoapods/p/PlaceholderTextView.svg?style=flat)](http://cocoapods.org/pods/PlaceholderTextView) | ||
|
||
## Usage | ||
|
||
To run the example project, clone the repo, and run `pod install` from the Example directory first. | ||
|
||
```swift | ||
let placeholderTextView = PlaceholderTextView() | ||
let frame = CGRectMake(0.0, 20.0, 320.0, 100.0) | ||
|
@@ -13,3 +17,24 @@ placeholderTextView.placeholder = "Placeholder Text" | |
placeholderTextView.placeholderColor = UIColor.lightGrayColor() | ||
self.view.addSubview(placeholderTextView) | ||
``` | ||
|
||
## Requirements | ||
|
||
- Swift 1.2+ | ||
|
||
## Installation | ||
|
||
PlaceholderTextView is available through [CocoaPods](http://cocoapods.org). To install | ||
it, simply add the following line to your Podfile: | ||
|
||
```ruby | ||
pod "PlaceholderTextView" | ||
``` | ||
|
||
## Author | ||
|
||
CHEEBOW, [email protected] | ||
|
||
## License | ||
|
||
PlaceholderTextView is available under the MIT license. See the LICENSE file for more info. |