-2

我有一个 UIActionSheet 来更改 UITextView 的字体。我遇到的问题是我添加了另一个 UITextView,现在希望它依赖于打开的 UITextView,(我的意思是打开编辑,显示键盘)为我分配一个或另一个源的类型......不是哪个是 UITextView 的正确属性,以便根据选择区分另一个。

任何的想法?

这是单个 UITextView 的情况。

- (IBAction) displayFontPicker:(id)sender {
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Select a font" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Helvetica", @"Courier", @"Arial", @"Zapfino", @"Verdana", nil];
[actionSheet showFromBarButtonItem:(UIBarButtonItem *)sender animated:YES];

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
NSString *selectedButtonTitle = [actionSheet buttonTitleAtIndex:buttonIndex];
selectedButtonTitle = [selectedButtonTitle lowercaseString];

if ([actionSheet.title isEqualToString:@"Select a font"])
switch (buttonIndex){

    case 0:

        [_textView setFont:[UIFont fontWithName:@"Helvetica" size:25]];

        break;
    case 1:

        [_textView setFont:[UIFont fontWithName:@"Courier" size:25]];

        break;        
}

我看到使用 textview 可以使用多种方法,特别是可以使用它,将在您开始编辑 textview 时运行。

(void)textViewDidBeginEditing:(UITextView *)textView

但我不知道如何在我的代码中实现它......

4

2 回答 2

0

您可以通过设置 setTag 属性为每个 UITextView 应用标签,并且可以区分两者。添加属性以检查哪个 textView 处于可编辑状态

@property NSInteger editabelTextView;

现在分配所有 UITextView 并为每个 UITextView 添加标签。

self.textView1 = [[UITextView alloc] init];
self.textView2 = [[UITextView alloc] init];
self.textView3 = [[UITextView alloc] init];

self.textView1.tag = 1;
self.textView2.tag = 2;
self.textView3.tag = 3;

现在将 textView.tag 分配给 self.editabelTextView

-(void)textViewDidBeginEditing:(UITextView *)textView
{
  self.editabelTextView=textView.tag;
}

现在检查 self.editabelTextView 并相应地设置特定文本视图的 setFont

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex (NSInteger)buttonIndex {
  switch (self.editabelTextView){
  case 1:
      [self.textView1 setFont:[UIFont font...]];
      break;
  case 2:
      [self.textView2 setFont:[UIFont font...]];
      break;
  case 3:
      [self.textView3 setFont:[UIFont font...]]
      break;  
  default:
      break;
  }
}
于 2013-10-21T07:29:53.683 回答
0

添加

@property UITextView *activeTextView;

现在在 textView 代表

-(void)textViewDidBeginEditing:(UITextView *)textView

{
_activeTextView=textView;
}

然后,将 UIActionsheet 委托编辑为

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
NSString *selectedButtonTitle = [actionSheet buttonTitleAtIndex:buttonIndex];
selectedButtonTitle = [selectedButtonTitle lowercaseString];

if ([actionSheet.title isEqualToString:@"Select a font"])
switch (buttonIndex){

    case 0:

        [_activeTextView setFont:[UIFont fontWithName:@"Helvetica" size:25]];

        break;
    case 1:

        [_activeTextView setFont:[UIFont fontWithName:@"Courier" size:25]];

        break;        
}
于 2013-10-21T07:30:34.027 回答