0
我是iOS新手,面臨滾動標籤文本的問題。我的代碼能夠滾動標籤文本,但它的寬度沒有設置正常。代碼是這樣的如何滾動UIlabel文本,並在目標中動態設置它的寬度和高度c
NSString * htmlString = @"<html><body>";
NSString *[email protected]"</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 *[email protected]"</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
@Joshua是的,我已經嘗試過。 – Muju
@Muju我建議使用'UITextView'而不是'UILabel',它允許你滾動文本,並在HTML屬性文本允許點擊超鏈接。 – kamwysoc