English 中文(简体)
页: 1 适当调整工作的方法
原标题:iOS. Method adjustsFontSizeToFitWidth doesn t work properly

我需要用一些案文来补充一下。 重新计算的规模是固定的。

我行使以下职能:

- (void)drawText:(NSString *)text onView:(UIView *)view inRect:(CGRect)rect
{
    UILabel *lbl = [[UILabel alloc] initWithFrame:rect];
    lbl.minimumFontSize = 1;
    lbl.font = [UIFont systemFontOfSize:30];
    [lbl setText:text];
    lbl.adjustsFontSizeToFitWidth = YES;
    [view addSubview:lbl];
    [lbl release];
}

接着,我写了类似的内容:

[self drawText:@"Asdfghjkl;" onView:view inRect:CGRectMake(45, 40, 85, 13)];
[self drawText:@"qwertyui" onView:view inRect:CGRectMake(173, 31, 126, 22)];

我拿到了截断的文本。 我的错误在哪里?

最佳回答

I think the main trouble is that some text fits width only but it must fit both width and height. So the fastest solution for me is to make a bigger transparent UILabel and put a text inside it using alignment

问题回答

正如加戈莱夫所说,它将根据适当的宽度调整 only/em>。

因此,你应选择合适的体格,以适应你再次提供的高度。

如果不事先知道你会再次使用这个词,那么简单的解决办法是:

UILabel *label = [[UILabel alloc] initWithFrame:myView.bounds];
label.font = [UIFont fontWithName:@"Helvetica" size:1.f];
float fontAspectFactor = 1.f/label.font.lineHeight;
label.font = [UIFont fontWithName:@"Helvetica" size:myView.height*fontAspectFactor];
[myView addSubview:label];




相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...