2014-11-05 28 views

回答

0

您需要爲每個文本字段設置高度限制。

而在編輯做結束的方法,你需要通過標籤來識別文本字段,而不是改變適當的文本字段的常量。

for ex。 heightTxt1,heightTxt2,heightTxt3,heightTxt4是約束條件。

比變化,而編輯也結束呼籲文本框1

heightTxt1.constant= (count height on the basis of text and font style,size); 

這應該解決您的問題,

0

第1步:設置沒有線在你的標籤爲0

enter image description here

第2步:添加此方法在類文件

//-- Dynamic label frame depend on text 
-(CGRect)getLabelHeightForIndex:(UILabel *)label label_string:(NSString *)label_string 
{ 
    CGSize maxSize = CGSizeMake(label.frame.size.width, 999); 
    CGRect contentFrame; 
    CGSize contentStringSize; 
    NSString *version = [[UIDevice currentDevice] systemVersion]; 
    NSString *contentStr = label_string; 

    if ([version floatValue] < 7.0) 
    { 
     contentStringSize = [contentStr sizeWithFont:label.font constrainedToSize:maxSize lineBreakMode:label.lineBreakMode]; 
    } 
    else 
    { 
     NSDictionary *contentDic = [NSDictionary dictionaryWithObjectsAndKeys:label.font, NSFontAttributeName, nil]; 
     contentStringSize = [contentStr boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:contentDic context:nil].size; 
    } 
    contentFrame = CGRectMake(label.frame.origin.x, label.frame.origin.y, label.frame.size.width, contentStringSize.height); 
    return contentFrame; 
} 

第3步:訪問動態幀標籤使用下面的代碼

//-- Get dynamic label height from given string 
    CGRect contentFrame = [self getLabelHeightForIndex:Label label_string:Label.text]; 
    Label.frame = contentFrame; 
    CGFloat LabelHeight = contentFrame.size.height; 
+0

這是'UITextField' – 2014-11-05 05:43:13

+0

沒問題。只需在該方法中將uilabel替換爲uitextfield即可。 – svmrajesh 2014-11-05 05:48:21

0

首先,你將要存儲的高度限制在一個屬性,特定UITextField

例如:@property (nonatomic, weak) IBOutlet NSLayoutConstraint *specificTextFieldHeightConstraint;

然後,實施-[UITextFieldDelegate textFieldDidBeginEditing:]UIViewController子類中:

- (void)textFieldDidBeginEditing:(UITextField *)textField { 
    specificTextFieldHeightConstraint.constant = expandedHeightGoesHere; 
} 

最後,你可能希望通過實施-[UITextFieldDelegate textFieldDidEndEditing:]UIViewController子類中,返回的高度恢復正常:

- (void)textFieldDidEndEditing:(UITextField *)textField { 
    specificTextFieldHeightConstraint.constant = originalHeightConstant; 
} 

如果在所有UITextFields之間設置了相等的高度約束,則在編輯時需要將與specificTextField相關的等高限制的優先級設置爲低,並在編輯結束時將其設置回高。在specificTextField上仍然需要高度約束,但specificTextField未編輯時,它的優先級應低於等高度約束,並且在specificTextField正在編輯時,它的優先級應高於等高度約束。

+0

OP詢問如何根據文本**擴展文本字段**。這雖然回答了一半的問題。 – 2014-11-05 05:44:08

相關問題