??? 上周遇到一個(gè)小的技術(shù)點(diǎn),產(chǎn)品設(shè)計(jì)如下竭宰,要求:點(diǎn)擊輸入的時(shí)候前面的標(biāo)題不消失屡拨,但是占位符部分的文字需要消失。而且顯示的文字內(nèi)容以及顯示的順序都是取決于網(wǎng)絡(luò)請(qǐng)求返回?cái)?shù)據(jù)娃胆。
??? 大體思路是放了幾張白底陰影邊框的圖片拉伸一下遍希,作為背景,上面幾行單行的輸入行好處理里烦,前面標(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í)候是這樣的:
??? 我做的處理是:圖片上放兩個(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;
? ? }
}