Ios 带文本的xcode不可见按钮
我知道我可以做到:Ios 带文本的xcode不可见按钮,ios,objective-c,xcode,uibutton,Ios,Objective C,Xcode,Uibutton,我知道我可以做到: UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; button.frame = CGRectMake(x, y, width, height); [button setTitle:text forState:UIControlStateNormal]; [button addTarget:self action:@selector(buttonPressed:)
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(x, y, width, height);
[button setTitle:text forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
button.backgroundColor = [UIColor clearColor];
但这在按钮周围留下了一个边界
我也可以这样做:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(x, y, width, height);
[button setTitle:text forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
然后在按钮后面贴上标签
有没有一种方法可以在不需要两个对象的情况下完成此任务?
更新:
为了澄清我的问题:
我想要有文本,只有文本,按下时执行方法调用。由于解释起来很复杂,我需要通过一个按钮来实现这一点。那么,如何使按钮的文本成为按钮唯一可见的部分呢。没有边界。没有背景。只发短信
UIButton *btnLikeUserCount = [[[UIButton alloc] init] autorelease];
[btnLikeUserCount.titleLabel setFont:[UIFont boldSystemFontOfSize:12]];
btnLikeUserCount.frame = CGRectMake(posx,posy,width,height);
[btnLikeUserCount setTitle:text forState:UIControlStateNormal];
[btnLikeUserCount setTitleColor:[UIColor colorWithRed:50/255.0 green:79/255.0 blue:133/255.0 alpha:1.0] forState:UIControlStateNormal];
[btnLikeUserCount addTarget:self action:@selector(btnLikeUserCountClick:) forControlEvents:UIControlEventTouchUpInside];
不需要使用标签对象
无需获取标签对象。我无法准确获取您想要的内容,但您的代码中可能会遗漏您的
[self.view addSubview:button]
因此您不能显示带有文本的按钮
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(x, y, width, height);
[button setTitle:text forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
我无法准确地获取您想要的内容,但您的代码中可能会遗漏您的
[self.view addSubview:button]
因此您不能显示带有文本的按钮
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(x, y, width, height);
[button setTitle:text forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
您的第二个代码运行良好。在下图中,hello是一个按钮。 我的代码是:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(110, 100, 100, 50);
[button setTitle:@"Hello" forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
您的第二个代码运行良好。在下图中,hello是一个按钮。 我的代码是:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(110, 100, 100, 50);
[button setTitle:@"Hello" forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
这样你就可以做到这一点
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(100, 400, 100, 50);
[button setTitle:@"Button text" forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[button.layer setBorderColor: [UIColor clearColor]];
[button.layer setBorderWidth: 0.0];
[self.view addSubview:button];
这样你就可以做到这一点
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(100, 400, 100, 50);
[button setTitle:@"Button text" forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[button.layer setBorderColor: [UIColor clearColor]];
[button.layer setBorderWidth: 0.0];
[self.view addSubview:button];
您也只想隐藏边框或背景??您的问题是什么?-->是否需要边界?没错,你们的问题有点让人困惑。明确你想要什么?什么都不需要,只需要文本。。。只有文本和动作outlet@Hoodai-你想要什么。。。??????如果您需要仅显示文本,则UIButtonTypeCustom无需在按钮上添加UILabel:)您也只想隐藏边框或背景??您的问题是什么?-->是否需要边界?没错,你们的问题有点让人困惑。明确你想要什么?什么都不需要,只需要文本。。。只有文本和动作outlet@Hoodai-你想要什么。。。??????如果您需要仅显示文本,则UIButtonTypeCustom无需在按钮上添加UILabel:)