簡體   English   中英

在UITextView中創建一個UIbutton和UIImageview

[英]Create a UIbutton and UIImageview in UITextView

我的屏幕上有一個UITextview,我在屏幕上的拖放操作中創建了textview,現在我想在textview的最后(文本視圖中的文本結尾)添加按鈕,然后該怎么做?

我一直在嘗試

[super viewDidLoad];
 SelectInvestiList = [[NSMutableArray alloc] initWithCapacity:[InvestiList count]]; 


AppDelegate *appDeleg = (AppDelegate *)[[UIApplication sharedApplication]delegate];

textViewInvest.text=nil;

UIButton*button=[UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(0, 0, 30, 30)];
[button setTitle:@"test" forState:UIControlStateNormal];
[textViewInvest addSubview:button];

NSMutableString *prev=[[NSMutableString alloc] init];
if (appDeleg.chiefCompText != nil) {
    prev=(NSMutableString *) textViewInvest.text;
    [prev appendString:(NSMutableString *) appDeleg.chiefCompText];
    textViewInvest.text=[prev capitalizedString];

}

if (appDeleg.vitalText != nil) {
    prev=(NSMutableString *)textViewInvest.text;
    [prev appendString:(NSMutableString *) appDeleg.vitalText];
    textViewInvest.text=[prev capitalizedString];

}

但這不起作用..

在此處輸入圖片說明

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [btn setFrame:CGRectMake(0, 0, 10, 20)];
        [btn setTitle:@"Ram" forState:UIControlStateNormal];
        //[self.view addSubview:btn];
        [textview addSubview:btn];
set the button frame according to your text in textview.

我不確定您是否正在使用“界面構建器”。

你可以試試看

UIButton *myButton  =   [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton.frame      =   CGRectMake(0.0, 100.0, 50.0, 30.0);
[yourTextView addSubview:myButton];

我猜您在通過代碼添加時應該提到按鈕的type 我只是用它來工作:

UIButton*button=[UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(0, 0, 30, 30)];
[button setTitle:@"test" forState:UIControlStateNormal];
[yourTextView addSubview:button];

看看吧,親愛的,它總是對我有用。

for (id view in [textView subviews]) {
        if ([view isKindOfClass:[UIScrollView class]]) {
            UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0,0,100,35)];
            [view addSubview:btn];
        }
    }

試試這個代碼。 在這里我也創建textview。 這對我來說很好。

UITextView *yourTextView = [[UITextView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 400.0)];
yourTextView.backgroundColor    =   [UIColor grayColor];
[self.view addSubview:yourTextView];

UIButton *myButton  =   [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton.frame      =   CGRectMake(0.0, 100.0, 50.0, 30.0);
[myButton setTitle:@"GO" forState:UIControlStateNormal];
[yourTextView addSubview:myButton];

它會工作。

 for (id view in [textView subviews]) {
            if ([view isKindOfClass:[UIScrollView class]]) {
                UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
                [btn setFrame:CGRectMake(0, 0, 10, 20)]; 
                [btn setTitle:@"Ram" forState:UIControlStateNormal];

                [view addSubview:btn];
            }
        }

暫無
暫無

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

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