日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

uilabel 自适应

發布時間:2023/12/10 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 uilabel 自适应 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

有時一個UILable的text內容是變化的,而且差異有很大,??

  • 需求上要求UILabel的大小高寬能夠自適應text的內容。代碼例子:???
  • myLable=[[UILabel?alloc]?initWithFrame:CGRectMake(0,?23,?175,?33)];???
  • [myLable?setFont:[UIFont?fontWithName:@"Helvetica"?size:10.0]];???
  • [myLable?setNumberOfLines:0];???
  • [myLable?setBackgroundColor:[UIColor?clearColor]];???
  • [myAdView?addSubview:myLable];???
  • UIFont?*font?=?[UIFont?fontWithName:@"Helvetica"?size:10.0];???
  • CGSize?size?=?[text?sizeWithFont:font?constrainedToSize:CGSizeMake(175.0f,?2000.0f)???
  • ??????????????????????????????????????????lineBreakMode:UILineBreakModeWordWrap];???
  • CGRect?rect=myLable.frame;???
  • rect.size=size;???
  • [myLable?setFrame:rect];???
  • [myLable?setText:text];??
  • ??
  • 核心的是??
  • CGSize?size?=?[text?sizeWithFont:font?constrainedToSize:CGSizeMake(175.0f,?2000.0f)???
  • ????????????????????lineBreakMode:UILineBreakModeWordWrap];??
  • 來預算text顯示時寬高。??
  • 其中font是顯示的字體,constrainedToSize是最大可接受的字符串寬高(例子中是寬175,高2000)??
  • lineBreakMode換行類型(UILineBreakModeWordWrap指的單詞邊界換行)??
  • ?

    sizeWithFont:constrainedToSize:lineBreakMode:

    Returns the size of the string if it were rendered with the specified constraints.

    - (CGSize)sizeWithFont:(UIFont?*)font?constrainedToSize:(CGSize)size?lineBreakMode:(UILineBreakMode)lineBreakMode
    Parameters
    font

    The font to use for computing the string size.

    size

    The maximum acceptable size for the string. This value is used to calculate where line breaks and wrapping would occur.

    lineBreakMode

    The line break options for computing the size of the string. For a list of possible values, see?NSLineBreakMode.

    Return Value

    The width and height of the resulting string’s bounding box. These values may be rounded up to the nearest whole number.

    Discussion

    You can use this method to obtain the layout metrics you need to draw a string in your user interface. This method does not actually draw the string or alter the receiver’s text in any way.

    This method computes the metrics needed to draw the specified string. This method lays out the receiver’s text and attempts to make it fit the specified size using the specified font and line break options. During layout, the method may break the text onto multiple lines to make it fit better. If the receiver’s text does not completely fit in the specified size, it lays out as much of the text as possible and truncates it (for layout purposes only) according to the specified line break mode. It then returns the size of the resulting truncated string. If the height specified in the?size?parameter is less than a single line of text, this method may return a height value that is bigger than the one specified.

    Availability
    • Available in iOS 2.0 and later.
    Declared In
    UIStringDrawing.h

    轉載于:https://www.cnblogs.com/mohe/p/3586568.html

    總結

    以上是生活随笔為你收集整理的uilabel 自适应的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。