{"id":257,"date":"2015-05-21T11:37:45","date_gmt":"2015-05-21T03:37:45","guid":{"rendered":"https:\/\/antijava.prjlife.com\/?p=257"},"modified":"2015-05-21T11:37:45","modified_gmt":"2015-05-21T03:37:45","slug":"uilabel-%e5%8b%95%e6%85%8b%e9%ab%98%e5%ba%a6-dynamic-height","status":"publish","type":"post","link":"https:\/\/antijava.prjlife.com\/?p=257","title":{"rendered":"UILabel \u52d5\u614b\u9ad8\u5ea6 Dynamic Height"},"content":{"rendered":"<p>UITableViewCell \u6709 heightForRowAtIndexPath \u53ef\u4ee5\u6307\u5b9a<\/p>\n<p>\u4f46\u662f\u5be6\u52d9\u4e0a\u7e3d\u662f\u6703\u78b0\u5230\u5ba2\u6236\u7684\u8981\u6c42\uff1a\u300c\u52d5\u614b\u5217\u9ad8\u300d<\/p>\n<p>\u82e5\u662f\u300c\u60f3\u4ee5\u6587\u5b57\u5167\u5bb9\u6c7a\u5b9a UILabel \u7684\u9ad8\u5ea6\u300d<\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528\u4e0b\u9762\u7684\u7a0b\u5f0f<\/p>\n<p>\u5148\u7528\u300c\u6587\u5b57\uff0c\u5b57\u578b\u548c\u5bec\u5ea6\u300d(\u9019\u9ede\u8981\u4e8b\u5148\u6c7a\u5b9a\uff0c\u4e0d\u7136\u6c92\u8fa6\u6cd5\u7b97)\u8a08\u7b97\u51fa UILabel \u7684\u9ad8\u5ea6<\/p>\n<p>\u7136\u5f8c\u518d\u7528\u9019\u500b\u7b97\u51fa\u4f86\u7684\u9ad8\u5ea6\u53bb initWithFrame<\/p>\n<p>\u641e\u5b9a\uff0c\u6536\u5de5\u653e\u98ef<\/p>\n<p>&nbsp;<\/p>\n<p>+(float) getHeightForText:(NSString*) text withFont:(UIFont*) font andWidth:(float)width {<br \/>\nCGSize constraint = CGSizeMake(width , 20000.0f);<br \/>\nCGSize title_size;<br \/>\nfloat totalHeight;<\/p>\n<p>SEL selector = @selector(boundingRectWithSize:options:attributes:context:);<br \/>\nif ([text respondsToSelector:selector]) {<br \/>\ntitle_size = [text boundingRectWithSize:constraint<br \/>\noptions:NSStringDrawingUsesLineFragmentOrigin<br \/>\nattributes:@{ NSFontAttributeName : font }<br \/>\ncontext:nil].size;<\/p>\n<p>totalHeight = ceil(title_size.height);<br \/>\n} else {<br \/>\ntitle_size = [text sizeWithFont:font<br \/>\nconstrainedToSize:constraint<br \/>\nlineBreakMode:NSLineBreakByWordWrapping];<br \/>\ntotalHeight = title_size.height ;<br \/>\n}<\/p>\n<p>CGFloat height = MAX(totalHeight, 40.0f);<br \/>\nreturn height;<br \/>\n}<\/p>\n","protected":false},"excerpt":{"rendered":"<p>UITableViewCell \u6709 heightForRowAtIndexPath \u53ef\u4ee5\u6307\u5b9a \u4f46\u662f\u5be6\u52d9\u4e0a\u7e3d\u662f\u6703\u78b0\u5230\u5ba2\u6236\u7684\u8981\u6c42\uff1a\u300c\u52d5\u614b\u5217\u9ad8\u300d \u82e5\u662f\u300c\u60f3\u4ee5\u6587\u5b57\u5167\u5bb9\u6c7a\u5b9a UILabel \u7684\u9ad8\u5ea6\u300d \u53ef\u4ee5\u4f7f\u7528\u4e0b\u9762\u7684\u7a0b\u5f0f \u5148\u7528\u300c\u6587\u5b57\uff0c\u5b57\u578b\u548c\u5bec\u5ea6\u300d(\u9019\u9ede\u8981\u4e8b\u5148\u6c7a\u5b9a\uff0c\u4e0d\u7136\u6c92\u8fa6\u6cd5\u7b97)\u8a08\u7b97\u51fa UILabel \u7684\u9ad8\u5ea6 \u7136\u5f8c\u518d\u7528\u9019\u500b\u7b97\u51fa\u4f86\u7684\u9ad8\u5ea6\u53bb initWithFrame \u641e\u5b9a\uff0c\u6536\u5de5\u653e\u98ef &nbsp; +(float) getHeightForText:(NSString*) text withFont:(UIFont*) font andWidth:(float)width { CGSize constraint = CGSizeMake(width , 20000.0f); CGSize title_size; float totalHeight; SEL selector = @selector(boundingRectWithSize:options:attributes:context:); if ([text respondsToSelector:selector]) &hellip; <a href=\"https:\/\/antijava.prjlife.com\/?p=257\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49],"tags":[],"class_list":["post-257","post","type-post","status-publish","format-standard","hentry","category-ios-development"],"_links":{"self":[{"href":"https:\/\/antijava.prjlife.com\/index.php?rest_route=\/wp\/v2\/posts\/257","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/antijava.prjlife.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/antijava.prjlife.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/antijava.prjlife.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/antijava.prjlife.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=257"}],"version-history":[{"count":0,"href":"https:\/\/antijava.prjlife.com\/index.php?rest_route=\/wp\/v2\/posts\/257\/revisions"}],"wp:attachment":[{"href":"https:\/\/antijava.prjlife.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=257"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/antijava.prjlife.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=257"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/antijava.prjlife.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=257"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}