标签:
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;
for(int i=0; i<newLinesToPad; i++)
self.text =[self.text stringByAppendingString:@"\n "];
// -- file: UILabel+VerticalAlign.h
#pragma mark VerticalAlign
@interfaceUILabel(VerticalAlign)
-(void)alignTop;
-(void)alignBottom;
@end
// -- file: UILabel+VerticalAlign.m
@implementationUILabel(VerticalAlign)
-(void)alignTop {
CGSize fontSize =[self.text sizeWithFont:self.font];
double finalHeight = fontSize.height *self.numberOfLines;
double finalWidth =self.frame.size.width;//expected width of label
CGSize theStringSize =[self.text sizeWithFont:self.font constrainedToSize:CGSizeMake(finalWidth, finalHeight)lineBreakMode:self.lineBreakMode];
int newLinesToPad =(finalHeight - theStringSize.height)/ fontSize.height;
for(int i=0; i<newLinesToPad; i++)
self.text =[self.text stringByAppendingString:@"\n "];
}
-(void)alignBottom {
CGSize fontSize =[self.text sizeWithFont:self.font];
double finalHeight = fontSize.height *self.numberOfLines;
double finalWidth =self.frame.size.width;//expected width of label
CGSize theStringSize =[self.text sizeWithFont:self.font constrainedToSize:CGSizeMake(finalWidth, finalHeight)lineBreakMode:self.lineBreakMode];
int newLinesToPad =(finalHeight - theStringSize.height)/ fontSize.height;
for(int i=0; i<newLinesToPad; i++)
self.text =[NSString stringWithFormat:@" \n%@",self.text];
}
@end
标签:
原文地址:http://www.cnblogs.com/yu0806/p/4279879.html