餓了么App中店鋪會有琅捏,減、特递雀、新等標簽柄延,這些標簽一般都是用UILabel控件實現(xiàn)。
這個要求我們做圓角缀程,最簡單的就是設(shè)置label.layer.cornerRadius = 2; label.layer.masksToBounds = YES; 但是這樣做(label.layer.cornerRadius > 0 && label.layer.masksToBounds = YES)會出現(xiàn)離屏渲染拦焚,如果是每個TableViewCell設(shè)置一些圓角,就會使列表滑動起來有明顯卡頓杠输。
解決方法:
cornerRadius屬性: ? 它是影響layer顯示的backgroundColor和border赎败,對layer的contents不起作用。
對于不需要設(shè)置label的backgroundColor蠢甲,只設(shè)置borderWidth僵刮、borderColor,直接設(shè)置cornerRadius鹦牛,不需要設(shè)置masksToBounds = YES搞糕,就可以實現(xiàn)圓角功能。
對于需要同時設(shè)置label的backgroundColor時,直接設(shè)置cornerRadius是不能正常顯示圓角的,原因是:UILabel設(shè)置backgroundColor的行為勿她,不再是設(shè)定layer的背景色而是為contents設(shè)置背景色溶锭。所以解決方式是我們不去設(shè)置label的backgroundColor秦躯,而是直接設(shè)置label.layer.backgroundColor,這樣就可以實現(xiàn)單獨設(shè)置cornerRadius,顯示圓角的效果。