Ios 像子视图中的字幕一样滚动UILabel
我在主视图中有一个UILabel,带有文本-“非常长的文本”。合适的宽度应该是142,但我把它缩短到55 基本上,我想实现一个选框类型的滚动,所以我编写了代码将其添加到子视图中,并在该视图的范围内设置动画 代码—— 之后,我在模拟器上得到这个输出--> 当我尝试使用此代码制作动画时出现问题-Ios 像子视图中的字幕一样滚动UILabel,ios,objective-c,cocoa-touch,uilabel,Ios,Objective C,Cocoa Touch,Uilabel,我在主视图中有一个UILabel,带有文本-“非常长的文本”。合适的宽度应该是142,但我把它缩短到55 基本上,我想实现一个选框类型的滚动,所以我编写了代码将其添加到子视图中,并在该视图的范围内设置动画 代码—— 之后,我在模拟器上得到这个输出--> 当我尝试使用此代码制作动画时出现问题- tempLblFrame.origin.x = -_lblLongText.intrinsicContentSize.width; [UIView animateWithDu
tempLblFrame.origin.x = -_lblLongText.intrinsicContentSize.width;
[UIView animateWithDuration:2.0 delay:1.0 options:UIViewAnimationOptionCurveLinear
animations:^{
[_lblLongText setFrame:tempLblFrame];
}
completion:^(BOOL finished) {
NSLog(@"completed");
}];
我希望我能看到整个“非常非常长的文本”,而不是从左到右的“非常…”卷轴
为了解决这个问题我添加了一行代码--
我认为全文将设置在新添加的UIView中,并且可以正确滚动。但是在模拟器中运行给了我这个--
再一次,只有“非常…”从左向右滚动
我做错了什么?请帮忙
编辑
显然罪魁祸首是自动布局 我不知道为什么,但一旦我取消选中视图的“使用自动布局” 在锡伯银行,一切都按预期开始工作。背景 TempblFrame.origin.x=--U lblLongText.intrinsicContentSize.width;是 工作正常,卷轴也正常 有什么解释吗
将
ui标签
设置为文本的宽度(或更长),并将ui视图
设置为要查看的滚动区域。然后将ui视图
的cliptobunds
设置为YES
(您正在执行此操作)。然后,当您从左到右设置动画时,您将只看到UIView
宽度的文本,因为它正在剪切任何额外的子视图。只需确保滚动ui标签的整个长度即可
现在,您正在将视图和标签的高度和宽度设置为相同的值。这就是为什么您得到的是剪裁文本,而不是剪裁标签。显然罪魁祸首是自动布局
我不知道为什么,但一旦我在XIB中取消选中视图的“使用自动布局”,一切都开始按预期工作。设置TempblFrame.origin.x=--u lblLongText.intrinsicContentSize.width代码>工作正常,卷轴也正常
不过,更好的解释肯定会有所帮助
编辑:使用自动布局的解决方案-
您可以在视图scrollview中添加,并在滚动视图中添加此标签。使用以下代码
scroll.contentSize =CGSizeMake(100 *[clubArray count],20);
NSString *bname;
bname=@"";
for(int i = 0; i < [clubArray count]; i++)
{
bname = [NSString stringWithFormat:@"%@ %@ ,",bname,[[clubArray objectAtIndex:i] objectForKey:@"bottle_name"]];
[bname retain];
}
UILabel *lbl1 = [[UILabel alloc] init];
[lbl1 setFrame:CGRectMake(0,5,[clubArray count]*100,20)];
lbl1.backgroundColor=[UIColor clearColor];
lbl1.textColor=[UIColor whiteColor];
lbl1.userInteractionEnabled=YES;
[scroll addSubview:lbl1];
lbl1.text= bname;
scroll.contentSize=CGSizeMake(100*[clubaray count],20);
NSString*bname;
bname=@;
对于(int i=0;i<[clubaray count];i++)
{
bname=[NSString stringWithFormat:@“%@%@”,bname,[[clubArray objectAtIndex:i]objectForKey:@“瓶子名称”];
[名称保留];
}
UILabel*lbl1=[[UILabel alloc]init];
[lbl1 setFrame:CGRectMake(0,5,[clubArray count]*100,20)];
lbl1.backgroundColor=[UIColor clearColor];
lbl1.textColor=[UIColor-whiteColor];
lbl1.userInteractionEnabled=是;
[滚动添加子视图:lbl1];
lbl1.text=bname;
这是实现代码。谢谢这个问题可能是
虽然有Charles Powell为
也来看看
我希望这将帮助您,并通过提供所需的输出来节省您的时间。为什么您不使用一切都已修复的开源控件?首先,我想自己尝试一下。其次,我必须对现有项目进行更改,该项目有很多标签。我不想从每个xib(包括iPad和iPhone)上删除每个标签,而是想尝试一种省力的替代方法,我希望视图的宽度保持相同的长度。正如我在上面的评论中提到的,我必须在一个大项目中更改很多标签,我不想更改UI中的大小。如果文本长度超过指定的大小,我希望它滚动。您不想更改IB中的大小吗?UILabel无法滚动,因此当您将文本放入其中时,它将缩小文本或将其剪辑(添加…)。你将需要改变一些东西,我会推荐标签的宽度,这可能是最简单的事情。我尝试过不使用临时UIView。我添加了一个带有长文本的小UILabel。这就如预期的那样删去了正文。后来我通过编程增加了宽度,我可以看到整个文本。这里必须有一个解决方案。为什么在我添加templeblframe.size.width=\u lblLongText.intrinsicContentSize.width代码>不更改原点或文本对齐方式。我不知道您在哪里设置contentSize,但我猜这是因为默认情况下UILabel将文本居中。因此,如果contentSize设置得非常大,它将向右移动(以居中)。很抱歉,Darshan,但我似乎不理解代码。什么是变量-“滚动”,“俱乐部”?还要记住,我的XIB中已经有UILabel。因此,我编写了代码,从主视图中提取它们,并将它们添加到临时子视图的顶部以进行滚动。因此,我希望避免[[UILabel alloc]init]
此代码用于自定义生成标签和该标签加载项在滚动视图中使用我的clubarray自动生成。因此我建议您使用动态标签。谢谢
//Add label to UIView at 0,0 wrt to new UIView
tempLblFrame.origin.x = 0;
tempLblFrame.origin.y = 0;
tempLblFrame.size.width = _lblLongText.intrinsicContentSize.width; //THIS LINE WAS ADDED
[_lblLongText setFrame:tempLblFrame];
[_lblLongText removeFromSuperview];
[lblView addSubview:_lblLongText];
//Make UIView for Label to sit in
CGRect tempLblFrame = _lblLongText.frame;
UIView *lblView = [[UIView alloc] initWithFrame:tempLblFrame];
//#CHANGE 1 Removing all constraints
[_lblLongText removeConstraints:_lblLongText.constraints];
//Add label to UIView at 0,0 wrt to new UIView
tempLblFrame.origin.x = 0;
tempLblFrame.origin.y = 0;
//Set Full length of Label so that complete text shows (else only truncated text will scroll)
tempLblFrame.size.width = _lblLongText.intrinsicContentSize.width;
//#CHANGE 2 setting fresh constraints using the frame which was manually set
[_lblLongText setTranslatesAutoresizingMaskIntoConstraints :YES];
[_lblLongText setFrame:tempLblFrame];
[_lblLongText removeFromSuperview];
[lblView addSubview:_lblLongText];
scroll.contentSize =CGSizeMake(100 *[clubArray count],20);
NSString *bname;
bname=@"";
for(int i = 0; i < [clubArray count]; i++)
{
bname = [NSString stringWithFormat:@"%@ %@ ,",bname,[[clubArray objectAtIndex:i] objectForKey:@"bottle_name"]];
[bname retain];
}
UILabel *lbl1 = [[UILabel alloc] init];
[lbl1 setFrame:CGRectMake(0,5,[clubArray count]*100,20)];
lbl1.backgroundColor=[UIColor clearColor];
lbl1.textColor=[UIColor whiteColor];
lbl1.userInteractionEnabled=YES;
[scroll addSubview:lbl1];
lbl1.text= bname;