2012-04-22 92 views
0

我有这样的代码,以显示从SQLite的DB一个TextView一些文字..追加的UIButton用的NSString

NSMutableString *combined = [NSMutableString string]; 
for(NSUInteger idx = 0; idx < [delegate.allSelectedVerseEnglish count]; idx++) { 
    [combined appendFormat:@" %d %@", idx, [delegate.allSelectedVerseEnglish objectAtIndex:idx]]; 
} 

self.multiPageView.text = combined; 
self.multiPageView.font = [UIFont fontWithName:@"Georgia" size:self.fontSize]; 

delegate.allSelectedVerseEnglishNSArray multiPageViewUITextView

我把上面的循环功能根据文本获得Numbers,例如1 hello 2 iPhone 3 iPad 4 mac etc etc..我只想UIButton之间的文字,而不是1 2 3 4 ..例如我想unbutton hello unbutton iPhone etc etc。因为我需要使一些触摸事件从它做到这一点? 在此先感谢。

+1

你不能把按钮内嵌在文本视图。如果这是对你的要求,请考虑使用“UIWebView”。 – 2012-04-22 16:27:18

+0

你不能将UIButton实例存储在NSString中,你需要将按钮作为子视图添加到滚动视图 – Felix 2012-04-22 16:27:49

+0

你能说清楚你想实现什么吗?文本视图允许用户编辑文本,是你想要的吗?其他评论者认为你想添加按钮作为文本的子视图,是吗?请从用户角度尝试从功能上描述目标。 – danh 2012-04-22 16:50:30

回答

1

如果你想在文本视图中的某些文本之间放置UIButton,除了将它作为单独的视图放在上面之外别无他法。因此,您需要在这些按钮下方添加空格,根据按钮的大小计算您自己应该计算的数量。 所以,如果你想哟看到这样的事情:

Press here [UIButton] or here [Another UIButton], 

文本字符串应该是这样的

Press here   or here     , 

所以,当你添加上这些地方的按钮,它看起来就像你希望。

UPDATE

好像我们在这里需要一些更多的代码,所以这里是: 首先,你需要计算一个字母的大小。我们假设它是10像素高度和8像素。不,我们只是称之为letterHeightletterWidth。我们还假设您需要64x10像素按钮。所以,我们需要8分之64= 8 + 2空间,以淘汰落后该按钮(2,使边框周围) 所以,在这里我们去

NSMutableString *combined = [NSMutableString string]; 
int letterHeight = 10; 
int letterWidth = 8; 
    for(NSString *verse in delegate.allSelectedVerseEnglish) { 
     [combined appendFormat:@"   %@",verse];//10 spaces there 
//You have to experiment with this, but idea is that your x coordinate is just proportional to the length of the line you are inserting your button in, and y is proportional to number of lines 
    int xCoordinate = [combined length]*letterWidth%(int)(self.multiPageView.frame.size.width); 
    int yCoordinate = [combined length]*letterWidth/(int)(self.multiPageView.frame.size.width)*letterHeight; 

    UIButton *newButton = [[UIButton alloc]initWithFrame:CGRectMake(xCoordinate,yCoordinate,64,10)]; 
    [self.multiPageView addSubview:newButton]; 
} 
self.multiPageView.text = combined; 
+0

你correct.but如何循环这个按钮,而不是数字.. – stackiphone 2012-04-22 17:18:54

+0

你循环相同的方式,每次添加UIButton到该视图,而不是硬,真的 – 2012-04-22 17:21:08

+0

我可以告诉你一个代码,动态构建与按钮的coloumview 。 – stackiphone 2012-04-22 17:24:08