ios6 uilabel不是中心文本对齐 [英] ios6 uilabel not center text aligned

查看:99
本文介绍了ios6 uilabel不是中心文本对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究ios应用程序最低ios 5,我刚刚与uilabels发生了一个奇怪的问题。或者也许是我遗漏了一些明显无论如何,我遇到的问题是我有一个uilabel,它将以文本对齐为中心。所有在ios 5上工作正常但在ios 6上它总是左对齐。我发现使用uilabel文本对齐的旧方法已经弃用,并且设置它应该可以工作。

  self.topComment。 textAlignment = NSTextAlignmentCenter; 

但即使这样,它仍然只在ios 5上居中对齐并在ios 6上保持对齐。
我确实有一些代码可以调整标签中文本的字体大小,以使其适合最小和最大尺寸。

  UIFont * font = self.topComment.font; 

for(int i = maxFont; i> minFont; i--)
{
//设置新的字体大小。
font = [font fontWithSize:i];
CGSize constraintSize = CGSizeMake(self.topComment.frame.size.width,1000);
CGSize labelSize = [topString sizeWithFont:font constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
if(labelSize.height< = self.topComment.frame.size.height){
fits = YES;
休息;
}
//self.topComment.text = topString;

}


self.topComment.font = font;
self.topComment.text = topString;

所以这是我对标签做的唯一事情,但它始终在ios 6中保持对齐重要的是要注意,如果我放入带有文本和中心对齐的uilabel并且不使用上面的代码,那么它将以ios 5和6为中心。

好吧,看起来好像打开Tighten Letter Spacing在ios 5和6上有不同的结果。老实说,我无法解释为什么差异,但只是关闭它给我所需的标签中心。只要将其留在这里作为答案,以防其他人犯同样的愚蠢错误。



例如:

  lbl.adjustsLetterSpacingToFitWidth = NO; 

注意这在iOS 7.0中已弃用。


I'm working on a ios app minimum ios 5 and i have just hit a weird issue with uilabels. or maybe im missing something obvious. anyway the problem im having is that i have a uilabel which is to be centered text aligned. all works fine on ios 5 but on ios 6 it is always is left aligned. I seen that the old way of doing uilabel text align has deprecated and that setting it as so should work.

self.topComment.textAlignment = NSTextAlignmentCenter;

But no even this way it still only is center aligned on ios 5 and left aligned on ios 6. I do have some code that resizes the font of the text in the label to try make it fit with a min and max size.

UIFont *font = self.topComment.font;

for(int i = maxFont; i > minFont; i--)
{
    // Set the new font size.
    font = [font fontWithSize:i];
    CGSize constraintSize = CGSizeMake(self.topComment.frame.size.width, 1000);
    CGSize labelSize = [topString sizeWithFont:font constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
    if(labelSize.height <= self.topComment.frame.size.height ) {
        fits = YES;
        break;
    }
    //self.topComment.text = topString;

}


self.topComment.font = font;
self.topComment.text = topString;

So that is the only thing i am doing to the label but it always is left aligned in ios 6. Important to note that if i drop in a uilabel with text and center align it and dont use the above code then it is centered on both ios 5 and 6.

解决方案

Ok looks like turning on Tighten Letter Spacing has a different result on ios 5 and 6. I honestly cant explain why the difference but just turning this off gives the desired centering of labels for me. Just leaving this here as an answer in case anyone else makes the same silly mistake.

For example:

    lbl.adjustsLetterSpacingToFitWidth = NO;

Note this is deprecated in iOS 7.0.

这篇关于ios6 uilabel不是中心文本对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