UITextView中的占位符

我的应用程序使用UITextView..现在我要UITextView控件的占位符与可以设置的占位符类似。UITextField.

怎么做?


慕森卡
浏览 754回答 3
3回答

神不在的星期二

我对BCD的解决方案做了一些小修改,以便允许从Xib文件,文字包装,并保持背景色。希望它能省去其他人的麻烦。UIPlaceHolderTextView.h:#import&nbsp;<Foundation/Foundation.h>IB_DESIGNABLE@interface&nbsp;UIPlaceHolderTextView&nbsp;:&nbsp; UITextView@property&nbsp;(nonatomic,&nbsp;retain)&nbsp;IBInspectable&nbsp;NSString&nbsp;*placeholder; @property&nbsp;(nonatomic,&nbsp;retain)&nbsp;IBInspectable&nbsp;UIColor&nbsp;*placeholderColor;-(void)textChanged:(NSNotification*)notification;@endUIPlaceHolderTextView.m:#import&nbsp;"UIPlaceHolderTextView.h"@interface&nbsp;UIPlaceHolderTextView&nbsp;()@property&nbsp;(nonatomic,&nbsp;retain)&nbsp;UILabel&nbsp;*placeHolderLabel; @end@implementation&nbsp;UIPlaceHolderTextViewCGFloat&nbsp;const&nbsp;UI_PLACEHOLDER_TEXT_CHANGED_ANIMATION_DURATION&nbsp;=&nbsp;0.25;-&nbsp;(void)dealloc{ &nbsp;&nbsp;&nbsp;&nbsp;[[NSNotificationCenter&nbsp;defaultCenter]&nbsp;removeObserver:self];#if&nbsp;__has_feature(objc_arc)#else &nbsp;&nbsp;&nbsp;&nbsp;[_placeHolderLabel&nbsp;release];&nbsp;_placeHolderLabel&nbsp;=&nbsp;nil; &nbsp;&nbsp;&nbsp;&nbsp;[_placeholderColor&nbsp;release];&nbsp;_placeholderColor&nbsp;=&nbsp;nil; &nbsp;&nbsp;&nbsp;&nbsp;[_placeholder&nbsp;release];&nbsp;_placeholder&nbsp;=&nbsp;nil; &nbsp;&nbsp;&nbsp;&nbsp;[super&nbsp;dealloc];#endif}-&nbsp;(void)awakeFromNib{ &nbsp;&nbsp;&nbsp;&nbsp;[super&nbsp;awakeFromNib];&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;Use&nbsp;Interface&nbsp;Builder&nbsp;User&nbsp;Defined&nbsp;Runtime&nbsp;Attributes&nbsp;to&nbsp;set&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;placeholder&nbsp;and&nbsp;placeholderColor&nbsp;in&nbsp;Interface&nbsp;Builder. &nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(!self.placeholder)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[self&nbsp;setPlaceholder:@""]; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(!self.placeholderColor)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[self&nbsp;setPlaceholderColor:[UIColor&nbsp;lightGrayColor]]; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;[[NSNotificationCenter&nbsp;defaultCenter]&nbsp;addObserver:self&nbsp;selector:@selector(textChanged:)&nbsp;name: &nbsp;&nbsp;&nbsp;&nbsp;UITextViewTextDidChangeNotification&nbsp;object:nil];}-&nbsp;(id)initWithFrame:(CGRect)frame{ &nbsp;&nbsp;&nbsp;&nbsp;if(&nbsp;(self&nbsp;=&nbsp;[super&nbsp;initWithFrame:frame])&nbsp;) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[self&nbsp;setPlaceholder:@""]; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[self&nbsp;setPlaceholderColor:[UIColor&nbsp;lightGrayColor]]; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[[NSNotificationCenter&nbsp;defaultCenter]&nbsp;addObserver:self&nbsp;selector:@selector(textChanged:)&nbsp;name: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;UITextViewTextDidChangeNotification&nbsp;object:nil]; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;self;}-&nbsp;(void)textChanged:(NSNotification&nbsp;*)notification{ &nbsp;&nbsp;&nbsp;&nbsp;if([[self&nbsp;placeholder]&nbsp;length]&nbsp;==&nbsp;0) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;[UIView&nbsp;animateWithDuration:UI_PLACEHOLDER_TEXT_CHANGED_ANIMATION_DURATION&nbsp;animations:^{ &nbsp;&nbsp;&nbsp;&nbsp;if([[self&nbsp;text]&nbsp;length]&nbsp;==&nbsp;0) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[[self&nbsp;viewWithTag:999]&nbsp;setAlpha:1]; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;else &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[[self&nbsp;viewWithTag:999]&nbsp;setAlpha:0]; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;}];}-&nbsp;(void)setText:(NSString&nbsp;*)text&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;[super&nbsp;setText:text]; &nbsp;&nbsp;&nbsp;&nbsp;[self&nbsp;textChanged:nil];}-&nbsp;(void)drawRect:(CGRect)rect{ &nbsp;&nbsp;&nbsp;&nbsp;if(&nbsp;[[self&nbsp;placeholder]&nbsp;length]&nbsp;>&nbsp;0&nbsp;) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(_placeHolderLabel&nbsp;==&nbsp;nil&nbsp;) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_placeHolderLabel&nbsp;=&nbsp;[[UILabel&nbsp;alloc]&nbsp;initWithFrame:CGRectMake(8,8,self.bounds.size.width&nbsp;-&nbsp;16,0)]; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_placeHolderLabel.lineBreakMode&nbsp;=&nbsp;NSLineBreakByWordWrapping; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_placeHolderLabel.numberOfLines&nbsp;=&nbsp;0; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_placeHolderLabel.font&nbsp;=&nbsp;self.font; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_placeHolderLabel.backgroundColor&nbsp;=&nbsp;[UIColor&nbsp;clearColor]; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_placeHolderLabel.textColor&nbsp;=&nbsp;self.placeholderColor; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_placeHolderLabel.alpha&nbsp;=&nbsp;0; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_placeHolderLabel.tag&nbsp;=&nbsp;999; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[self&nbsp;addSubview:_placeHolderLabel]; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_placeHolderLabel.text&nbsp;=&nbsp;self.placeholder; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[_placeHolderLabel&nbsp;sizeToFit]; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[self&nbsp;sendSubviewToBack:_placeHolderLabel]; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;if(&nbsp;[[self&nbsp;text]&nbsp;length]&nbsp;==&nbsp;0&nbsp;&&&nbsp;[[self&nbsp;placeholder]&nbsp;length]&nbsp;>&nbsp;0&nbsp;) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[[self&nbsp;viewWithTag:999]&nbsp;setAlpha:1]; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;[super&nbsp;drawRect:rect];}@end

