Có thể nhận kích thước phông chữ cuối cùng sau khi tự động điều chỉnh không? (thuộc tính adjustsFontSizeToFitWidth được đặt thành YES và kích thước phông chữ văn bản đang được thu nhỏ để vừa với nhãn)Làm thế nào để có được kích thước phông chữ tự động điều chỉnh UILabel (UITextView)?
Tôi đang phân lớp drawTextInRect trong UILabel để đặt gradient trên văn bản, nhưng kích thước gradient cần phải giống nhau, như kích thước của phông chữ. Tôi không thể có được kích thước thích hợp của phông chữ được điều chỉnh ... Thậm chí có thể không?
//draw gradient
CGContextSaveGState(myContext);
CGGradientRef glossGradient;
CGColorSpaceRef rgbColorspace;
size_t num_locations = 2;
CGFloat locations[2] = { 0.0, 1.0 };
CGFloat components[8] = { 1, 1, 1, 0.25, // BOTTOM color
1, 1, 1, 0.12 }; // UPPER color
//scale and translate so that text would not be rotated 180 deg wrong
CGContextTranslateCTM(myContext, 0, rect.size.height);
CGContextScaleCTM(myContext, 1.0, -1.0);
//create mask
CGImageRef alphaMask = CGBitmapContextCreateImage(myContext);
CGContextClipToMask(myContext, rect, alphaMask);
rgbColorspace = CGColorSpaceCreateDeviceRGB();
glossGradient = CGGradientCreateWithColorComponents(rgbColorspace, components, locations, num_locations);
//gradient should be sized to actual font size. THIS IS THE PROBLEM - EVEN IF FONT IS AUTO ADUJSTED, I AM GETTING THE SAME ORIGINAL FONT SIZE!!!
CGFloat fontCapHeightHalf = (self.font.capHeight/2)+5;
CGRect currentBounds = rect;
CGPoint topCenter = CGPointMake(CGRectGetMidX(currentBounds), CGRectGetMidY(currentBounds)-fontCapHeightHalf);
CGPoint midCenter = CGPointMake(CGRectGetMidX(currentBounds), CGRectGetMidY(currentBounds)+fontCapHeightHalf);
CGContextDrawLinearGradient(myContext, glossGradient, topCenter, midCenter, 0);
CGGradientRelease(glossGradient);
CGColorSpaceRelease(rgbColorspace);
CGContextRestoreGState(myContext);
thể trùng lặp của [Làm thế nào để tính toán kích thước của một UIFont khi văn bản trong nhãn bị hạn chế xuống để phù hợp với chiều rộng?] (Http: // stackoverflow.com/questions/1385338/how-to-calculate-the-size-of-a-uifont-when-text-in-label-is-constrained-down-to-f) –
có, cảm ơn bạn đã chỉ cho tôi Đúng hướng. Tôi không tìm thấy nó trước khi đăng câu hỏi của tôi. –