[英]how to draw and scale text in rectangle
我的問題很簡單。 我有一個矩形,例如(0.0,0.0,300,45),需要在這個矩形的中心畫一個字符串,例如“Text”。 “文本”的高度必須符合矩形的高度。
困難的部分是:矩形可以縮放。 “文本”的大小必須縮放為矩形的大小。
我可以在矩形的中心繪制字符串作為下面的編碼,但難點是我無法管理“文本”字體大小更改為矩形大小。
[@“Text”drawInRect:textRect withFont:font];
我想在矩形中繪制文本,而不是標簽,用戶可以用手指縮放矩形大小,最后,我將在圖像上按比例繪制文本,我認為標簽不適用於這些功能。
誰有好的解決方案?
謝謝!
更新:
實際上,我需要在大尺寸圖像上繪制文字,而不僅僅是在iPhone屏幕上顯示,請看下面的代碼:
UIImageVIew *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320, 320)];
imageView.image = [UIImage imageNamed:@"LargeSizeImage"]; // Image size = 2048 *2048
[self.view addSubview:imageView];
UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 300, 45)];
textLabel.text = @"Text";
[self.view addSubviews:textLabel];
// Draw text on image
UIGraphicsBeginImageCurrentContext(imageView.image.size);(2048 * 2048)
[imageView.image drawInRect:CGRectMake(0, 0, 2048, 2048)];
CGRect scaleRect = CGRectMake(10 * scaleFactor, 10 *scaleFactor, textLabel.bounds.size.width * scaleFactor, text.Label.bounds.size.height *scaleFactor);
UIFont *font = [UIFont systemOfSize:**?**];
[textLabel.text drawInRect:scaleRect withFont:font];
........... ........... ...........
我的問題是如何確定字體大小?
UILabel *text = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 300, 45)];
text.backgroundColor = [UIColor clearColor];
text.font = [UIFont systemFontOfSize:45];
text.textAlignment = UITextAlignmentCenter;
text.text = @"TEXT";
[self.view addSubview:text];
可能會得到你感興趣的效果。
編輯
實際上,如果調整方塊大小,您可以動態調整文本大小。 在您執行調整大小的方法中,只需為UILabel添加這些代碼行以觸發調整大小。
text.frame = CGRectMake(0.0, 0.0, rect.frame.size.width, rect.frame.size.height);
text.font = [UIFont systemFontOfSize:rect.frame.size.height];
rect是我用來描述你正在調整大小的矩形。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.