SMILET

简单的方法,只需在UITextView通过使用以下方法UITextViewDelegate方法:-&nbsp;(void)textViewDidBeginEditing:(UITextView&nbsp;*)textView{ &nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;([textView.text&nbsp;isEqualToString:@"placeholder&nbsp;text&nbsp;here..."])&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;textView.text&nbsp;=&nbsp;@""; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;textView.textColor&nbsp;=&nbsp;[UIColor&nbsp;blackColor];&nbsp;//optional &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;[textView&nbsp;becomeFirstResponder];}-&nbsp;(void)textViewDidEndEditing:(UITextView&nbsp;*)textView{ &nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;([textView.text&nbsp;isEqualToString:@""])&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;textView.text&nbsp;=&nbsp;@"placeholder&nbsp;text&nbsp;here..."; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;textView.textColor&nbsp;=&nbsp;[UIColor&nbsp;lightGrayColor];&nbsp;//optional &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;[textView&nbsp;resignFirstResponder];}只要记住myUITextView与创作的确切文本。UITextView&nbsp;*myUITextView&nbsp;=&nbsp;[[UITextView&nbsp;alloc]&nbsp;init];myUITextView.delegate&nbsp;=&nbsp;self;myUITextView.text&nbsp;=&nbsp;@"placeholder&nbsp;text&nbsp;here..."; myUITextView.textColor&nbsp;=&nbsp;[UIColor&nbsp;lightGrayColor];&nbsp;//optional并使父类成为UITextViewDelegate在包括这些方法之前。@interface&nbsp;MyClass&nbsp;()&nbsp;<UITextViewDelegate>@endSWIFT 3.1代码func&nbsp;textViewDidBeginEditing(_&nbsp;textView:&nbsp;UITextView)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(textView.text&nbsp;==&nbsp;"placeholder&nbsp;text&nbsp;here..."&nbsp;&&&nbsp;textView.textColor&nbsp;==&nbsp;.lightGray) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;textView.text&nbsp;=&nbsp;"" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;textView.textColor&nbsp;=&nbsp;.black&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;textView.becomeFirstResponder()&nbsp;//Optional}func&nbsp;textViewDidEndEditing(_&nbsp;textView:&nbsp;UITextView){ &nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(textView.text&nbsp;==&nbsp;"") &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;textView.text&nbsp;=&nbsp;"placeholder&nbsp;text&nbsp;here..." &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;textView.textColor&nbsp;=&nbsp;.lightGray&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;textView.resignFirstResponder()}只要记住myUITextView与创作的确切文本。&nbsp;let&nbsp;myUITextView&nbsp;=&nbsp;UITextView.init() &nbsp;myUITextView.delegate&nbsp;=&nbsp;self &nbsp;myUITextView.text&nbsp;=&nbsp;"placeholder&nbsp;text&nbsp;here..." &nbsp;myUITextView.textColor&nbsp;=&nbsp;.lightGray并使父类成为UITextViewDelegate在包括这些方法之前。class&nbsp;MyClass:&nbsp;UITextViewDelegate{}

阿晨1998

我对发布的任何解决方案都不太满意,因为它们有点重。向视图添加视图并不理想(特别是在drawRect:)。他们都有泄密,这也是不可接受的。这是我的解决方案:SAMTextViewSAMTextView.h////&nbsp;&nbsp;SAMTextView.h//&nbsp;&nbsp;SAMTextView////&nbsp;&nbsp;Created&nbsp;by&nbsp;Sam&nbsp;Soffes&nbsp;on&nbsp;8/18/10.//&nbsp;&nbsp;Copyright&nbsp;2010-2013&nbsp;Sam&nbsp;Soffes.&nbsp;All&nbsp;rights&nbsp;reserved. //#import&nbsp;<UIKit/UIKit.h>/** &nbsp;UITextView&nbsp;subclass&nbsp;that&nbsp;adds&nbsp;placeholder&nbsp;support&nbsp;like&nbsp;UITextField&nbsp;has. &nbsp;*/@interface&nbsp;SAMTextView&nbsp;:&nbsp;UITextView/** &nbsp;The&nbsp;string&nbsp;that&nbsp;is&nbsp;displayed&nbsp;when&nbsp;there&nbsp;is&nbsp;no&nbsp;other&nbsp;text&nbsp;in&nbsp;the&nbsp;text&nbsp;view. &nbsp;The&nbsp;default&nbsp;value&nbsp;is&nbsp;`nil`. &nbsp;*/@property&nbsp;(nonatomic,&nbsp;strong)&nbsp;NSString&nbsp;*placeholder;/** &nbsp;The&nbsp;color&nbsp;of&nbsp;the&nbsp;placeholder. &nbsp;The&nbsp;default&nbsp;is&nbsp;`[UIColor&nbsp;lightGrayColor]`. &nbsp;*/@property&nbsp;(nonatomic,&nbsp;strong)&nbsp;UIColor&nbsp;*placeholderTextColor;/** &nbsp;Returns&nbsp;the&nbsp;drawing&nbsp;rectangle&nbsp;for&nbsp;the&nbsp;text&nbsp;views’s&nbsp;placeholder&nbsp;text. &nbsp;@param&nbsp;bounds&nbsp;The&nbsp;bounding&nbsp;rectangle&nbsp;of&nbsp;the&nbsp;receiver. &nbsp;@return&nbsp;The&nbsp;computed&nbsp;drawing&nbsp;rectangle&nbsp;for&nbsp;the&nbsp;placeholder&nbsp;text. &nbsp;*/-&nbsp;(CGRect)placeholderRectForBounds:(CGRect)bounds;@endSAMTextView.m////&nbsp;&nbsp;SAMTextView.m//&nbsp;&nbsp;SAMTextView////&nbsp;&nbsp;Created&nbsp;by&nbsp;Sam&nbsp;Soffes&nbsp;on&nbsp;8/18/10.//&nbsp;&nbsp; Copyright&nbsp;2010-2013&nbsp;Sam&nbsp;Soffes.&nbsp;All&nbsp;rights&nbsp;reserved.//#import&nbsp;"SAMTextView.h" @implementation&nbsp;SAMTextView#pragma&nbsp;mark&nbsp;-&nbsp;Accessors@synthesize&nbsp;placeholder&nbsp;=&nbsp;_placeholder;@synthesize&nbsp;placeholderTextColor&nbsp;= &nbsp;_placeholderTextColor;-&nbsp;(void)setText:(NSString&nbsp;*)string&nbsp;{ &nbsp;&nbsp;[super&nbsp;setText:string]; &nbsp;&nbsp;[self&nbsp;setNeedsDisplay];}-&nbsp;(void)insertText:(NSString&nbsp;*)string&nbsp;{ &nbsp;&nbsp;[super&nbsp;insertText:string]; &nbsp;&nbsp;[self&nbsp;setNeedsDisplay];}-&nbsp;(void)setAttributedText:(NSAttributedString&nbsp;*)attributedText&nbsp;{ &nbsp;&nbsp;[super&nbsp;setAttributedText:attributedText]; &nbsp;&nbsp;[self&nbsp;setNeedsDisplay];}-&nbsp;(void)setPlaceholder:(NSString&nbsp;*)string&nbsp;{ &nbsp;&nbsp;if&nbsp;([string&nbsp;isEqual:_placeholder])&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;return; &nbsp;&nbsp;} &nbsp;&nbsp;_placeholder&nbsp;=&nbsp;string; &nbsp;&nbsp;[self&nbsp;setNeedsDisplay];}-&nbsp;(void)setContentInset:(UIEdgeInsets)contentInset&nbsp;{ &nbsp;&nbsp;[super&nbsp;setContentInset:contentInset]; &nbsp;&nbsp;[self&nbsp;setNeedsDisplay];}-&nbsp;(void)setFont:(UIFont&nbsp;*)font&nbsp;{ &nbsp;&nbsp;[super&nbsp;setFont:font]; &nbsp;&nbsp;[self&nbsp;setNeedsDisplay];}-&nbsp;(void)setTextAlignment:(NSTextAlignment)textAlignment&nbsp;{ &nbsp;&nbsp;[super&nbsp;setTextAlignment:textAlignment]; &nbsp;&nbsp;[self&nbsp;setNeedsDisplay];}#pragma&nbsp;mark&nbsp;-&nbsp;NSObject-&nbsp;(void)dealloc&nbsp;{ &nbsp;&nbsp;[[NSNotificationCenter&nbsp;defaultCenter]&nbsp;removeObserver:self&nbsp;name:UITextViewTextDidChangeNotification&nbsp;object:self];} &nbsp;&nbsp;#pragma&nbsp;mark&nbsp;-&nbsp;UIView-&nbsp;(id)initWithCoder:(NSCoder&nbsp;*)aDecoder&nbsp;{ &nbsp;&nbsp;if&nbsp;((self&nbsp;=&nbsp;[super&nbsp;initWithCoder:aDecoder]))&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;[self&nbsp;initialize]; &nbsp;&nbsp;} &nbsp;&nbsp;return&nbsp;self;}-&nbsp;(id)initWithFrame:(CGRect)frame&nbsp;{ &nbsp;&nbsp;if&nbsp;((self&nbsp;=&nbsp;[super&nbsp;initWithFrame:frame]))&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;[self&nbsp;initialize]; &nbsp;&nbsp;} &nbsp;&nbsp;return&nbsp;self;}-&nbsp;(void)drawRect:(CGRect)rect&nbsp;{ &nbsp;&nbsp;[super&nbsp;drawRect:rect]; &nbsp;&nbsp;if&nbsp;(self.text.length&nbsp;==&nbsp;0&nbsp;&&&nbsp;self.placeholder)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;rect&nbsp;=&nbsp;[self&nbsp;placeholderRectForBounds:self.bounds]; &nbsp;&nbsp;&nbsp;&nbsp;UIFont&nbsp;*font&nbsp;=&nbsp;self.font&nbsp;?&nbsp;self.font&nbsp;:&nbsp;self.typingAttributes[NSFontAttributeName]; &nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;Draw&nbsp;the&nbsp;text &nbsp;&nbsp;&nbsp;&nbsp;[self.placeholderTextColor&nbsp;set]; &nbsp;&nbsp;&nbsp;&nbsp;[self.placeholder&nbsp;drawInRect:rect&nbsp;withFont:font&nbsp;lineBreakMode:NSLineBreakByTruncatingTail&nbsp;alignment:self.textAlignment]; &nbsp;&nbsp;}}#pragma&nbsp;mark&nbsp;-&nbsp;Placeholder-&nbsp;(CGRect)placeholderRectForBounds:(CGRect)bounds&nbsp;{ &nbsp;&nbsp;//&nbsp;Inset&nbsp;the&nbsp;rect &nbsp;&nbsp;CGRect&nbsp;rect&nbsp;=&nbsp;UIEdgeInsetsInsetRect(bounds,&nbsp;self.contentInset); &nbsp;&nbsp;if&nbsp;(self.typingAttributes)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;NSParagraphStyle&nbsp;*style&nbsp;=&nbsp;self.typingAttributes[NSParagraphStyleAttributeName]; &nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(style)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rect.origin.x&nbsp;+=&nbsp;style.headIndent; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rect.origin.y&nbsp;+=&nbsp;style.firstLineHeadIndent; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;} &nbsp;&nbsp;return&nbsp;rect;}#pragma&nbsp;mark&nbsp;-&nbsp;Private-&nbsp;(void)initialize&nbsp;{ &nbsp;&nbsp;[[NSNotificationCenter&nbsp;defaultCenter]&nbsp;addObserver:self&nbsp;selector:@selector(textChanged:) &nbsp;&nbsp;&nbsp;name:UITextViewTextDidChangeNotification&nbsp;object:self]; &nbsp;&nbsp;self.placeholderTextColor&nbsp;=&nbsp;[UIColor&nbsp;colorWithWhite:0.702f&nbsp;alpha:1.0f];}-&nbsp;(void)textChanged:(NSNotification&nbsp;*)notification&nbsp;{ &nbsp;&nbsp;[self&nbsp;setNeedsDisplay];}@end它比其他的要简单得多,因为它不使用子视图(或者有泄漏)。可以随意使用。
打开App,查看更多内容
随时随地看视频慕课网APP