標籤:val 字串 system color max name drawing return ora
//擷取字串的寬度-(float)widthForString:(NSString *)value fontSize:(float)fontSize andHeight:(float)height{ UIColor *backgroundColor=[UIColor blackColor]; UIFont *font=[UIFont boldSystemFontOfSize:fontSize]; CGRect sizeToFit = [value boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, height) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{ NSForegroundColorAttributeName:backgroundColor, NSFontAttributeName:font } context:nil]; return sizeToFit.size.width;}//獲得字串的高度-(float) heightForString:(NSString *)value fontSize:(float)fontSize andWidth:(float)width{ UIColor *backgroundColor=[UIColor blackColor]; UIFont *font=[UIFont boldSystemFontOfSize:18.0]; CGRect sizeToFit = [value boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{ NSForegroundColorAttributeName:backgroundColor, NSFontAttributeName:font } context:nil]; return sizeToFit.size.height;}
iOS 字串的寬度和高度自適應