UILabel *label = [[UILabel alloc]init];
label.numberOfLines = 0; // 需要把显示行数设置成无限制
label.font = [UIFont systemFontOfSize:15];
label.textAlignment = NSTextAlignmentCenter;
label.text = @"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
CGSize size = [self sizeWithString:label.text font:label.font];
label.bounds = CGRectMake(0, 0, size.width, size.height);
label.center = self.view.center;
[self.view addSubview:label];
// 定义成方法方便多个label调用 增加代码的复用性
-(CGSize)sizeWithString:(NSString *)string font:(UIFont *)font
CGRect rect = [string boundingRectWithSize:CGSizeMake(320, 8000)//限制最大的宽度和高度
options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesFontLeading |NSStringDrawingUsesLineFragmentOrigin//采用换行模式
attributes:@{NSFontAttributeName: font}//传人的字体字典
context:nil];
return rect.size;
CGRect contentRect = [NSString heightForString:badgeValue Size:contentSize Font:kValueFont];
//获取某固定文本的显示高度
+(CGRect)heightForString:(NSString*)str Size:(CGSize)size Font:(UIFont*)font
return [NSString heightForString:str Size:size Font:font Lines:0];
+(CGRect)heightForString:(NSString*)str Size:(CGSize)size Font:(UIFont*)font Lines:(NSInteger)lines
if (StringIsNullOrEmpty(str)) {
return CGRectMake(0, 0, 0, 0);
static UILabel *lbtext;
if (lbtext==nil) {
lbtext = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height)];
}else{
lbtext.frame=CGRectMake(0, 0, size.width, size.height);
lbtext.font=font;
lbtext.text=str;
lbtext.numberOfLines=lines;
CGRect rect= [lbtext textRectForBounds:lbtext.frame limitedToNumberOfLines:lines];
if(rect.size.height<0)
rect.size.height=0;
if (rect.size.width<0) {
rect.size.width=0;
return rect;
@return 根据字符串的的长度来计算UITextView的高度
*/
+(float)heightForString:(NSString *)value fontSize:(float)fontSize andWidth:(float)width
{
float height = [[NSString stringWithFormat:@"%@
",value] boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:fontSize],NSFontAttributeName, nil] context:nil].size.height;
return height;
UITextView根据内容自动改变frame
*/
(void)textViewDidChange:(UITextView *)textView
{
[textView flashScrollIndicators]; // 闪动滚动条
static CGFloat maxHeight = 130.0f;
CGRect frame = textView.frame;
CGSize constraintSize = CGSizeMake(frame.size.width, MAXFLOAT);
CGSize size = [textView sizeThatFits:constraintSize];
if (size.height >= maxHeight)
size.height = maxHeight;
textView.scrollEnabled = YES; // 允许滚动
textView.scrollEnabled = NO; // 不允许滚动,当textview的大小足以容纳它的text的时候,需要设置scrollEnabed为NO,否则会出现光标乱滚动的情况
textView.frame = CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, size.height);
2.字体、大小、单位、颜色
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 30, 300, 260)];
label.text = @"Label Text Content, This is a text label things ...
SexySix
代码演示:
UILabel *lable = [[UILabel alloc] init];
lable.frame = CGRectMake(10, 100, 350, 300);
lable.backgroundColor = [UIColor yellowColor];
// 文本
lable.text = @"网提供了丰富的移动端开发、php开发、web前端、html...
RayKr
UITextField和UITextView
UITextField
UITextField文本框与UILabel的不同是文本框是可以编辑的。iOS用到的文本框的地方很多,比如搜索框、用户登录框等。
UITextField的属性检查器的主要内容有:
...个问题困扰了很久,这里谈一下最近的一个理解。我们拿UITextView来看
当我们把一个UITextView放到一个没有导航的VC中时:
UITextView *textView = [[UITextView alloc] init];
textView.frame = CGRectMake(10, 200, 300, 120);
textView.backgroundColor = [UIColor redCol...
PrototypeZ
阅读 2267·2021-11-22 09:34
阅读 3645·2021-11-15 18:13
阅读 2269·2021-09-24 10:24
阅读 920·2021-09-22 16:01
阅读 2359·2021-09-06 15:02
阅读 787·2019-08-30 13:01
阅读 721·2019-08-30 10:52
阅读 367·2019-08-29 16:36