关于使用MTLabel进行设备旋转的问题
我必须使用 MTLabel 类来设置 UILabel 的行距。
(参考示例代码:https://github.com/Tuszy/MTLabel)
但是存在一些问题。
我正在制作一个 iPad 应用程序。该应用程序能够旋转 - 横向或纵向。
我在没有 IB 的情况下将 UILabel 和 MTLable 对象放在视图中。
每当设备的方向改变时,文本的宽度也会改变。
这个结果不是我想要的。
我的代码:
#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 的行距。
请帮助我...谢谢。
I have to use MTLabel Class for line-spacing of UILabel.
( refered sample code : https://github.com/Tuszy/MTLabel )
But Some problems exists.
I'm making a iPad App. This app is able to rotate - landscape or portrait.
I put UILabel and MTLable objects on view without IB.
Whenever orientation of device is changed, width of text also changed.
This result is not what I want.
My code :
#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];
}
Result :
1) portrait image :
2) landscape image :
If I use UILabel class, no problem!
But I have to MTLabel class for line-spacing of UILabel.
Please help me... Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
试试这个:
try this:
您需要使用 setNeedsDisplay 启动 MTLabel,以便以新尺寸正确重绘自身。可能在 didRotateFromOrientation 中。
You'll need to kick MTLabel with setNeedsDisplay to redraw itself properly in the new size. Probably in didRotateFromOrientation.
试试这个,它对我有用。
并且确保您应该禁用
setAutoresizesSubviews:
try this ,it works for me.
and sure you should disable
setAutoresizesSubviews:<NO>