我想採用標準UILabel並增加字體大小以填充垂直空間。以靈感來自於接受的答案爲這個question,我定義上的UILabel子類此的drawRect實現:縮放字體大小以垂直放入UILabel
- (void)drawRect:(CGRect)rect
{
// Size required to render string
CGSize size = [self.text sizeWithFont:self.font];
// For current font point size, calculate points per pixel
float pointsPerPixel = size.height/self.font.pointSize;
// Scale up point size for the height of the label
float desiredPointSize = rect.size.height * pointsPerPixel;
// Create and assign new UIFont with desired point Size
UIFont *newFont = [UIFont fontWithName:self.font.fontName size:desiredPointSize];
self.font = newFont;
// Call super
[super drawRect:rect];
}
但因爲它縮放字體超出標籤的底部,這是行不通的。如果你想複製這個,我開始使用標籤289x122(寬x高),Helvetica作爲字體,起始點大小爲60,可以很好地貼在標籤上。下面是使用字符串「{汞柱」從標準的UILabel和我的子類輸出例如:
我已經看了字體伸伸和,試圖縮放考慮這些在不同的組合,但仍然沒有任何運氣。任何想法,這是不同的字體具有不同的下行和上行長度?
好吧......這是一個令人尷尬的簡單錯誤。感謝您的發現。 – user524261
我試着在'layoutSubviews'中實現它,但我不認爲它可以工作。我認爲原因是給UILabel子類分配一個新的字體會觸發另一個'layoutSubviews'的調用,這是有問題的。在我的實現中,我的一個應用程序隊列被鎖定。 – user444731