2d tagcloud
37884 단어 cloud
-(void)loadTags {
NSAutoreleasePool*pool =[[NSAutoreleasePool alloc] init];
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
// Create the request.
MindzAppDelegate*delegate=[[UIApplication sharedApplication]delegate];
NSString*theUrl =[[NSString alloc] initWithFormat:@"%@/WikiService.asmx/%@?path=%@&apiKey=%@&userKey=%@&plazaId=%i&form=json",
delegate.hostUrl,
@"GetWikiTagOverview",
delegate.wikiUrl,
delegate.apiKey,
delegate.profileKey,
delegate.plazaId
];
NSURL *tagURL =[[NSURL alloc] initWithString:theUrl];
NSString*content =[[NSString alloc] initWithContentsOfURL:tagURL];
[theUrl release];
[tagURL release];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
if([content length]==0){
UIAlertView*myAlert =[[UIAlertView alloc]
initWithTitle:
NSLocalizedString
(@"ConnectionError",
@"Alert the user that The connection failed")
message:NSLocalizedString
(@"ConnectionError",
@"Alert the user that The connection failed")
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[myAlert show];
[myAlert release];
loadingSpinner.hidden = TRUE;
loadingLabel.hidden = TRUE;
}
else{
SBJsonParser*parser =[[SBJsonParser alloc] init];
NSDictionary*json =[parser objectWithString:content];
[parser release];
[content release];
//random tagcloud
NSArray*theArray =[[json objectForKey:@"Results"] shuffled];
self.contentArray =[NSArray arrayWithArray:theArray];
//first scroll contraint
scrollYConstraint =550;
scrollLastUsed =0;
[self showTags];
}
[pool release];
}
-(void)showTags {
NSAutoreleasePool*pool =[[NSAutoreleasePool alloc] init];
int tag =1;
//max width to go down with the button (portrait iphone screen)
int xConstraint =300;
int yConstraint =0;
for(NSDictionary*theRow inself.contentArray){
NSString*theTitle =[theRow objectForKey:@"Name"];
if(tag > scrollLastUsed){
float x =10.0f;
float y = scrollYConstraint -540;
//between 15 and 50
float fontSize =(arc4random()%50)+15;
for(UIButton*btn in tagsArray){
CGSize theSize =[theTitle sizeWithFont:[UIFont boldSystemFontOfSize:fontSize] constrainedToSize:CGSizeMake(9999.0f,9999.0f) lineBreakMode:UILineBreakModeWordWrap];
//not too wide?
while(theSize.width > xConstraint){
fontSize -=1;
theSize =[theTitle sizeWithFont:[UIFont boldSystemFontOfSize:fontSize] constrainedToSize:CGSizeMake(9999.0f,9999.0f) lineBreakMode:UILineBreakModeWordWrap];
}
CGRect compareRect =CGRectMake(x, y, theSize.width +10, theSize.height);
while([self rectIntersects:compareRect]){
if((theSize.width + x +5)< xConstraint) x +=15;
else{
x =10;
y +=12;
}
compareRect =CGRectMake(x, y, theSize.width, theSize.height);
}
}
[self.tagScrollView addSubview:[self addLabelWithTitle:theTitle andFontSize:fontSize WithX:x AndY:y AndTag:tag]];
//get the button height and add it to the constraint
int btnHeight =(int)[self.tagScrollView viewWithTag:tag].frame.size.height;
if((y + btnHeight)> yConstraint) yConstraint =(y + btnHeight);
}
//stop if we reached the wanted constraint
if(yConstraint > scrollYConstraint)break;
//otherwise, next tag
tag++;
}
//resize scrollview
self.tagScrollView.contentSize =CGSizeMake(320, yConstraint+10);
//remember the last used tag
scrollLastUsed = tag;
//hide loaders
loadingSpinner.hidden = TRUE;
loadingLabel.hidden = TRUE;
UIActivityIndicatorView*theSpinner =(UIActivityIndicatorView*)[self.tagScrollView viewWithTag:99999];
if(theSpinner)[theSpinner removeFromSuperview];
//show scrollview
tagScrollView.hidden = FALSE;
loadingNewTags = FALSE;
[pool release];
}
-(UIButton*)addLabelWithTitle:(NSString*)theTitle andFontSize:(float)fontSize WithX:(float)x AndY:(float)y AndTag:(NSInteger)tag {
MindzAppDelegate*delegate=[[UIApplication sharedApplication]delegate];
UIButton*theBtn =[UIButton buttonWithType:UIButtonTypeCustom];
//get width for the label
CGSize theSize =[theTitle sizeWithFont:[UIFont boldSystemFontOfSize:fontSize] constrainedToSize:CGSizeMake(9999.0f,9999.0f) lineBreakMode:UILineBreakModeWordWrap];
theBtn.frame =CGRectMake(x, y, theSize.width, theSize.height);
//save the button in an array to check whether it overlaps with other buttons
[self.tagsArray addObject:theBtn];
[theBtn setTitle:theTitle forState:UIControlStateNormal];
UIColor*titleColor =[UIColor blackColor];
[theBtn setTitleColor:titleColor forState:UIControlStateNormal];
theBtn.titleLabel.font =[UIFont boldSystemFontOfSize:fontSize];
theBtn.tag = tag;
[theBtn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
CGRect frame = theBtn.frame;
frame.size.width +=10;//l + r padding
theBtn.frame = frame;
// add action
[theBtn addTarget:self action:@selector(tagPressed:) forControlEvents:UIControlEventTouchUpInside];
return theBtn;
}
-(BOOL)rectIntersects:(CGRect)theRect {
int t =0;
for(UIButton*btn in tagsArray){
t++;
if(t >=(scrollLastUsed -10)){
CGRect testRect =CGRectIntersection(btn.frame, theRect);
if(!CGRectIsNull(testRect))return TRUE;
}
}
return FALSE;
}
#pragma mark -
#pragma mark ScrollViewDelegate
-(void)scrollViewDidScroll:(UIScrollView*)scrollView {
if(scrollView.contentOffset.y >(scrollYConstraint -400)&&self.scrollLastUsed <[self.contentArray count]&&!loadingNewTags){
loadingNewTags = TRUE;
//add a spinner
UIActivityIndicatorView*extraSpinner =[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
extraSpinner.tag =99999;
extraSpinner.frame =CGRectMake(150,(int)scrollYConstraint +20,20,20);
[extraSpinner startAnimating];
self.tagScrollView.contentSize =CGSizeMake(320,(int)scrollYConstraint +50);
[self.tagScrollView addSubview:extraSpinner];
//new contraint
scrollYConstraint = scrollYConstraint +550;
//load new tags
[NSThread detachNewThreadSelector:@selector(showTags) toTarget:self withObject:nil];
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
노드 및 Express AWS Lambda 함수 생성첫 번째 AWS Lambda 함수를 배포하기 전에 Serverless 및 AWS Lambda에 대해 조금 이야기하겠습니다. 이것에 대해 이미 많이 알고 있다면 다음 부분으로 건너뛸 수 있습니다. AWS Lambda는...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.