UILabel未在ios

时间:2015-09-15 12:47:57

标签: ios iphone uilabel

我遇到了一个问题。我已经创建了UILabel并根据内容更改了框架,即尺寸高度,所有iPhone设备都非常好。然而,如果我们只得到几个字,我的标签不是中心对齐的,比如阿迪达斯,耐克等。

brandtitlelabel = [[UILabel alloc] init];
brandtitlelabel.backgroundColor = [UIColor clearColor];
brandtitlelabel.textColor = [UIColor commonTextColor];
brandtitlelabel.numberOfLines = 0;
brandtitlelabel.adjustsFontSizeToFitWidth = YES;
brandtitlelabel.minimumScaleFactor = 0.1;
brandtitlelabel.textAlignment = NSTextAlignmentCenter;
brandtitlelabel.lineBreakMode = NSLineBreakByWordWrapping;
brandtitlelabel.font = [UIFont fontWithName:@"HelveticaNeue-Medium" size:30];

NSString *text = @"Adidas";
NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:text attributes:@{NSFontAttributeName:brandtitlelabel.font}];
rect = [attributedText boundingRectWithSize:(CGSize){self.view.frame.size.width, CGFLOAT_MAX} options:NSStringDrawingUsesLineFragmentOrigin context:nil];
rect.origin = CGPointMake(0, 66);
brandtitlelabel.frame = rect;
brandtitlelabel.text = text;
[self.view addSubview:brandtitlelabel];

2 个答案:

答案 0 :(得分:0)

在设置框架后,在结尾处添加以下代码行。

brandtitlelabel.textAlignment = NSTextAlignmentCenter;
brandtitlelabel.lineBreakMode = NSLineBreakByWordWrapping;

答案 1 :(得分:0)

试试这个。

brandtitlelabel.text = text;
brandtitlelabel.center = CGPointMake(self.view.center.x,brandtitlelabel.center.y);//replace an 'self.view' with your 'superver' on which you want to center.
[self.view addSubview:brandtitlelabel];