簡體   English   中英

如何創建一個可以在按下的特定單詞上產生音頻的字符串

[英]How to create a string that can produce audio on specific word pressed

我在UITextView中有一個長文本,可以使用NSAttributedString和符號進行修改

因此,例如:

"""
if it works like
$I want$
it
will be awesome
"""

“我想要”將以粗體顯示,因為我用$ ... $標記了粗體

我有一個接收字符串並啟動for-in循環的方法。 當找到$符號時,它將開始計算開始索引和結束索引。 完成計數后,我使用:

 attributedString.addAttribute(NSAttributedString.Key.font, value: 
 boldFont, range: NSMakeRange(start, end))

但是現在我死胡同了。 當我敲擊他時,我該如何制作出完全相同的弦以產生聲音。

正如您提到的要在TextView中添加的字符串一樣,您提到要在單擊“我想要”時播放一些聲音,讓我說得更清楚,您需要在字符串中添加鏈接屬性。 讓我給你看一個簡單的例子。 您可以翻譯成迅捷的

  NSString* description = @"if it works like @% it will be awesome";
  NSString* clickAbleStr = @"I want";
  NSString* detail = [NSString stringWithFormat:description,clickAbleStr];

  NSMutableAttributedString *linkStr = [[NSMutableAttributedString alloc]initWithString:detail attributes:nil];
 [linkStr addAttribute:NSLinkAttributeName value:NSLocalizedString(@"http://www.googl.com", @"") range:[detail rangeOfString:clickAbleStr]];
 [linkStr addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:[detail rangeOfString:clickAbleStr]];
 yourtextview.attributedText = linkStr;

 yourtextview.userInteractionEnabled = YES;
 [yourtextview addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)]];

- (void)tapAction:(id)sender {
  // implement tap action for sound
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM