需求图如下所示
 
  1. UILabel “上期”   距离屏幕最左边 有35px
  2. UILabel “下期”   距离屏幕最右边 有35px
  3. 进行中文字在UIlabel 中间
 
图片效果如下:
 
 
实现思路:
 
  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.textboundingRectWithSize: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.textboundingRectWithSize: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 = [[UIScreenmainScreen] bounds].size.width;
   
    return [self contentSizeForWidth:screenWidth];
}

- (BOOL)isTruncated
{
    CGSize size = [self.textboundingRectWithSize: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文字的自适应宽度。
 
 
 
 
如上图的  “步数”,自适应方法也一样,“步数”的UILabel 距离 屏幕最右边的距离是固定不变的,所以要用UILabel中文字自适应的方法来解决。UILabel 的X坐标会随着“步数”Label的扩大而减少,使UILabel距离屏幕右边距离固定不变。
 
 
 
图(1)具体实现:
 
    _topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40)];
    _topView.backgroundColor = NF_Color_C19;
   
    _topViewBigLabel = [[UILabelalloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
    _topViewBigLabel.font = [UIFont systemFontOfSize:Near_Final_Font_T4];
    _topViewBigLabel.textColor = NF_Color_C1;
    _topViewBigLabel.text = @"进行中";
    [_topViewBigLabelsizeToFit];
    _topViewBigLabel.center = CGPointMake(_topView.bounds.size.width/2,_topView.bounds.size.height/2);
    [self.view addSubview:_topView];
    [self.view addSubview:_topViewBigLabel];
   
    _topViewleftLabel = [[UILabelalloc] init];
    _topViewleftLabel.font = [UIFont systemFontOfSize:Near_Final_Font_T9];
    _topViewleftLabel.textColor = NF_Color_C1;
    _topViewleftLabel.text = @"上期";
    _topViewleftLabel.frame = CGRectMake(35, 0, 0, 0);
    [_topViewleftLabelsizeToFit];
    _topViewleftLabel.centerY = _topView.bounds.size.height/2;
    [_topView addSubview:_topViewleftLabel];
      
    _topViewrightLabel = [[UILabelalloc] 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);
    [_topViewrightLabelsizeToFit];
    _topViewrightLabel.centerY = _topView.bounds.size.height/2;
    [_topView addSubview:_topViewrightLabel];
    
 
 

UILabel实现自适应宽高需要注意的地方(二)的更多相关文章

  1. UILabel实现自适应宽高需要注意的地方(三)

        一.需求图如下所示    UILabel 的高度自适应 UILabel中的段落间距可设置   图片效果如下:   调整段落适应长宽高方式:         需求:   保证"游戏玩法 ...

  2. UILabel实现自适应宽高需要注意的地方

    需求如下:   需要显示2行文字,宽度为 SCREEN_Width - 40 高度为两行文本的自适应高度 需要在此UILabel 下面添加imageView , 因此UIlabel 的高度需要准确,不 ...

  3. 小程 序swiper自适应宽高

    https://blog.csdn.net/qq_31604363/article/details/73715944 小程 序swiper自适应宽高 小程 序swiper自适应宽高

  4. OpenGL ES学习笔记(二)——平滑着色、自适应宽高及三维图像生成

    首先申明下,本文为笔者学习<OpenGL ES应用开发实践指南(Android卷)>的笔记,涉及的代码均出自原书,如有需要,请到原书指定源码地址下载. <Android学习笔记--O ...

  5. iOS - web自适应宽高(预设置的大小)

    //web自适应宽高 -(void)webViewDidFinishLoad:(UIWebView *)webView { NSLog(@"wessd"); [ webView s ...

  6. iview Carousel 轮播图自适应宽高;iview 轮播图 图片重叠问题;iview tabs 高度互相影响问题;vue this问题;

    最终效果图: 一.轮播图中图片自适应宽高:  <Carousel loop v-bind:height="imgHeight+'px'" v-model="caro ...

  7. UILabel 自适应宽高

    #import <UIKit/UIKit.h> @interface UILabel (UILabel_LabelHeighAndWidth) + (CGFloat)getHeightBy ...

  8. js和php计算图片自适应宽高算法实现

    js Code: <script> $width = $(imgobj).width(); //图原始宽 $newheight = $(imgobj).height(); //图原始高 $ ...

  9. css background-image 自适应宽高——转载

    就是这么简单的一句话,设置背景图,并让它100%的适应导航栏宽高,并设置不重复,大小100%就OK了 .zjhn-nav li.active a{ background-image:url(../im ...

随机推荐

  1. C 删除字符串1字符串2

    #include<stdio.h> #include<string.h> void main() { char s1[1000],s2[100],b[100]; int i,j ...

  2. mingw qt(可以去掉mingwm10.dll、libgcc_s_dw2-1.dll、libstdc++-6.dll的依赖,mingw默认都是动态链接gcc的库而TDM是静态链接gcc库,tdm版本更好用。用aspack压缩没有问题。qt本身不使用异常处理)good

    原文地址:mingw qt作者:孙1东 不使用Qt SDK,使用mingw编译qt源代码所遇问题及解决方法: configure -fast -release -no-exceptions -no-r ...

  3. sql知识收藏小总结

    div { background-color: #eee; border-radius: 3px; border: 1px solid #999; padding: 4px; display: blo ...

  4. Array类型判断

    http://www.cnblogs.com/YaoAzhen/archive/2010/01/27/1657696.html

  5. struts2 no extension(excludePattern)

    采用struts2 小伙伴非常希望更改或删除action扩展,本文将帮助你实现 struts2-core-2.3.16.jar , 下载链接: http://repo1.maven.org/maven ...

  6. Java中文件的上传与下载

    文件的上传与下载主要用到两种方法: 1.方法一:commons-fileupload.jar  commons-io.jar apache的commons-fileupload实现文件上传,下载 [u ...

  7. 【转】Mybatis传多个参数(三种解决方案)

    转自: http://www.2cto.com/database/201409/338155.html 据我目前接触到的传多个参数的方案有三种. 第一种方案: DAO层的函数方法 Public Use ...

  8. qt的应用层主要是大型3d,vr,管理软件和器械嵌入软件(有上千个下一代软件黑科技项目是qt的,美国宇航局,欧洲宇航局,超级战舰DDG1000)

    作者:Nebula.Trek链接:https://www.zhihu.com/question/24316868/answer/118944490来源:知乎著作权归作者所有.商业转载请联系作者获得授权 ...

  9. WPF:拖动父窗口行为

    原文 WPF:拖动父窗口行为 这次只是一个快速的帖子:当我点击并拖动特定的UIElement时,我需要能够重新定位WPF窗口.目的是重新创建在标准Windows标题栏上单击和拖动的行为(在我的情况下, ...

  10. JDK10下安装Eclipse photon 提示Java for Windows Missing

    这两天把服务器清理了一下,操作系统也重新装了,没办法啊,就是喜欢倒腾...在重新安装软件的时候,我又到各个官网去看了软件的最新版本,其中就去了JDK和Eclipse的官网溜达了一圈. 很久没有更新过自 ...