2d tagcloud

-(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];
       
}
}

转载于:https://www.cnblogs.com/rothwell/archive/2012/04/09/2439714.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值