textView光標(biāo)起始位置后移動(dòng)

??? 上周遇到一個(gè)小的技術(shù)點(diǎn),產(chǎn)品設(shè)計(jì)如下竭宰,要求:點(diǎn)擊輸入的時(shí)候前面的標(biāo)題不消失屡拨,但是占位符部分的文字需要消失。而且顯示的文字內(nèi)容以及顯示的順序都是取決于網(wǎng)絡(luò)請(qǐng)求返回?cái)?shù)據(jù)娃胆。

產(chǎn)品圖片01

??? 大體思路是放了幾張白底陰影邊框的圖片拉伸一下遍希,作為背景,上面幾行單行的輸入行好處理里烦,前面標(biāo)題用label凿蒜,后面放個(gè)textField,就可以了招驴,代碼如下:

- (void)addtextField:(UITextField **)tField frame:(CGRect)frame andPlaceHolder:(NSString *)placeholder andFontSize:(CGFloat)fontSize andTextColor:(UIColor *)textColor inView:(UIView *)superView appendPlaceHolder:(NSString *)appendPlaceHolder

{

? ? UIImage *image = [[UIImage imageNamed:@"NTalkerUIKitResource.bundle/ntalker_leaveMsgA.png"] stretchableImageWithLeftCapWidth:30 topCapHeight:30];

? ? UIImageView *backgroundImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, frame.origin.y, frame.size.width, frame.size.height + 5)];

? ? backgroundImageView.image = image;

? ? [self.view addSubview:backgroundImageView];

? ? //titleLabel

? ? UILabel *titleLabel = [[UILabel alloc] init];

? ? titleLabel.backgroundColor = [UIColor clearColor];

? ? titleLabel.textColor = [UIColor blackColor];

? ? titleLabel.text = placeholder;

? ? titleLabel.font = [UIFont systemFontOfSize:14*autoSizeScaleY];

? ? CGSize titleTextSize = [titleLabel.text sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:titleLabel.font, NSFontAttributeName,nil]];

? ? CGFloat titleLabelContentWidth = titleTextSize.width;

? ? if (titleLabelContentWidth >= 150.0) {

? ? ? ? titleLabelContentWidth = 150;

? ? }

? ? titleLabel.frame = CGRectMake(0,0, titleLabelContentWidth, frame.size.height);

? ? [backgroundImageView addSubview:titleLabel];

? ? //輸入框

? ? UITextField *textField = [[UITextField alloc] initWithFrame:frame];

? ? UIView *zeroViewName = [[UIView alloc] initWithFrame:CGRectMake(0,0,8+titleLabel.frame.size.width,CGRectGetHeight(frame))];

? ? textField.leftView = zeroViewName;

? ? textField.leftViewMode = UITextFieldViewModeAlways;

? ? textField.delegate = self;

? ? if (appendPlaceHolder && appendPlaceHolder.length > 0) {

? ? ? ? NSMutableAttributedString *behindPlaceHolder = [[NSMutableAttributedString alloc] initWithString:appendPlaceHolder];

? ? ? ? [behindPlaceHolder addAttributes:@{NSForegroundColorAttributeName : [UIColor lightGrayColor],

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? NSFontAttributeName : [UIFont systemFontOfSize:fontSize - 1.0]}

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? range:NSMakeRange(0, behindPlaceHolder.length)];

? ? ? ? textField.attributedPlaceholder = behindPlaceHolder;

? ? }

??? textField.font = [UIFont systemFontOfSize:fontSize];

? ? textField.textColor = textColor;

? ? textField.returnKeyType = UIReturnKeyDone;

? ? [superView addSubview:textField];

? ? textField.layer.borderColor = [UIColor clearColor].CGColor;

? ? if (tField) {

? ? ? ? *tField = textField;

? ? }

??? 最下面的留言輸入是多行篙程,因此選擇用textView,但是有個(gè)問題,如何讓第一行起始輸入從標(biāo)題的后面開始别厘,且標(biāo)題不消失,而且placeHold文字自動(dòng)消失拥诡。輸入的時(shí)候是這樣的:


產(chǎn)品圖片02

??? 我做的處理是:圖片上放兩個(gè)label,一個(gè)顯示title,一個(gè)顯示placeHold文字触趴,當(dāng)開始輸入的時(shí)候textView做首行縮進(jìn)處理氮发,剛好縮進(jìn)的距離是title寬度的距離,同事將placeHold那個(gè)label隱藏掉冗懦。代碼如下:

- (void)configureTextView:(CGRect)frame placeHolder:(NSString *)placeHolder isRequired:(NSString *)isRequired

