iOS自定義控制項 CustomTextView

2021-07-09 02:51:37 字數 3976 閱讀 6861

以前使用輸入框文字限制,基本上是打字過程中階段輸入,效果很不好,特別輸中文的過程中,好不容易用智慧型拼音打了一長串字,結果被自動擷取打斷了,變成了拼音,簡直是氣死寶寶了。後來想了個辦法,就是識別輸入框輸入中文,在使用者把一長串拼音確認轉換為文字之後再擷取。下面看效果:

效果(一),使用自動擷取

效果(二),不使用自動擷取

實現**:

textviewcustom.h

////  textviewcustom.h

//  pstextfiled

////  created by william sterling on 14/12/25.

//#import

typedef void (^textblocks)();

@inte***ce textviewcustom : uitextview

///編輯字型個數超過限制,是否使用自動擷取的方式,預設:no

@property (nonatomic,assign) bool interception;

///輸入字型個數最大限制個數,預設:10000

@property (nonatomic,assign) nsinteger textlength;

///預設提示lab

@property (nonatomic,strong) uilabel *placehlab;

///預設:[uicolor graycolor]

@property (nonatomic,strong) uicolor *placehtextcolor;

///預設:[uifont systemfontofsize:14.0]

@property (nonatomic,strong) uifont *placehfont;

///預設:@""

@property (nonatomic,strong) nsstring *placehtext;

///是否需要右下角文字計數顯示lab,預設:yes

@property (nonatomic,assign) bool promptlabhiden;

///右下角文字計數顯示lab

@property (nonatomic,strong) uilabel *promptlab;

///預設:[uicolor graycolor]

@property (nonatomic,strong) uicolor *prompttextcolor;

///預設:[uifont systemfontofsize:14.0];

@property (nonatomic,strong) uifont *promptfont;

///預設:self.backgroundcolor

@property (nonatomic,strong) uicolor *promptbackground;

///右下角,文字個數提示框_距父檢視右邊距_預設:10

@property (nonatomic,assign) cgfloat promptframemaxx;

///右下角,文字個數提示框_距父檢視底邊距_預設:10

@property (nonatomic,assign) cgfloat promptframemaxy;

///乙個詞語輸出監聽

@property (nonatomic,copy) textblocks editchangedblock;

@end

textviewcustom.m

////  textviewcustom.m

//  pstextfiled

////  created by william sterling on 14/12/25.

//#import "textviewcustom.h"

#define int_long_base(x) ((long)x)

#define int_ulong_base(x) ((unsigned long)x)

@inte***ce textviewcustom ()

@end

@implementation textviewcustom

@synthesize promptlab,placehlab;

- (id)initwithframe:(cgrect)frame

return self;

}-(void)dealloc

}- (void)viewdidload

}#pragma mark

- (void)settextlength:(nsinteger)textlength

#pragma mark

- (void)setplacehtextcolor:(uicolor *)placehtextcolor

- (void)setplacehfont:(uifont *)placehfont

- (void)setplacehtext:(nsstring *)placehtext

#pragma mark

- (void)setpromptframemaxx:(cgfloat)promptframemaxx

- (void)setpromptframemaxy:(cgfloat)promptframemaxy

- (void)setprompttextcolor:(uicolor *)prompttextcolor

- (void)setpromptfont:(uifont *)promptfont

- (void)setpromptbackground:(uicolor *)promptbackground

#pragma mark

- (void)textvieweditbegin:(nsnotification *)obj

}- (void)textvieweditend:(nsnotification *)obj

-(void)textvieweditchanged:(nsnotification *)objelse

nsstring *tobestring = self.text;

nsstring *primarylanguagestr = self.textinputmode.primarylanguage; // 鍵盤輸入模式

if ([primarylanguagestr isequaltostring:@"zh-hans"]) else

[self changepromptlab];

}else

}else

}elseelse

[self changepromptlab];

}else

}if (self.editchangedblock)

}- (void)changepromptlabelse

[promptlab sizetofit];

promptlab.frame = cgrectmake(cgrectgetmaxx(self.frame)-cgrectgetwidth(promptlab.frame)-_promptframemaxx, cgrectgetmaxy(self.frame)-cgrectgetheight(promptlab.frame)-_promptframemaxy, cgrectgetwidth(promptlab.frame), cgrectgetheight(promptlab.frame));}/*

// only override drawrect: if you perform custom drawing.

// an empty implementation adversely affects performance during animation.

- (void)drawrect:(cgrect)rect

*/@end

IOS 控制項 自定義UISlider Slider

在軟體中看到的滑塊都比較好看,在網上找了幾個例子,知道怎麼自定義uislider的樣式和滑塊 先建立uislider slider1 uislider alloc initwithframe cgrectmake 30,30,257,7 再設定顏色和數值範圍以及預設值的大小 slider1.back...

ios 自定義控制項 方法

initwithframe 前面已經見到,程式建立 ui控制項時常常會呼叫該方法執行初始化,因此,如果你需要對 ui控制項執行一些額外的初始化,即可通過重寫該方法來實現。initwithcoder 程式通過在 nib檔案中載入完該控制項後會自動呼叫該方法。因此,如果程式需要在 nib檔案中載入該控制...

iOS之自定義控制項

注意 如果使用 方式建立控制項,那麼建立時一定會呼叫initwithframe 方法 如果使用xib stotyboard方式建立控制項,那麼在建立時一定會呼叫initwithcoder 方法 在initwithcoder 裡面訪問屬性,會發現屬性為nil,因為此時自定義好控制項正在初始化,屬性還沒...