2013-10-19 70 views
1

我有计算UITextField中显示的NSString的准确大小的问题。计算NSString大小以调整UITextField帧

我的目标是以编程方式根据字符串大小更新文本框的大小(不使用sizeToFit)。我正在使用sizeWithFont函数。

-(void)resizeTextFieldAccordingToText:(NSString*)textFieldString { 

    CGPoint originalCenter = self.textField.center; 

    UIFont* currentFont = [textField font]; 
    CGSize newSize = [textFieldString sizeWithFont:currentFont]; 

    //Same incorrect results with the extended version of sizeWithFont, e.g. 
    //[textFieldString sizeWithFont:currentFont constrainedToSize:CGSizeMake(300.0, 100.0) lineBreakMode:NSLineBreakByClipping]; 

    [self.textField setFrame:(CGRectMake(self.textField.frame.origin.x, self.textField.frame.origin.y, newSize.width, newSize.height))]; 
    [self.textField setCenter:originalCenter]; 

} 

问题:尽管该返回正确的大小的结果在第一其变为通过将字符越来越unprecise因此终于开始夹住的字符串(如在右屏幕截图所示)。

enter image description here

如何获得文本域字符串正确地调整它的大小尺寸准确?

+0

可能是这个职位可能是有用于你的目的:http://stackoverflow.com/questions/1435544/measuring-the-pixel-width-of-a-string – RFG

+0

我知道这一点,但即使使用这个扩展版本的sizeWithFont(例如CGSize newSize = [ textFieldString sizeWithFont:currentFont constrainedToSize:CGSizeMake(300.0,100.0)lineBreakMode:NSLineBreakByClipping];)会返回相同的稍微不正确的大小。 – Bernd

+0

是的,因为它没有考虑填充 –

回答

1

问题是你没有考虑到UITextField的contentInset。你的代码对于不是textfield的标签可以。

例如:一种方法可能是:

CGPoint originalCenter = self.textField.center; 

UIFont* currentFont = [textField font]; 
CGSize oldSize = [self.textField.text sizeWithFont:currentFont]; 
CGSize newSize = [textFieldString sizeWithFont:currentFont]; 

CGRect finalFrame = self.textField.frame 
finalFrame.size.width -= oldSize.width; 
finalFrame.size.width += newSize.width; 
finalFrame.size.height -= oldSize.height; 
finalFrame.size.height += newSize.height; 
[self.textField setFrame:finalFrame]; 

[self.textField setCenter:originalCenter]; 

ios7不赞成sizeWithFont:currentFont所以它是sizeWithAttributes:@{NSFontAttributeName:currentFont}

+0

内联,未经测试 - 但原则应该变得清晰:尺寸不仅是字符串的大小 –

1

的UITextField有它自己的布局里面,如果你使用的borderStyle = UITextBorderStyleNone!在这种情况下,你必须增加一些常量的文字大小。

随着UITextBorderStyleNone你没有这个问题,下面的代码工作就像一个魅力(的iOS 7中引入新的方法来获取文本大小,-sizeWithFont:不推荐使用)

- (IBAction)textChanged:(UITextField *)field 
{ 
    UIFont *font = field.font; 
    NSString *string = field.text; 

    CGSize size = [string sizeWithAttributes: 
      [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName]]; 

    CGPoint center = field.center; 
    CGRect frame = field.frame; 
    frame.size = size; // or CGSizeMake(size.width + WIDTH_PADDING * 2, size.height + HEIGHT_PADDING * 2) 
    field.frame = frame; 
    field.center = center; 
} 
+0

不幸的是,问题依然存在。一个静态填充没有帮助。我需要调整每个字符串更改的填充,因为sizeWithFont的精度随着每个字符更改而改变。 – Bernd

+0

你使用哪种字体?我已经尝试了几种标准字体 - 没有剪裁,一切似乎都OK – LorikMalorik

+0

嗯。这令人费解。在我的例子中,我使用了非系统字体(font-family:“SourceSansPro-ExtraLight”; font-weight:normal; font-style:normal; font-size:38.00pt)。但是,当我使用默认字体(font-family:“.HelveticaNeueInterface-M3”; font-weight:normal; font-style:normal; font-size:17.00pt)时,我遇到了同样的问题。 – Bernd