uilabel 自适应

有时一个UILable的text内容是变化的,而且差异有很大,  

  1. 需求上要求UILabel的大小高宽能够自适应text的内容。代码例子:   
  2. myLable=[[UILabel alloc] initWithFrame:CGRectMake(0, 23, 175, 33)];   
  3. [myLable setFont:[UIFont fontWithName:@"Helvetica" size:10.0]];   
  4. [myLable setNumberOfLines:0];   
  5. [myLable setBackgroundColor:[UIColor clearColor]];   
  6. [myAdView addSubview:myLable];   
  7. UIFont *font = [UIFont fontWithName:@"Helvetica" size:10.0];   
  8. CGSize size = [text sizeWithFont:font constrainedToSize:CGSizeMake(175.0f, 2000.0f)   
  9.                                           lineBreakMode:UILineBreakModeWordWrap];   
  10. CGRect rect=myLable.frame;   
  11. rect.size=size;   
  12. [myLable setFrame:rect];   
  13. [myLable setText:text];  
  14.   
  15. 核心的是  
  16. CGSize size = [text sizeWithFont:font constrainedToSize:CGSizeMake(175.0f, 2000.0f)   
  17.                     lineBreakMode:UILineBreakModeWordWrap];  
  18. 来预算text显示时宽高。  
  19. 其中font是显示的字体,constrainedToSize是最大可接受的字符串宽高(例子中是宽175,高2000)  
  20. 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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/421140.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

1065. 单身狗(25)

1065. 单身狗(25) 时间限制300 ms内存限制65536 kB代码长度限制8000 B判题程序Standard作者CHEN, Yue“单身狗”是中文对于单身人士的一种爱称。本题请你从上万人的大型派对中找出落单的客人,以便给予特殊关爱。 输入格式: 输入第一行给出一个正整数N&am…

ArrayList的remove方法(重写equals方法) 与LinkedList的常用操作

