如何滚动 UIlabel 文本并在目标 c 中动态设置它的宽度和高度
Posted
技术标签:
【中文标题】如何滚动 UIlabel 文本并在目标 c 中动态设置它的宽度和高度【英文标题】:How to scroll UIlabel Text and set it width and Hight dynamically in objective c 【发布时间】:2016-10-26 09:47:47 【问题描述】:我是 ios 新手,滚动标签文本时遇到问题。我的代码能够滚动标签文本,但宽度设置不正确。代码是这样的
NSString * htmlString = @"<html><body>";
NSString *htmlString2=@"</body></html>";
NewString=[NSString stringWithFormat:@"%@%@%@",htmlString,result,htmlString2];
NSLog(@"New String =%@",NewString);
NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[NewString dataUsingEncoding:NSUnicodeStringEncoding] options:@ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType documentAttributes:nil error:nil];
NSLog(@"String Value = %@",result);
// [myDataNSMArray addObject:idarray];
shortnamearray=[[NSMutableArray alloc]init];
shortnamearray=[responsedict valueForKey:@"abc"];
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
lbl = [[UILabel alloc]initWithFrame:CGRectMake(0,0,700, 1800)];
NSString *theText = @"A long string";
CGRect labelRect = CGRectMake(10, 50, 300, 50);
lbl.adjustsFontSizeToFitWidth = YES;
[lbl setNumberOfLines:0];
CGFloat fontSize = 30;
while (fontSize > 0.0)
CGSize size = [theText sizeWithFont:[UIFont fontWithName:@"Verdana" size:fontSize] constrainedToSize:CGSizeMake(labelRect.size.width, 10000) lineBreakMode:NSLineBreakByWordWrapping];
if (size.height <= labelRect.size.height) break;
fontSize -= 1.0;
//set font size
lbl.font = [UIFont fontWithName:@"Arial" size:fontSize];
else
lbl = [[UILabel alloc]initWithFrame:CGRectMake(0,0,350, 800)];
NSLog(@"Result Array =%@",shortnamearray);
CGFloat y = 10;
NSMutableArray* animals = [NSMutableArray new];
NSUInteger maxCount = headarray.count > shortnamearray.count ? headarray.count : shortnamearray.count;
for (int i = 0; i < maxCount; i ++)
if ([headarray objectAtIndex:i])
[animals addObject:[headarray objectAtIndex:i]];
if ([shortnamearray objectAtIndex:i])
[animals addObject:[shortnamearray objectAtIndex:i]];
NSLog(@"Array is =%@",animals);
for(int i=0;i<[shortnamearray count] && i<[headarray count];i++)
// y+=20;
y+=10;
NSString *newArray =[animals objectAtIndex:i];
newArray=[animals componentsJoinedByString:@""];
NSString *NewString;
[lbl setLineBreakMode:NSLineBreakByWordWrapping];
lbl.textAlignment = NSTextAlignmentCenter;
NSString * htmlString = @"<html><body>";
NSString *htmlString2=@"</body></html>";
NewString=[NSString stringWithFormat:@"%@%@%@",htmlString,newArray,htmlString2];
NSLog(@"New String =%@",NewString);
NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[NewString dataUsingEncoding:NSUnicodeStringEncoding] options:@ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType documentAttributes:nil error:nil];
lbl.attributedText = attrStr;
NSString *theText = @"A long string";
CGRect labelRect = CGRectMake(10, 50, 300, 50);
lbl.adjustsFontSizeToFitWidth = YES;
[lbl setNumberOfLines:0];
CGFloat fontSize = 15;
while (fontSize > 0.0)
CGSize size = [theText sizeWithFont:[UIFont fontWithName:@"Verdana" size:fontSize] constrainedToSize:CGSizeMake(labelRect.size.width, 10000) lineBreakMode:NSLineBreakByWordWrapping];
if (size.height <= labelRect.size.height) break;
fontSize -= 1.0;
//set font size
lbl.font = [UIFont fontWithName:@"Arial" size:fontSize];
NSTimer *timer = [NSTimer timerWithTimeInterval:1
target:self
selector:@selector(timer)
userInfo:nil
repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
//[_scrollView scrollRectToVisible:NSMakeRange(0,0,0.0) animated:YES];
[scroll addSubview:lbl];
[UIView commitAnimations];
scroll.contentSize=CGSizeMake(scroll.frame.size.width+[shortnamearray count], lbl.frame.size.height);
我使用的是这样的代码,但它的宽度不是动态设置的。如何动态设置宽度。提前致谢!
【问题讨论】:
你试过 sizeToFit 吗? @Joshua 是的,我试过了。 @Muju 我建议使用UITextView
而不是UILabel
它允许您滚动文本,并且在 html 属性文本中允许单击超链接。
@k8mil 文本应该是自动滚动的。与 HTML 中的 Marque 标记相同。不同的是它是垂直滚动。
@Muju 使用 sizeToFit 后标签的框架是什么?
【参考方案1】:
请使用 UITextView 而不是 UILabel。 您的问题将在最少的代码行中解决。
【讨论】:
以上是关于如何滚动 UIlabel 文本并在目标 c 中动态设置它的宽度和高度的主要内容,如果未能解决你的问题,请参考以下文章
当 UITableView 加载并在滚动后得到修复时,UILabel 向左移动