UITextField右框架

时间:2010-06-05 08:13:23

标签: iphone uitextfield frame

我有一段这样的代码:

CGSize lSize = [@"Hello World"  sizeWithFont:[UIFont fontWithName:@"Arial" size:13]];

现在,我希望有一个正确的框架,可以在UITextField中查看此文字。我不想更改字体大小,只需让UITextField的框架适合此文本。

我真的很感激任何帮助

1 个答案:

答案 0 :(得分:0)

#define MAIN_FONT_SIZE      17
#define MIN_MAIN_FONT_SIZE  11
#define MAX_TEXT_WIDTH      100

CGPoint point;
CGFloat actualFontSize;
CGSize size;

UIFont *mainFont = [UIFont systemFontOfSize:MAIN_FONT_SIZE];

NSString *string = @"A String Of Text";
size = [string sizeWithFont:mainFont 
             minFontSize:MIN_MAIN_FONT_SIZE 
             actualFontSize:&actualFontSize 
             forWidth:MAX_TEXT_WIDTH 
             lineBreakMode:UILineBreakModeTailTruncation];

从这里你可以调整textField的宽度:

[textField setFrameSize:NSMakeSize(size.width,textField.frame.size.height)];

或者您可以根据尺寸在计算点绘制文本:

point = CGPointMake(textXPosition, textYPosition);
[string drawAtPoint:point 
              forWidth:MAX_TEXT_WIDTH 
              withFont:mainFont 
              minFontSize:actualFontSize actualFontSize:&actualFontSize 
              lineBreakMode:UILineBreakModeTailTruncation 
              baselineAdjustment:UIBaselineAdjustmentAlignBaselines];