-
(void)setTextTop
{
NSMutableString *resultStr = [self.text mutableCopy];
NSArray *array = [resultStr componentsSeparatedByString:@"\n"];
NSInteger temp = 0;
NSInteger lines = self.lineBreakMode;
for (int j = 0; j < array.count; j++) {
NSString *str = array[j];if ([str isEqual:@""]) { temp --; } if (str.length > self.width - 40) { temp++; }
}
for (int i = 0; i < (lines - array.count - temp + 1); i++) {
[resultStr appendString:@"\n"];
}
self.text = resultStr;
}
以下两个没试过:
-
(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];
}
网友评论