UILabel文本超出界限一些时间

时间:2014-05-27 18:21:18

标签: objective-c uilabel

我一直在争论这一段时间 问题有时是阿拉伯字体和多行,文字刚刚离开UILabel框架,,我将添加图像更清晰...
UILabel有一个天蓝色的背景,箭头是为了显示错误及其应该是什么..

enter image description here

enter image description here

编辑: 这里是调整大小的代码

//myString contain the text I'm showing..

UIFont *font = [UIFont fontWithName:@"myArabicFont" size:30.0];
self.myLabel.text = myString;
self.myLabel.font = font;
NSDictionary *attributes = @{NSFontAttributeName:font};
CGRect rect = [myString boundingRectWithSize:CGSizeMake(265, MAXFLOAT)
                                options:NSStringDrawingUsesLineFragmentOrigin
                             attributes:attributes
                                context:nil];
CGRect frame = self.myLabel.frame;
frame.size.height = rect.size.height;
self.myLabel.frame = frame;

1 个答案:

答案 0 :(得分:1)

不要测量文本的边界矩形并将其分配给标签,而是让标签自行调整大小。使用自动布局来定位标签,设置其preferredMaxLayoutWidth,然后将文本填入标签。

另外,显然,换行模式也有所不同。

(另外,不要将属性字符串与标签的“原生”属性混合。我的经验是,当您这样做时,标签会变得相当混乱。例如,设置标签textfont,但是如果你要设置它的attributedText,那么也不要设置它的font。我无法从你给出的代码中看出你是否这样做,因为你没有'提供实际设置文本的代码。)