UILabel文本在旋转时会截断.怎么修?

too*_*fah 5 xcode cocoa-touch ios

我正在创建一个叠加在图像视图顶部的水印.图像可以平移,缩放等.

我创建了一个水印视图类,当调整大小时,会覆盖一个与边界一样大的文本标签.由于图像尺寸等,这种方法非常适用并且尺寸合适.

现在我想旋转标签.只要应用变换,标签中的文本就会截断.我认为我很接近,但我做的事情很愚蠢,因为我是新手.有任何想法吗?

这是我的班级:

@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
Run Code Online (Sandbox Code Playgroud)

Flu*_*ead 0

一个简单的解决方案可能是简单地创建第二个标签,就像第一个标签一样,但适应旋转视图。保持隐藏或 alpha=0,然后在旋转时隐藏初始 textView 并取消隐藏新 Text。

另一个问题可能是您的框架太小,一旦旋转,请尝试保持字体为您想要的大小并将文本居中对齐,但使 TextView 框架比您认为需要的大得多。