写法一:对象方法,传入:字体/最大尺寸. 即可得到宽高,
最大尺寸主要限制宽度,如果是一行就给个{MAXFLOAT,MAXFLOAT};如果是多行就限制X值,Y值随便给
- (CGSize)sizeWithFont:(UIFont *)font maxSize:(CGSize)maxSize { NSDictionary *attrs = @{NSFontAttributeName : font}; return [self boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size; }
写法二: 传入文字/字体/最大尺寸. 即可得到该文字宽高
-(CGSize)sizeWithText:(NSString *)text font:(UIFont *)font maxW:(CGFloat)maxW { NSMutableDictionary *attrs=[NSMutableDictionary dictionary]; attrs[NSFontAttributeName]=font; CGSize maxSize=CGSizeMake(maxW, MAXFLOAT); return [text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size; } - (CGSize)sizeWithText:(NSString *)text font:(UIFont *)font { return [self sizeWithText:text font:font maxW:MAXFLOAT]; }
自定义控件设置UILable的位置:
self.nameLabel.frame=CGRectMake(iconX, iconY, iconWH, iconWH);
或者:
CGFloat timeX = nameX; CGFloat timeY = CGRectGetMaxY(self.nameLabelF) + HWStatusCellBorderW; CGSize timeSize = [self sizeWithText:status.created_at font:HWStatusCellTimeFont]; self.timeLabelF = (CGRect){{timeX, timeY}, timeSize};
时间: 2024-10-24 19:16:21