2011-08-23 42 views
4

我正在創建一個水印來重疊在圖像視圖的頂部。圖像可以平移,縮放等。UILabel文本旋轉時會截斷。怎麼修?

我已經創建了一個水印視圖類,它的大小和覆蓋範圍一樣大的文本標籤會適合邊界。這個工程很好,尺寸適當,因爲圖像的大小等。

現在我想旋轉標籤。只要應用轉換,標籤中的文本就會截斷。我認爲我很接近,但我做了一些愚蠢的事情,因爲我是新手。有任何想法嗎?

這裏是我的類:

@interface WatermarkView() 
- (CGFloat)biggestBoldFontForString:(NSString*)text inRect:(CGRect)rect; 
@end 

@implementation WatermarkView 

- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) 
    { 
     // create a label that will display the watermark 
     mainLabel = [[UILabel alloc] initWithFrame:frame]; 
     mainLabel.alpha = 0.35; 
     mainLabel.backgroundColor = [UIColor clearColor]; 
     mainLabel.text = @"watermark"; 
     mainLabel.font = [UIFont boldSystemFontOfSize:[self biggestBoldFontForString:mainLabel.text inRect:mainLabel.frame]]; 
     mainLabel.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; 

     // rotate label to a random slanted angle 
     mainLabel.transform = CGAffineTransformMakeRotation(RANDOM_FLOAT(-M_PI/5, -M_PI/6)); 

     [self addSubview:mainLabel]; 
    } 
    return self; 
} 

- (void)dealloc 
{ 
    [mainLabel release]; 

    [super dealloc]; 
} 

- (CGFloat)biggestBoldFontForString:(NSString*)text inRect:(CGRect)rect 
{ 
    CGFloat actualFontSize = 200; 
    [text sizeWithFont:[UIFont boldSystemFontOfSize:200] minFontSize:1 actualFontSize:&actualFontSize forWidth:rect.size.width lineBreakMode:0]; 
    return actualFontSize; 
} 

- (void)layoutSubviews 
{ 
    [super layoutSubviews]; 

    mainLabel.font = [UIFont boldSystemFontOfSize:[self biggestBoldFontForString:mainLabel.text inRect:self.frame]]; 
} 

@end 
+0

你發現問題了嗎? – newenglander

+0

你說'現在我想旋轉標籤',這是用戶何時旋轉設備? – Patrick

回答

0

一個簡單的解決方案可能是簡單地創建第二個標籤,就像你的第一個,但適應旋轉視圖。保持隱藏或alpha = 0,然後旋轉隱藏初始textView並取消隱藏新文本。

另一個問題可能是您的框架太小,一旦旋轉,請儘量保持字體的大小和文本的中心對齊,但使TextView框架比您認爲需要的大得多。

0

旋轉變換後標籤截斷的原因是邊界縮小。解決方案是在旋轉之前存儲標籤的寬度,並在didRotateFromInterfaceOrientation之後重新分配。

例如

@synthesize labelWidth 

(void)viewDidLoad 
{ 
self.labelWidth = _label.bounds.size.width; 
_label.transform = CGAffineTransformMakeRotation(-1.3f); 
} 

-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation 
{ 
CGRect bounds = _label.bounds; 
bounds.size.width = self.labelWidth; 
_label.bounds = bounds; 
}