【问题标题】:putting words of a sentence into different transparent labels when a button is clicked单击按钮时将句子中的单词放入不同的透明标签中
【发布时间】:2013-06-18 02:03:08
【问题描述】:

我有一个存储在 NSMutablesstring 中的句子,我有一个按钮,当点击时,它将句子单词加载到标签中,例如,以“我是男孩”为例,当单击按钮时,它将“i”加载到一个标签中,“am”加载到另一个标签中,“a”加载到另一个标签中,“boy”加载到另一个标签中。

【问题讨论】:

  • 你在问什么...?
  • @David 将句子中的单词分开并在单击按钮时将它们放入标签中

标签: ios objective-c uilabel nsmutablestring


【解决方案1】:
NSArray* labels = [@"i am a boy" componentsSeparatedByString: @" "];
CGFloat xOffset = 0.0;
for NSString *labeltext in labels
   UILabel *wordLabel = [[UILabel alloc] initWithFrame:CGRectMake(xOffset,0,300,40)];
   wordLabel.text  = word;
   xOffset+=200;
   [self.view addSubview:wordLabel];

【讨论】:

    【解决方案2】:

    您可以将“我是男孩”拆分为数组。

    NSString* example = @"I am a boy";
    NSArray *arr = [example componentsSeparatedByCharactersInSet:
              [NSCharacterSet characterSetWithCharactersInString:@" "]];
    

    然后,您可以将 Text 设置为 UILabel

    http://developer.apple.com/library/ios/#documentation/cocoa/reference/foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/cl/NSString

    【讨论】:

      【解决方案3】:
      NSString *sentence = @"i am a boy";
      NSMutableArray *words = [sentence componentsSeparatedByString:@" "];
      
      CGFloat xVal = 0.0;
      
      for( NSString *word in words ) {
      
          UILabel *wordLabel = [[UILabel alloc] initWithFrame:CGRectMake(xVal,0,200,40)];
          wordLabel.backgroundColor = [UIColor clearColor];
          wordLabel.textColor = [UIColor blackColor];
          wordLabel.text  = word;
          xVal+=200;
      
          [self.view addSubview:wordLabel];
      }
      

      【讨论】:

      • 这不起作用,当我单击包含此方法的按钮时应用程序崩溃
      猜你喜欢
      • 2016-04-30
      • 2020-06-25
      • 2021-06-09
      • 2020-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-04
      • 1970-01-01
      相关资源
      最近更新 更多