{

? ? UIImage *image = [[UIImage imageNamed:@"NTalkerUIKitResource.bundle/ntalker_leaveMsgB.png"] stretchableImageWithLeftCapWidth:30 topCapHeight:30];

? ? UIImageView *backgroundImageView = [[UIImageView alloc] initWithFrame:frame];

? ? backgroundImageView.image = image;

? ? [self.view addSubview:backgroundImageView];

??? CGRect textFrame = CGRectMake(frame.origin.x + 3.5, frame.origin.y, CGRectGetWidth(frame) - 7, CGRectGetHeight(frame) - 5);

? ? //設(shè)置TitleLabel

? ? UILabel *titleLabel = [[UILabel alloc] init];

? ? titleLabel.backgroundColor = [UIColor clearColor];

? ? titleLabel.textColor = [UIColor blackColor];

? ? titleLabel.text = placeHolder;

? ? titleLabel.tag = 2106;

? ? titleLabel.font = [UIFont systemFontOfSize:14*autoSizeScaleY];

? ? CGSize titleTextSize = [titleLabel.text sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:titleLabel.font, NSFontAttributeName,nil]];

? ? CGFloat titleLabelContentWidth = titleTextSize.width;

? ? if (titleLabelContentWidth >= 150.0) {

? ? ? ? titleLabelContentWidth = 150;

? ? }

? ? titleLabel.frame = CGRectMake(frame.origin.x,frame.origin.y+3.5,titleLabelContentWidth,14*autoSizeScaleY+7.0);

? ? [self.view addSubview:titleLabel];

? ? self.startLocation = titleLabelContentWidth+2.5;

? ? self.titleLabel = titleLabel;

? ? //占位提示

? ? UILabel *placeHoldLabel = [[UILabel alloc] init];

? ? placeHoldLabel.backgroundColor = [UIColor clearColor];

? ? placeHoldLabel.textColor = [UIColor lightGrayColor];

? ? NSString * behindPlaceHolder = [NSString stringWithFormat:@" %@",NSLocalizedStringFromTable(@"PlaceholderForLeaveMessage", @"XNLocalizable", nil)];

? ? if (isRequired && [isRequired isEqualToString:@"1"]) {

? ? ? ? placeHoldLabel.text = behindPlaceHolder?:@"";

? ? }

? ? placeHoldLabel.tag = 2107;

? ? placeHoldLabel.font = [UIFont systemFontOfSize:13*autoSizeScaleY];

? ? CGSize placeHoldTextSize = [placeHoldLabel.text sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:placeHoldLabel.font, NSFontAttributeName,nil]];

? ? CGFloat placeHoldLabelContentWidth = placeHoldTextSize.width;

? ? CGFloat placeHoldMaxContentWidth = frame.size.width -CGRectGetMaxX(titleLabel.frame)-3.5;

? ? if (placeHoldLabelContentWidth >= placeHoldMaxContentWidth) {

? ? ? ? placeHoldLabelContentWidth = placeHoldMaxContentWidth;

? ? }

? ? placeHoldLabel.frame = CGRectMake(CGRectGetMaxX(titleLabel.frame)+3.5,frame.origin.y+3.5,placeHoldLabelContentWidth,titleLabel.frame.size.height);

? ? self.placeHoldLabel = placeHoldLabel;

? ? [self.view addSubview:placeHoldLabel];

??? // 添加留言輸入框

? ? UITextView *textView = [[UITextView alloc] initWithFrame:textFrame];

? ? textView.backgroundColor = [UIColor clearColor];

? ? textView.delegate = self;

? ? textView.tag = 2101;

? ? textView.returnKeyType = UIReturnKeyDone;

? ? textView.textColor = [UIColor blackColor];

? ? textView.font = [UIFont systemFontOfSize:14 * autoSizeScaleY];

? ? textView.layer.borderColor = [UIColor clearColor].CGColor;

? ? [self.view addSubview:textView];

}

#pragma mark ==================UITextViewDelegate==============

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView

{

? ? if (textView.tag != 2101) {

? ? ? ? return NO;

? ? }

//不加下面這行爽冕,首次彈起鍵盤時(shí)候,光標(biāo)會(huì)跑到最前面披蕉,開始輸入才首行縮進(jìn)颈畸,加上就可以解決這個(gè)問題了

? ? if (textView.tag==2101&&!textView.text.length) {

? ? ? ? textView.text = @" ";

? ? }

? ? //首行縮進(jìn)

? ? NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];

? ? paragraphStyle.lineSpacing = 3;? ? //行間距

? ? CGFloat titleWidth = self.titleLabel.frame.size.width-2.5;

? ? paragraphStyle.firstLineHeadIndent = titleWidth; /**首行縮進(jìn)寬度*/

? ? paragraphStyle.alignment = NSTextAlignmentJustified;

? ? NSDictionary *attributes = @{

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? NSFontAttributeName:[UIFont systemFontOfSize:13*autoSizeScaleY],

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? NSParagraphStyleAttributeName:paragraphStyle

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? };

? ? textView.attributedText = [[NSAttributedString alloc] initWithString:textView.text attributes:attributes];

