2012-04-22 18 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