UILabel實現自適應寬高須要注意的地方(二)

需求圖以下所示
 
  1. UILabel 「上期」   距離屏幕最左邊 有35px
  2. UILabel 「下期」   距離屏幕最右邊 有35px
  3. 進行中文字在UIlabel 中間
 
圖片效果以下:
 
屏幕快照 2017 03 27 上午12 16 08
 
實現思路:
 
  1. 「上期"距離左邊35,設置「上期」的X座標爲35便可。設置「上期」的Y座標爲整個頭部(紅色View)的中心位置便可,經過紅色View的Frame高度 來獲得他的中心軸的座標。
  2. 「下期」距離右邊35,這個就沒法經過直接設置35的距離來保持和右邊邊界距離恰好爲35,要知道「下期」這個UIlabel的寬度,設置「下期」的X座標爲UIlabel「下期」的寬度 + 35px  便可。UILabel寬度如何獲取?
 
獲取UILabel寬度方法:經過UILabel中的文本方法來獲取UILabel寬度,方法 boundingRectWithSize以下
 
封裝方法以下:
 
@implementation UILabel (ContentSize)

- ( CGSize )contentSizeForWidth:( CGFloat )width
{
   
if ( nil == self . text || [ @「"  isEqualToString : self . text ]) {
       
return  CGSizeZero ;
    }
   
   
CGRect contentFrame = [ self . text boundingRectWithSize : CGSizeMake (width, MAXFLOAT )
                                                 
options : NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
                                              
attributes : @{ NSFontAttributeName : self . font }
                                                 
context : nil ];
   
   
return  CGSizeMake ( ceil (contentFrame. size . width + 0.5 ), ceil (contentFrame. size . height + 0.5 ));
}

- (
CGSize )contentSizeForWidthUsesDeviceMetrics:( CGFloat )width
{
   
CGRect contentFrame = [ self . text boundingRectWithSize : CGSizeMake (width, MAXFLOAT )
                                                 
options : NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesDeviceMetrics
                                              
attributes : @{ NSFontAttributeName : self . font }
                                                 
context : nil ];
   
   
return  CGSizeMake ( ceil (contentFrame. size . width + 0.5 ), ceil (contentFrame. size . height + 0.5 ));
}

- (
CGSize )contentSize
{
   
CGFloat screenWidth = [[ UIScreen mainScreen ] bounds ]. size . width ;
   
   
return [ self  contentSizeForWidth :screenWidth];
}

- (
BOOL )isTruncated
{
   
CGSize size = [ self . text boundingRectWithSize : CGSizeMake ( self . bounds . size . width , MAXFLOAT )
                                         
options : NSStringDrawingUsesLineFragmentOrigin
                                      
attributes : @{ NSFontAttributeName : self . font }
                                         
context : nil ]. size ;
   
   
return (size. height > self . frame . size . height );
}
 
經過以上方式,直接實現UIlabel 中的  contentSize 方法便可得到UIlabel文字的自適應寬度。
 
 
  屏幕快照 2017 03 27 上午12 24 56
 
如上圖的  「步數」,自適應方法也同樣,「步數」的UILabel 距離 屏幕最右邊的距離是固定不變的,因此要用UILabel中文字自適應的方法來解決。UILabel 的X座標會隨着「步數」Label的擴大而減小,使UILabel距離屏幕右邊距離固定不變。
 
 
 
圖(1)具體實現:
 
    _topView = [[ UIView  alloc ] initWithFrame : CGRectMake ( 0 , 0 , SCREEN_WIDTH , 40 )];
   
_topView . backgroundColor = NF_Color_C19 ;
   
   
_topViewBigLabel = [[ UILabel alloc ] initWithFrame : CGRectMake ( 0 , 0 , 0 , 0 )];
   
_topViewBigLabel . font = [ UIFont  systemFontOfSize : Near_Final_Font_T4 ];
   
_topViewBigLabel . textColor = NF_Color_C1 ;
   
_topViewBigLabel . text = @" 進行中 " ;
    [
_topViewBigLabel sizeToFit ];
   
_topViewBigLabel . center = CGPointMake ( _topView . bounds . size . width / 2 , _topView . bounds . size . height / 2 );
    [
self . view  addSubview : _topView ];
    [
self . view  addSubview : _topViewBigLabel ];
   
   
_topViewleftLabel = [[ UILabel alloc ] init ];
   
_topViewleftLabel . font = [ UIFont  systemFontOfSize : Near_Final_Font_T9 ];
   
_topViewleftLabel . textColor = NF_Color_C1 ;
   
_topViewleftLabel . text = @" 上期 " ;
   
_topViewleftLabel . frame = CGRectMake ( 35 , 0 , 0 , 0 );
    [
_topViewleftLabel sizeToFit ];
   
_topViewleftLabel . centerY = _topView . bounds . size . height / 2 ;
    [
_topView  addSubview : _topViewleftLabel ];
   
  
   
_topViewrightLabel = [[ UILabel alloc ] init ];
   
_topViewrightLabel . font = [ UIFont  systemFontOfSize : Near_Final_Font_T9 ];
   
_topViewrightLabel . textColor = NF_Color_C1 ;
   
_topViewrightLabel . text = @" 下期 " ;
   
_topViewrightLabel . frame = CGRectMake ( SCREEN_WIDTH - _topViewrightLabel . contentSize . width - 35 , 0 , 0 , 0 );
    [
_topViewrightLabel sizeToFit ];
   
_topViewrightLabel . centerY = _topView . bounds . size . height / 2 ;
    [
_topView  addSubview : _topViewrightLabel ];    
相關文章
相關標籤/搜索