关于使用MTLabel的设备旋转的麻烦
我必须使用MTLabel类的线间距的UILabel。
(引用的示例代码: https : //github.com/Tuszy/MTLabel )
但存在一些问题。
我正在制作一个iPad应用程序。 这个应用程序可以旋转 – 风景或肖像。
我把UILabel和MTLable对象放在视图中而没有IB。
每当设备的方向改变时,文本的宽度也改变了。
这个结果不是我想要的。
我的代码:
#import "MTLabel.h"
。
- (void)viewDidLoad { [super viewDidLoad]; MTLabel *TitleFont = [[MTLabel alloc] initWithFrame:CGRectMake(255, 60, 270, 60)]; [TitleFont setFont:[UIFont fontWithName:@"Arial" size:30.0]]; TitleFont.backgroundColor = [UIColor greenColor]; TitleFont.text = @"Happy! - 1"; TitleFont.autoresizingMask = UIViewAutoresizingFlexibleTopMargin& UIViewAutoresizingFlexibleLeftMargin&UIViewAutoresizingFlexibleRightMargin; TitleFont.autoresizingMask= UIViewAutoresizingFlexibleWidth; [self.view addSubview:TitleFont]; //---------------------- UILabel *TitleFont2 = [[UILabel alloc] initWithFrame:CGRectMake(255, 120, 270, 60)]; [TitleFont2 setFont:[UIFont fontWithName:@"Arial" size:30.0]]; TitleFont2.backgroundColor = [UIColor orangeColor]; TitleFont2.text = @"Happy! - 2"; TitleFont2.autoresizingMask = UIViewAutoresizingFlexibleTopMargin& UIViewAutoresizingFlexibleLeftMargin&UIViewAutoresizingFlexibleRightMargin; TitleFont2.autoresizingMask= UIViewAutoresizingFlexibleWidth; [self.view addSubview:TitleFont2]; }
结果:1)肖像图像:
2)风景图像:
如果我使用UILabel类,没问题! 但我不得不MTLabel类行间距的UILabel。
请帮助我…谢谢。
尝试这个:
[TitleFont setContentMode:UIViewContentModeRedraw];
您需要用setNeedsDisplay踢MTLabel才能正确地重新绘制新的尺寸。 可能在didRotateFromOrientation。
[TitleFont setNeedsDisplay]
试试这个,它适用于我。 并确定你应该禁用setAutoresizesSubviews:<NO>