2014-01-06 178 views
1

我想拉伸UILabel中的文本,以便如果完全符合標籤(寬度和高度)。我不想以任何方式調整UILabel的大小。如何根據標籤大小調整文本大小?

到目前爲止,我使用這個:How to render stretched text in iOS?,但文本不能伸展100%(有時它超出了邊界,有時它留下邊距的間距)。

是否有另一種(最好更容易)的方法來做到這一點?我想說的是:http://i.imgur.com/AMvfhsA.png。我在左側獲得間距,文本超出右側和底部邊界的邊界。

這是自定義標籤類:

#import "CustomUILabel.h" 

@implementation CustomUILabel 

- (id)initWithFrame:(CGRect)frame text:(NSString*)text 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     self.edgeInsets = UIEdgeInsetsMake(0, 0, 0, 0); 
     self.text = text; 
    } 
    return self; 
} 

- (void)drawTextInRect:(CGRect)rect { 
    [super drawTextInRect:UIEdgeInsetsInsetRect(rect, self.edgeInsets)]; 
} 

- (void)drawRect:(CGRect)rect 
{ 
    [self drawScaledString:self.text]; 
} 

- (void)drawScaledString:(NSString *)string 
{ 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextSetTextMatrix(context, CGAffineTransformIdentity); 

    NSAttributedString *attrString = [self generateAttributedString:string]; 

    CFAttributedStringSetAttribute((CFMutableAttributedStringRef)attrString, CFRangeMake(0, string.length), 
            kCTForegroundColorAttributeName, self.textColor.CGColor); 

    CTLineRef line = CTLineCreateWithAttributedString((CFAttributedStringRef) attrString); 

    // CTLineGetTypographicBounds doesn't give correct values, 
    // using GetImageBounds instead 
    CGRect imageBounds = CTLineGetImageBounds(line, context); 
    CGFloat width = imageBounds.size.width; 
    CGFloat height = imageBounds.size.height; 

    CGFloat padding = 0; 

    width += padding; 
    height += padding; 

    float sx = self.bounds.size.width/width; 
    float sy = self.bounds.size.height/height; 

    CGContextSetTextMatrix(context, CGAffineTransformIdentity); 

    CGContextTranslateCTM(context, 1, self.bounds.size.height); 
    CGContextScaleCTM(context, 1, -1); 
    CGContextScaleCTM(context, sx, sy); 

    CGContextSetTextPosition(context, -imageBounds.origin.x + padding/2, -imageBounds.origin.y + padding/2); 

    CTLineDraw(line, context); 
    CFRelease(line); 
} 

- (NSAttributedString *)generateAttributedString:(NSString *)string 
{ 

    CTFontRef helv = CTFontCreateWithName(CFSTR("Helvetica-Bold"),20, NULL); 
    CGColorRef color = [UIColor blackColor].CGColor; 

    NSDictionary *attributesDict = [NSDictionary dictionaryWithObjectsAndKeys: 
            (__bridge id)helv, (NSString *)kCTFontAttributeName, 
            color, (NSString *)kCTForegroundColorAttributeName, 
            nil]; 

    NSAttributedString *attrString = [[NSMutableAttributedString alloc] 
             initWithString:string 
             attributes:attributesDict]; 

    return attrString; 
} 

@end 

這也是我如何使用它(我已經添加了從故事板標籤):

@property (weak, nonatomic) IBOutlet CustomUILabel *label; 

... 

self.label.backgroundColor = [UIColor redColor]; 
self.label.text = @"OOOOO"; 
+0

你的意思是你想要真正拉伸文本或你想要適應字體大小取決於標籤大小? – Vik

+0

另一個問題在理論上是合理的,所以它表明你有一些計算錯誤。顯示您針對指定輸入顯示的問題的代碼和屏幕截圖。 – Wain

+0

我真的很想拉伸文本。 – meee

回答

0

你問歸結來計算渲染文本的確切邊界框。當你有這個盒子時,你可以調整CTM使文本填滿所需的區域。

還有:似乎沒有一個簡單的方法來做到這一點。許多問題導致此問題:

  1. 字體指標是一個相當複雜的主題。呈現的字符(字形)具有多個取決於意圖的邊界框。
  2. 在字體中,字形通常使用貝塞爾曲線表示,這使得計算精確的邊界框變得困難。
  3. 屬性可能會以不可預見的方式影響圖形外觀。 AppKit/UIKit例如知道可以擴展字體渲染像素的區域的陰影屬性。

還有更多的問題,但我列出的可能足以表明正是填充盒與一個渲染文本的任務也不是那麼容易。

也許還有另一種方式來做你的想法。

+0

謝謝。我希望也許有一個解決方案,但我開始相信,沒有解決方案:)(至少不是我能弄明白的)。我會嘗試找到另一種解決方案。 – meee

相關問題