由于UILable中的文字一般都比较短,所以都显示在了UILable的中间,我想把这些文字置顶对齐,就是说不出现在UILable的中间,而是在其上方,该怎么做?


2个回答

可以用sizeToFit实现:

myLabel.text = @"Some Text";
myLabel.numberOfLines = 0;
myLabel.frame = CGRectMake(20,20,200,800);
[myLabel sizeToFit];

    CGSize maximumSize = CGSizeMake(300, 9999);
    NSString *dateString = @"The date today is January 1st, 1999";
    UIFont *dateFont = [UIFont fontWithName:@"Helvetica" size:14];
    CGSize dateStringSize = [dateString sizeWithFont:dateFont 
        constrainedToSize:maximumSize 
        lineBreakMode:self.dateLabel.lineBreakMode];

    CGRect dateFrame = CGRectMake(10, 10, 300, dateStringSize.height);

    self.dateLabel.frame = dateFrame;

更多的代码你可以参考:http://discussions.apple.com/thread.jspa?threadID=1759957