Viewcontroller.m
#import "JYZTextView.h"
#define Ktextbordercolor RGBCOLOR (227,224,216)
#undef RGBCOLOR
#define RGBCOLOR (r,g,b) [Uicolor colorwithred:r/255.0 green:g/255.0 blue:b/255.0 alpha:1]
@interface Viewcontroller () <UITextViewDelegate>
@property (Strong, Nonatomic) Jyztextview * TextView;
@property (Strong, Nonatomic) UILabel * Numlabel;
@end
@implementation Viewcontroller
-(void) Viewdidload {
[Super Viewdidload];
Custom TextView
_textview = [[Jyztextview alloc]initwithframe:cgrectmake (+, self.view.frame.size.width-60, 100)];
_textview.backgroundcolor = [Uicolor Whitecolor];
_textview.delegate = self;
_textview.font = [Uifont systemfontofsize:14.f];
_textview.textcolor = [Uicolor blackcolor];
_textview.textalignment = Nstextalignmentleft;
_textview.placeholder = @ "Please enter a rating greater than 10 less than 30,000 words";
[Self.view Addsubview:_textview];
_numlabel = [[UILabel alloc] Initwithframe:cgrectmake (Cgrectgetmaxx (_textview.frame) -90, Cgrectgetmaxy (_ Textview.frame) +6, 80, 21)];
_numlabel.textalignment = Nstextalignmentright;
_numlabel.text = @ "300";
_numlabel.backgroundcolor = [Uicolor Whitecolor];
[Self.view Addsubview:_numlabel];
}
#pragma mark TextField's word limit
Calculate the number of words entered in this place
-(void) Textviewdidchange: (Uitextview *) TextView
{
Nsinteger wordCount = textView.text.length;
Self.numLabel.text = [NSString stringwithformat:@ "%ld/300", (long) wordCount];
[Self wordlimit:textview];
}
#pragma mark cannot enter more than 300 words
-(BOOL) Wordlimit: (Uitextview *) text{
if (Text.text.length < 300) {
NSLog (@ "%ld", text.text.length);
self.textView.editable = YES;
}
else{
self.textView.editable = NO;
}
return nil;
}
JYZTextView.h
@interface Jyztextview:uitextview
@property (nonatomic, Strong) UILabel * Placeholderlabel;
@property (nonatomic, copy) NSString * placeholder;
@property (nonatomic, Strong) Uicolor * PLACEHOLDERCOLOR;
/**
* Detect changes in Font color when typing
*
* @param notification Monitoring
*/
-(void) textChanged: (nsnotification *) notification;
jyztextview.m
-(Instancetype) initWithFrame: (CGRect) frame{
if (self = [Super Initwithframe:frame]) {
[Self setplaceholder:@ "];
[Self Setplaceholdercolor:[uicolor lightgraycolor]];
Self.layer.cornerRadius = 4.0f;
Self.layer.borderColor = Ktextbordercolor.cgcolor;
Self.layer.borderWidth = 0.5;
Self.placeholdercolor = RGBCOLOR (0x89, 0x89, 0x89);
self.editable = YES;
[[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (textChanged:) Name: Uitextviewtextdidchangenotification Object:nil];
}
return self;
}
-(void) Setplaceholder: (NSString *) placeholder{
if (_placeholder! = placeholder) {
_placeholder = placeholder;
[Self.placeholderlabel Removefromsuperview];
Self.placeholderlabel = nil;
[Self setneedsdisplay];
}
}
-(void) textChanged: (nsnotification *) notification{
if ([[Self placeholder] length] = = 0) {
Return
}
if ([[Self text] length] = = 0) {
[[Self viewwithtag:999] setalpha:1.0];
}
else{
[[Self viewwithtag:999] setalpha:0];
}
}
-(void) DrawRect: (CGRect) rect{
[Super Drawrect:rect];
if ([[Self placeholder] length] > 0) {
if (_placeholderlabel = = nil) {
_placeholderlabel = [[UILabel alloc]initwithframe:cgrectmake (8, 8, self.bounds.size.width-16, 0)];
_placeholderlabel.linebreakmode = nslinebreakbywordwrapping;
_placeholderlabel.numberoflines = 0;
_placeholderlabel.font = Self.font;
_placeholderlabel.backgroundcolor = [Uicolor Clearcolor];
_placeholderlabel.textcolor = Self.placeholdercolor;
_placeholderlabel.alpha = 0;
_placeholderlabel.tag = 999;
[Self Addsubview:_placeholderlabel];
}
_placeholderlabel.text = Self.placeholder;
[_placeholderlabel SizeToFit];
[Self Sendsubviewtoback:_placeholderlabel];
}
if ([self text] length] = = 0 && [[self placeholder] length] >0) {
[[Self viewwithtag:999] setalpha:1.0];
}
}
Custom TextView Limit Words