当前位置:文档之家› ios UITextView的placeHolder的设置

ios UITextView的placeHolder的设置

ios UITextView的placeHolder的设置
ios UITextView的placeHolder的设置

我在用UITextView的时候,默认是不能设置placeholder的,于是网上找了一些资料,最后找到了一个别人写的自定义类,拿来用用,顺便分享一下。

CPTextViewPlaceholder.m

//

// CPTextViewPlaceholder.m

// Cassius Pacheco

//

// Created by Cassius Pacheco on 30/01/13.

// Copyright (c) 2013 Cassius Pacheco. All rights reserved.

//

#import "CPTextViewPlaceholder.h"

@interface CPTextViewPlaceholder()

@property (nonatomic) UITextAutocorrectionType originalCorrection;

@property (nonatomic, strong) UIColor *placeholderColor;

@property (nonatomic, strong) UIColor *originalTextColor;

@property (nonatomic, getter = isUsingPlaceholder) BOOL usingPlaceholder; @property (nonatomic, getter = isSettingPlaceholder) BOOL settingPlaceholder;

@end

@implementation CPTextViewPlaceholder

#pragma mark -

#pragma mark Life Cycle method

- (id)initWithCoder:(NSCoder *)aDecoder

{

if (self = [super initWithCoder:aDecoder]) {

self.placeholderColor = [UIColor lightGrayColor];

self.originalCorrection = self.autocorrectionType;

self.originalTextColor = super.textColor;

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didBeginEdit ing) name:UITextViewTextDidBeginEditingNotification object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didEndEditin g) name:UITextViewTextDidEndEditingNotification object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChan ge:) name:UITextViewTextDidChangeNotification object:self];

}

return self;

}

- (void)dealloc

{

[[NSNotificationCenter defaultCenter] removeObserver:self name:UITextViewTextDidBeginE ditingNotification object:self];

[[NSNotificationCenter defaultCenter] removeObserver:self name:UITextViewTextDidEndEdi tingNotification object:self];

[[NSNotificationCenter defaultCenter] removeObserver:self name:UITextViewTextDidChang eNotification object:self];

}

- (void)layoutSubviews

{

[super layoutSubviews];

//Fixes iOS 5.x cursor when becomeFirstResponder

if ([UIDevice currentDevice].systemVersion.floatValue < 6.000000) { if (self.isUsingPlaceholder && self.isFirstResponder) {

self.text = @"";

}

}

}

#pragma mark -

#pragma mark Notifications

- (void)didBeginEditing

{

if (self.isUsingPlaceholder) {

[self sendCursorToBeginning];

}

}

- (void)didEndEditing

{

if (self.text.length == 0) {

[self setupPlaceholder];

}

}

- (void)textDidChange:(NSNotification *)notification

{

//self.text received the placeholder text by CPTex tViewPlaceholder if (self.isSettingPlaceholder) {

return;

}

if (self.text.length == 0) {

[self setupPlaceholder];

return;

}

if (self.isUsingPlaceholder) {

https://www.doczj.com/doc/0d4764755.html,ingPlaceholder = NO;

NSRange range = [self.text rangeOfString:self.placeholder options:NSLiteralSearch];

if (range.location != NSNotFound) {

NSString *newText = [self.text stringByReplacingCharactersInRange:range withStr ing:@""];

super.textColor = self.originalTextColor;

super.autocorrectionType = self.originalCorrection;

//User pasted a text equals to placeholder or setText was called

if ([newText isEqualToString:self.placeholder]) {

[self sendCursorToEnd];

//this is necessary for iOS 5.x

} else if (newText.length == 0) {

[self setupPlaceholder];

return;

}

self.text = newText;

}

}

}

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender

{

if (self.isUsingPlaceholder && action != @selector(paste:)) {

return NO;

}

return [super canPerformAction:action withSender:sender];

}

#pragma mark - Getters and Setters

- (void)setAutocorrectionType:(UITextAutocorrectionType)autocorrectionType {

[super setAutocorrectionType:autocorrectionType];

self.originalCorrection = autocorrectionType;

}

- (void)setPlaceholder:(NSString *)placeholder

{

_placeholder = placeholder;

if (self.isUsingPlaceholder || self.text.length == 0) { [self setupPlaceholder];

}

}

- (void)setTextColor:(UIColor *)textColor

{

[super setTextColor:textColor];

self.originalTextColor = textColor;

}

- (void)setSelectedRange:(NSRange)selectedRange

{

if (self.isUsingPlaceholder) {

[self sendCursorToBeginning];

} else {

[super setSelectedRange:selectedRange];

}

}

- (void)setSelectedTextRange:(UITextRange *)selectedTextRange {

if (self.isUsingPlaceholder) {

[self sendCursorToBeginning];

} else {

[super setSelectedTextRange:selectedTextRange];

}

}

#pragma mark -

#pragma mark Utilities methods

- (void)setupPlaceholder

{

super.autocorrectionType = UITextAutocorrectionTypeNo;

https://www.doczj.com/doc/0d4764755.html,ingPlaceholder = YES;

self.settingPlaceholder = YES;

self.text = self.placeholder;

self.settingPlaceholder = NO;

super.textColor = self.placeholderColor;

[self sendCursorToBeginning];

}

- (void)sendCursorToBeginning

{

[self performSelector:@selector(cursorToBeginning) withObject:nil afterDelay:0.01]; }

- (void)cursorToBeginning

{

super.selectedRange = NSMakeRange(0, 0);

}

- (void)sendCursorToEnd

{

[self performSelector:@selector(cursorToEnd) withObject:nil afterDelay:0.01]; }

- (void)cursorToEnd

{

super.selectedRange = NSMakeRange(self.text.length, 0);

}

@end

CPTextViewPlaceholder.h

//

// CPTextViewPlaceholder.h

// Cassius Pacheco

//

// Created by Cassius Pacheco on 30/01/13.

// Copyright (c) 2013 Cassius Pacheco. All rights reserved. //

#import

@interface CPTextViewPlaceholder : UITextView

@property (nonatomic, strong) NSString *placeholder;

@end

相关主题
文本预览
相关文档 最新文档