0

我有这段代码可以在 SQLite DB 的 textview 中显示一些文本。

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 multiPageView不是UITextView

我使用上面的循环函数来根据文本获取数字,例如1 hello 2 iPhone 3 iPad 4 mac etc etc..我只想要文本之间的UIButton而不是1 2 3 4 ..例如我想要 的。unbutton hello unbutton iPhone etc etc因为我需要从中进行一些触摸事件。如何做到这一点?提前致谢。

4

1 回答 1

1

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

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

您的文本字符串应如下所示

Press here            or here                   ,

因此,当您在这些位置添加按钮时,它看起来就像您希望的那样。

更新

似乎我们需要更多的代码,所以这里是:首先,你需要计算一个字母的大小。让我们假设它是 10 像素高度和 8 像素。不,让我们称之为letterHeightletterWidth。我们还假设您想要 64x10 像素的按钮。所以,我们需要 64/8=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;
于 2012-04-22T17:08:25.973 回答