热更新demo

UILabel_ VerticalAlign.m 1.8KB

    // // UILabel_ VerticalAlign.m // ThePaperBase // // Created by scar1900 on 15/11/16. // Copyright © 2015年 scar1900. All rights reserved. // #import "UILabel_ VerticalAlign.h" @implementation UILabel (VerticalAlign) - (void)alignTop { CGSize fontSize = [self.text sizeWithAttributes:@{NSFontAttributeName:self.font}]; double finalHeight = fontSize.height * self.numberOfLines; double finalWidth = self.frame.size.width; //expected width of label CGSize theStringSize = [self.text boundingRectWithSize:CGSizeMake(finalWidth, finalHeight) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:self.font} context:nil].size; 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 sizeWithAttributes:@{NSFontAttributeName:self.font}]; double finalHeight = fontSize.height * self.numberOfLines; double finalWidth = self.frame.size.width; //expected width of label CGSize theStringSize = [self.text boundingRectWithSize:CGSizeMake(finalWidth, finalHeight) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:self.font} context:nil].size; int newLinesToPad = (finalHeight - theStringSize.height) / fontSize.height; for(int i=0; i<newLinesToPad; i++) self.text = [NSString stringWithFormat:@" \n%@",self.text]; } @end