zoukankan      html  css  js  c++  java
  • IOS UILabel的一些使用小技巧

    1. 你在iOS6的需要NSLineBreakByWordWrapping 为了您的代码试试这个:

    NSString *string = @"bla";
    CGSize s = [string sizeWithFont:[UIFont systemFontOfSize:20]
        constrainedToSize:CGSizeMake(self.view.bounds.size.width - 40,  CGFLOAT_MAX) // - 40 For cell padding
         lineBreakMode:NSLineBreakByWordWrapping];
    

    的标签上,例如,将

     [label setLineBreakMode:NSLineBreakByWordWrapping];
    

    而不是

    label.lineBreakMode = UILineBreakModeWordWrap;
    


    2. 为了保持你可以创建一个宏,如下:

    #ifdef __IPHONE_6_0
    # define LINE_BREAK_WORD_WRAP NSLineBreakByWordWrapping
    #else
    # define LINE_BREAK_WORD_WRAP UILineBreakModeWordWrap
    #endif




    typedef enum {
       UILineBreakModeWordWrap = 0,
       UILineBreakModeCharacterWrap,
       UILineBreakModeClip,
       UILineBreakModeHeadTruncation,
       UILineBreakModeTailTruncation,
       UILineBreakModeMiddleTruncation,
    } UILineBreakMode;


       UILineBreakModeWordWrap = 0,
       以单词为单位换行,以单位为单位截断。
       UILineBreakModeCharacterWrap,
       以字符为单位换行,以字符为单位截断。
       UILineBreakModeClip,
       以单词为单位换行。以字符为单位截断。
       UILineBreakModeHeadTruncation,
       以单词为单位换行。如果是单行,则开始部分有省略号。如果是多行,则中间有省略号,省略号后面有4个字符。
       UILineBreakModeTailTruncation,
       以单词为单位换行。无论是单行还是多行,都是末尾有省略号。
       UILineBreakModeMiddleTruncation,
       以单词为单位换行。无论是单行还是多行,都是中间有省略号,省略号后面只有2个字符。


    NSLineBreakByWordWrapping = 0,//以空格为边界,保留单词
    NSLineBreakByCharWrapping,    //保留整个字符
    NSLineBreakByClipping,        //简单剪裁,到边界为止
    NSLineBreakByTruncatingHead,  //按照"……文字"显示
    NSLineBreakByTruncatingTail,  //按照"文字……文字"显示
    NSLineBreakByTruncatingMiddle //按照"文字……"显示
     
     
    例如:
     
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0,0,0,0)];//后面还会重新设置其size。
    [label setNumberOfLines:0];
    NSString *s = @"string......";
    UIFont *font = [UIFont fontWithName:@"Arial" size:12];
    CGSize size = CGSizeMake(320,2000);
    CGSize labelsize = [s sizeWithFont:font constrainedToSize:sizelineBreakMode:UILineBreakModeWordWrap];
    [label setFrame:CGRectMake(0, 0, labelsize.width, labelsize.height)];

    [self.view addSubView:label];

  • 相关阅读:
    ecs云服务器 mysql经常自动停止挂掉重启问题分析
    mysql报错mmap(137428992 bytes) failed; errno 12,Cannot allocate memory for the buffer pool
    nodeJs的nodemailer发邮件报错hostname/IP doesn't match certificate's altnames怎么解决?
    js数组去重常用方法
    rsync远程数据同步工具的使用
    nginx报错 [error] open() “/usr/local/var/run/openresty.pid” failed (2: No such file or directory)
    webstorm使用问题总结
    正则表达式
    docker
    【转】90%的人会遇到性能问题,如何用1行代码快速定位
  • 原文地址:https://www.cnblogs.com/damnbird/p/4939934.html
Copyright © 2011-2022 走看看