Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions AppTextviewPlaceholder.podspec
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@

Pod::Spec.new do |s|
s.name = 'AppTextviewPlaceholder'
s.version = '1.0'
s.version = '1.2'
s.swift_version = '4.2'
s.summary = 'A candy for Textview Placeholder, TL;DR Plug it, Get It, Use It.'
s.homepage = 'https://github.com/monicarajendran/AppTextviewPlaceholder'
s.license = { :type => 'MIT', :file => 'LICENSE' }
s.author = { 'monicarajendran' => '[email protected]' }
s.source = { :git => 'https://github.com/monicarajendran/AppTextviewPlaceholder.git', :tag => s.version.to_s }
s.social_media_url = 'https://twitter.com/monicarajendran'
s.swift_version = '4.0'
s.social_media_url = 'https://twitter.com/monica__raja'


s.ios.deployment_target = '10.0'
s.source_files = 'AppTextviewPlaceholder/Classes/**/*.{swift}'
Expand Down
19 changes: 9 additions & 10 deletions AppTextviewPlaceholder/Classes/PlaceholderTextView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ private let PlaceholderInset: CGFloat = 8

/** Initializes the placeholder text view, waiting for a notification of text changed */
func addObservers() {
NotificationCenter.default.addObserver(self, selector: #selector(AppPlaceholderTextView.textChangedForPlaceholderTextView(_:)), name:NSNotification.Name.UITextViewTextDidChange , object: self)
NotificationCenter.default.addObserver(self, selector: #selector(AppPlaceholderTextView.textChangedForPlaceholderTextView(_:)), name:NSNotification.Name.UITextViewTextDidBeginEditing , object: self)
NotificationCenter.default.addObserver(self, selector: #selector(AppPlaceholderTextView.textChangedForPlaceholderTextView(_:)), name:UITextView.textDidChangeNotification , object: self)
NotificationCenter.default.addObserver(self, selector: #selector(AppPlaceholderTextView.textChangedForPlaceholderTextView(_:)), name: UITextView.textDidBeginEditingNotification, object: self)
}

func removeObservers() {
Expand All @@ -91,15 +91,14 @@ private let PlaceholderInset: CGFloat = 8
if text.count == 0 && self.placeholder != nil {

let baseRect = placeholderBoundsContainedIn(self.bounds)

let font = self.font ?? self.typingAttributes[NSAttributedStringKey.font.rawValue] as? UIFont ?? UIFont.systemFont(ofSize: UIFont.systemFontSize)
let font = self.font ?? self.typingAttributes[NSAttributedString.Key.paragraphStyle] ?? UIFont.systemFont(ofSize: UIFont.systemFontSize)

self.placeholderColor.set()

// build the custom paragraph style for our placeholder text
var customParagraphStyle: NSMutableParagraphStyle!

if let defaultParagraphStyle = typingAttributes[NSAttributedStringKey.paragraphStyle.rawValue] as? NSParagraphStyle {
if let defaultParagraphStyle = typingAttributes[NSAttributedString.Key.paragraphStyle] as? NSParagraphStyle {
customParagraphStyle = defaultParagraphStyle.mutableCopy() as! NSMutableParagraphStyle
} else {
customParagraphStyle = NSMutableParagraphStyle.default.mutableCopy() as! NSMutableParagraphStyle
Expand All @@ -108,9 +107,9 @@ private let PlaceholderInset: CGFloat = 8
// set attributes
customParagraphStyle.lineBreakMode = NSLineBreakMode.byTruncatingTail
customParagraphStyle.alignment = self.textAlignment
let attributes = [NSAttributedStringKey.font: font,
NSAttributedStringKey.paragraphStyle: customParagraphStyle.copy() as! NSParagraphStyle,
NSAttributedStringKey.foregroundColor: self.placeholderColor]
let attributes = [NSAttributedString.Key.font: font,
NSAttributedString.Key.paragraphStyle: customParagraphStyle.copy() as! NSParagraphStyle,
NSAttributedString.Key.foregroundColor: self.placeholderColor]

// draw in rect.
self.placeholder?.draw(in: baseRect, withAttributes: attributes)
Expand All @@ -120,10 +119,10 @@ private let PlaceholderInset: CGFloat = 8
func placeholderBoundsContainedIn(_ containerBounds: CGRect) -> CGRect {

// get the base rect with content insets.
let baseRect = UIEdgeInsetsInsetRect(containerBounds, UIEdgeInsetsMake(PlaceholderInset, PlaceholderInset/2.0, 0, 0))
let baseRect = containerBounds.inset(by: UIEdgeInsets(top: PlaceholderInset, left: PlaceholderInset/2.0, bottom: 0, right: 0))

// adjust typing and selection attributes
if let paragraphStyle = typingAttributes[NSAttributedStringKey.paragraphStyle.rawValue] as? NSParagraphStyle {
if let paragraphStyle = typingAttributes[NSAttributedString.Key.paragraphStyle] as? NSParagraphStyle {
baseRect.offsetBy(dx: paragraphStyle.headIndent, dy: paragraphStyle.firstLineHeadIndent)
}

Expand Down
123 changes: 123 additions & 0 deletions Example/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
# Created by https://www.gitignore.io/api/swift,xcode,swiftpm,carthage,fastlane,cocoapods,appbuilder,swiftpackagemanager

### AppBuilder ###
.ab/

### Carthage ###
# Carthage
#
# Add this line if you want to avoid checking in source code from Carthage dependencies.
# Carthage/Checkouts

Carthage/Build

### CocoaPods ###
## CocoaPods GitIgnore Template

# CocoaPods - Only use to conserve bandwidth / Save time on Pushing
# - Also handy if you have a large number of dependant pods
# - AS PER https://guides.cocoapods.org/using/using-cocoapods.html NEVER IGNORE THE LOCK FILE
Pods/
!Podfile
!Podfile.lock

### fastlane ###
# fastlane - A streamlined workflow tool for Cocoa deployment
#
# It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the
# screenshots whenever they are needed.
# For more information about the recommended setup visit:
# https://docs.fastlane.tools/best-practices/source-control/#source-control

# fastlane specific
fastlane/report.xml

# deliver temporary files
fastlane/Preview.html

# snapshot generated screenshots
fastlane/screenshots/**/*.png
fastlane/screenshots/screenshots.html

# scan temporary files
fastlane/test_output
### Swift ###
# Xcode
#
# gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore

## Build generated
build/
DerivedData/

## Various settings
*.pbxuser
!default.pbxuser
*.mode1v3
!default.mode1v3
*.mode2v3
!default.mode2v3
*.perspectivev3
!default.perspectivev3
xcuserdata/

## Other
*.moved-aside
*.xccheckout
*.xcscmblueprint

## Obj-C/Swift specific
*.hmap
*.ipa
*.dSYM.zip
*.dSYM

## Playgrounds
timeline.xctimeline
playground.xcworkspace

# Swift Package Manager
#
# Add this line if you want to avoid checking in source code from Swift Package Manager dependencies.
# Packages/
# Package.pins
.build/

# CocoaPods - Refactored to standalone file

# Carthage - Refactored to standalone file

# fastlane
#
# It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the
# screenshots whenever they are needed.
# For more information about the recommended setup visit:
# https://docs.fastlane.tools/best-practices/source-control/#source-control

fastlane/screenshots

### SwiftPackageManager ###
Packages
xcuserdata

### SwiftPM ###

### Xcode ###
# Xcode
#
# gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore

## Build generated

## Various settings

## Other

### Xcode Patch ###
*.xcodeproj/*
!*.xcodeproj/project.pbxproj
!*.xcodeproj/xcshareddata/
!*.xcworkspace/contents.xcworkspacedata
/*.gcno

# End of https://www.gitignore.io/api/swift,xcode,swiftpm,carthage,fastlane,cocoapods,appbuilder,swiftpackagemanager
Loading