ios6 uilabel not center text aligned

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.


对于iOS 6或更高版本:

label.textAlignment = NSTextAlignmentLeft;
链接地址: http://www.djcxy.com/p/28272.html

上一篇: 在容器视图中均匀地分隔多个视图

下一篇: ios6 uilabel没有对齐中心文本