• 关于UIFont和计算字符串的高度和宽度


    转自:http://i.cnblogs.com/EditPosts.aspx?opt=1

    1、创建方法:
    + fontWithName:size:
    - fontWithSize:
    2、创建系统字体:
    + systemFontOfSize:
    + boldSystemFontOfSize:
    + italicSystemFontOfSize:
    3、获得可用的Font Names:
    + familyNames
    + fontNamesForFamilyName:
    4、获得Font Name属性:
    familyName 属性 和 fontName属性
    5、获得Font Metrics:
    pointSize属性:
    ascender属性:基准线以上的最高y坐标。
    descender属性:基准线以下的最低y坐标
    capHeight属性:The receiver’s cap height information 接收者的大写高度信息??
    xHeight属性:返回小写x的高度
    lineHeight属性:文本行的高度
    6、获得系统字体信息:
    + labelFontSize:
    + buttonFontSize:
    + smallSystemFontSize:
    + systemFontSize:

    所以,一般来说要用UILabel显示文本内容,而计算高度的话,使用font.lineHeight*行数就可以了。当然这需要每一行的长度不能超过label的长度。
    如果这仍然难以控制,可以使用string的
    – sizeWithFont:  
    – sizeWithFont:forWidth:lineBreakMode:  
    – sizeWithFont:constrainedToSize:  
    – sizeWithFont:constrainedToSize:lineBreakMode:  
    – sizeWithFont:minFontSize:actualFontSize:forWidth:lineBreakMode:
    这些方法来测量字符串所占的CGSize。
    通过– sizeWithFont:constrainedToSize:lineBreakMode:  这个可以方法,可以指定一个比较大的constrainedSize,来计算字符串的实际高度,例如:

    然后计算Label的实际行数,以决定Label的高度:
        UILabel *label1=[[UILabel alloc]initWithFrame:CGRectMake(0, 200, 200, 1)];
        label1.text=@"我会根据字符串的长度计算行数,并动态设置label1的高度!不信就试试看看? 好的,让我们来看看吧!!!!!!!!!!!!!!!!!";
        label1.lineBreakMode=UILineBreakModeCharacterWrap;
        label1.numberOfLines=0;

        CGRect rect=label1.frame;
        CGSize testSize=tempRect.size;
        testSize.height=testSize.height*1000;
        tempRect.size=[[label1 text] sizeWithFont:label1.font constrainedToSize:testSize lineBreakMode:NSLineBreakByCharWrapping]; 
        label1.frame=rect;

  • 相关阅读:
    [CF590C] Three States
    [CF767B] The Queue
    [CF1296F] Berland Beauty
    [CF3D] Least Cost Bracket Sequence
    YUV420 转 RGB 测试
    [POI2012] TOU-Tour de Byteotia
    [CF576C] Points on Plane
    [CF191C] Fools and Roads
    [CF1485C] Floor and Mod
    [CF1399D] Binary String To Subsequences
  • 原文地址:https://www.cnblogs.com/wujie123/p/5392626.html
Copyright © 2020-2023  润新知