? return YES;

}

- (void)textViewDidChange:(UITextView *)textView

{

? ? #pragma? 文本框默認(rèn)文字

? ? if (textView.tag == 2101&&textView.text.length == 0) {

? ? ? ? self.placeHoldLabel.hidden = NO;

? ? }else{

? ? ? ? self.placeHoldLabel.hidden = YES;

? ? }

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市没讲,隨后出現(xiàn)的幾起案子眯娱,更是在濱河造成了極大的恐慌,老刑警劉巖爬凑,帶你破解...
    沈念sama閱讀 223,002評(píng)論 6 519
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件徙缴,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡嘁信,警方通過查閱死者的電腦和手機(jī)于样,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,357評(píng)論 3 400
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來潘靖,“玉大人穿剖,你說我怎么就攤上這事∝砸纾” “怎么了糊余?”我有些...
    開封第一講書人閱讀 169,787評(píng)論 0 365
  • 文/不壞的土叔 我叫張陵,是天一觀的道長既绕。 經(jīng)常有香客問我啄刹,道長,這世上最難降的妖魔是什么凄贩? 我笑而不...
    開封第一講書人閱讀 60,237評(píng)論 1 300
  • 正文 為了忘掉前任誓军,我火速辦了婚禮,結(jié)果婚禮上疲扎,老公的妹妹穿的比我還像新娘昵时。我一直安慰自己,他們只是感情好椒丧,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,237評(píng)論 6 398
  • 文/花漫 我一把揭開白布壹甥。 她就那樣靜靜地躺著,像睡著了一般壶熏。 火紅的嫁衣襯著肌膚如雪句柠。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,821評(píng)論 1 314
  • 那天,我揣著相機(jī)與錄音溯职,去河邊找鬼精盅。 笑死,一個(gè)胖子當(dāng)著我的面吹牛谜酒,可吹牛的內(nèi)容都是我干的叹俏。 我是一名探鬼主播,決...
    沈念sama閱讀 41,236評(píng)論 3 424
  • 文/蒼蘭香墨 我猛地睜開眼僻族,長吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼粘驰!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起述么,我...
    開封第一講書人閱讀 40,196評(píng)論 0 277
  • 序言:老撾萬榮一對(duì)情侶失蹤蝌数,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后碉输,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體籽前,經(jīng)...
    沈念sama閱讀 46,716評(píng)論 1 320
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,794評(píng)論 3 343
  • 正文 我和宋清朗相戀三年敷钾,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了枝哄。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,928評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡阻荒,死狀恐怖挠锥,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情侨赡,我是刑警寧澤蓖租,帶...
    沈念sama閱讀 36,583評(píng)論 5 351
  • 正文 年R本政府宣布,位于F島的核電站羊壹,受9級(jí)特大地震影響蓖宦,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜油猫,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,264評(píng)論 3 336
  • 文/蒙蒙 一稠茂、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧情妖,春花似錦睬关、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,755評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至料睛,卻和暖如春丐箩,著一層夾襖步出監(jiān)牢的瞬間摇邦,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,869評(píng)論 1 274
  • 我被黑心中介騙來泰國打工雏蛮, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留涎嚼,地道東北人阱州。 一個(gè)月前我還...
    沈念sama閱讀 49,378評(píng)論 3 379
  • 正文 我出身青樓挑秉,卻偏偏與公主長得像,于是被迫代替她去往敵國和親苔货。 傳聞我的和親對(duì)象是個(gè)殘疾皇子犀概,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,937評(píng)論 2 361

推薦閱讀更多精彩內(nèi)容

  • // // JackDateAndDateView.m // ZHB // // Created by JackR...
    JackRen閱讀 414評(píng)論 0 1
  • iOS開發(fā)系列--網(wǎng)絡(luò)開發(fā) 概覽 大部分應(yīng)用程序都或多或少會(huì)牽扯到網(wǎng)絡(luò)開發(fā),例如說新浪微博夜惭、微信等姻灶,這些應(yīng)用本身可...
    lichengjin閱讀 3,674評(píng)論 2 7
  • 蕭瑟深秋已過,白雪落在荒年诈茧。 我心獨(dú)戚戚产喉,千里寄寒衣。盼吾君郎早日還敢会,猶是當(dāng)年模樣曾沈。 庭院始新綠,花開只待時(shí)鸥昏。 江...
    顏尋歡閱讀 244評(píng)論 2 5
  • 路很長塞俱,慢慢走,遇到的一切吏垮,都接受障涯!
    蝴蝶夢(mèng)魚閱讀 195評(píng)論 0 0
  • 嗯,我拿起畫本就想拿起畫筆膳汪,就算只是畫個(gè)裸體少年唯蝶,來啊~造作啊~ (初中畫的,有不足的歡迎指出)
    坎哥啊閱讀 253評(píng)論 0 1