package C12_18;import java.util.ArrayList;public class joy {public static void main(String[] args) {show();//重写equals方法&#xff0c;移除集合里面的元素。public static void show() {ArrayList<dog> al new ArrayList<dog>();al.add(new dog("j…

期末寒假绝对好好学

期末考试考7门&#xff0c;距离最近的一门还有7天&#xff0c;我保证我这几天绝对好好复习&#xff0c;好好过一个寒假。我的寒假从来都是浪完的&#xff0c;我发誓我这一次绝对好好学习٩( •̀㉨•́ )و &#xff0c;我不好好学&#xff0c;我这辈子都单身

android学习日记13--数据存储之ContentProvide

3、ContentProvider  数据在Android当中是私有的&#xff0c;当然这些数据包括文件数据和数据库数据以及一些其他类型的数据。ContentProvider实现多应用程序间的数据共享类一般利用ContentProvider为需要共享的数据定义一个URI(统一资源定位符)然后其他程序通过Context获得C…

cin,cin.get(),getline()

我势必扫清我对c的各种疑惑&#xff0c;重拾csdn水文之任 结论&#xff1a;cin在获得需要接受的东西之前&#xff0c;对缓冲区里的空格和换行符不会理睬(但是会把它们从缓冲区删去)&#xff0c;但如果达到了可以结束接受的时候&#xff0c;空格和换行符都会让cin不再接 收,并且…

1067. 试密码(20)

1067. 试密码(20) 时间限制400 ms内存限制65536 kB代码长度限制8000 B判题程序Standard作者CHEN, Yue当你试图登录某个系统却忘了密码时&#xff0c;系统一般只会允许你尝试有限多次&#xff0c;当超出允许次数时&#xff0c;账号就会被锁死。本题就请你实现这个小功能。 输入格…

Map接口的实现类HashMap的操作

3中遍历HashMap方式 package C12_21;import java.util.Collection; import java.util.HashMap; import java.util.Set; import java.util.Map.Entry;public class testHashMap {public static void main(String[] args) {//定义两个HashMap 集合HashMap<Integer, Integer&g…

1068. 万绿丛中一点红(20)

1068. 万绿丛中一点红(20) 时间限制500 ms内存限制65536 kB代码长度限制8000 B判题程序Standard作者CHEN, Yue对于计算机而言&#xff0c;颜色不过是像素点对应的一个24位的数值。现给定一幅分辨率为MxN的画&#xff0c;要求你找出万绿丛中的一点红&#xff0c;即有独一无二颜色…

MapReduce 作业调试

1. 最经典的方法通过打印语句来调试程序 System.err.println("Bad Data"value.toString()); 这些输出错误都会记录到一个标准错误中&#xff0c;可以通过WebUI进行查看 2.可以创建一个自定义计数器来统计整个数据集中不合理的数据的数量。 首先创建一个enum enum BA…

寻找边界值,二分模板,简洁凝练

二分模板 class Solution {public static void main(String[] args) {int[] arr{1,2,2,2,2,4,5,6};int l0,rarr.length-1;while(l<r){int midl(r-l)/2;if(arr[mid]>2) rmid;else lmid1;}System.out.println("l:"l"r:"r);System.out.println("le…

Java 字节和字符流的读写+Buffered

一个关于IO流的导图 IO流字节的读写&#xff0c;实现复制 import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException;public class TestCopy {public static void main(String[] args) throws IOException {copyT…

1069. 微博转发抽奖(20)

1069. 微博转发抽奖(20) 时间限制400 ms内存限制65536 kB代码长度限制8000 B判题程序Standard作者CHEN, Yue小明PAT考了满分&#xff0c;高兴之余决定发起微博转发抽奖活动&#xff0c;从转发的网友中按顺序每隔N个人就发出一个红包。请你编写程序帮助他确定中奖名单。 输入格式…

jdbc 连接 Oracle 进行基本的增删改查

package api8;import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement;public class UpdateSelect {public static void main(String[] args) {// jdbc 连接 OracleselectAll();}//…

1070. 结绳(25)

1070. 结绳(25) 时间限制200 ms内存限制65536 kB代码长度限制8000 B判题程序Standard作者CHEN, Yue给定一段一段的绳子&#xff0c;你需要把它们串成一条绳。每次串连的时候&#xff0c;是把两段绳子对折&#xff0c;再如下图所示套接在一起。这样得到的绳子又被当成是另一段绳…

近期的随笔

有一段时间没有更新博客了&#xff0c;这一段时间确实忙&#xff0c;换工作&#xff0c;过年&#xff0c;适应新工作&#xff0c;培训&#xff0c;搬家等等。今天积累了一点想法想写写。 1.关于博客 在运营了独立博客一段时间后&#xff0c;有了比较深的感受就是为了高大上而高…

Java预编译和批处理

预编译 package csdn.prepare.take;import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException;public class TestCompiling {public static void main(String[] args) {prep…

1028. 人口普查(20)

1028. 人口普查(20) 时间限制200 ms内存限制65536 kB代码长度限制8000 B判题程序Standard作者CHEN, Yue某城镇进行人口普查&#xff0c;得到了全体居民的生日。现请你写个程序&#xff0c;找出镇上最年长和最年轻的人。 这里确保每个输入的日期都是合法的&#xff0c;但不一定是…

结对项目开发-电梯调度

结对项目开发-电梯调度n设计和实现一个电梯调度瞄准两个正确性和性能&#xff0c;在托管代码。n技能训练&#xff1a;na&#xff09;需求分析nb&#xff09;高层次设计&#xff08;界面&#xff0c;信息隐藏&#xff0c;松耦合&#xff09;nc&#xff09;设计文档nd&#xff09…

1030. 完美数列(25)

1030. 完美数列(25) 时间限制300 ms内存限制65536 kB代码长度限制8000 B判题程序Standard作者CAO, Peng给定一个正整数数列&#xff0c;和正整数p&#xff0c;设这个数列中的最大值是M&#xff0c;最小值是m&#xff0c;如果M < m * p&#xff0c;则称这个数列是完美数列。 …

Java模拟事务Demo

Java操作Oracle事务&#xff0c;以转账为例。 转账之前 package translate.commit;import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException;public class CommitRollback…