相关疑难解决方法(0)

Autolayout - UIButton的内在大小不包括标题插入

如果我使用autolayout安排了UIButton,它的大小可以很好地调整以适应其内容.

如果我将图像设置为button.image,则内在大小似乎再次考虑到这一点.

但是,如果我调整titleEdgeInsets按钮的按钮,布局不会考虑到这一点,而是截断按钮标题.

如何确保按钮的固有宽度占据插入?

在此输入图像描述

编辑:

我使用以下内容:

[self.backButton setTitleEdgeInsets:UIEdgeInsetsMake(0, 5, 0, 0)];
Run Code Online (Sandbox Code Playgroud)

目标是在图像和文本之间添加一些分隔.

cocoa-touch uibutton uiview ios autolayout

184
推荐指数
9
解决办法
7万
查看次数

UIButton标题中有两行文字(numberOfLines = 2)

我试图UIButton在其titleLabel中创建一个包含两行文本的文本.这是我正在使用的代码:

    UIButton *titleButton = [[UIButton alloc] initWithFrame:CGRectMake(15, 10, frame.size.width-100, 100)];
    titleButton.titleLabel.font = [UIFont boldSystemFontOfSize:24.0];
    [titleButton setTitle:@"This text is very long and should get truncated at the end of the second line" forState:UIControlStateNormal];
    titleButton.titleLabel.lineBreakMode = UILineBreakModeTailTruncation;
    titleButton.titleLabel.numberOfLines = 2;
    [self addSubview:titleButton];
Run Code Online (Sandbox Code Playgroud)

当我尝试这个时,文本只出现在一行上.似乎实现多行文本的唯一方法UIButton.titleLabel是设置numberOfLines=0和使用UILineBreakModeWordWrap.但这并不能保证文本正好是两行.

UILabel但是,使用plain 可以工作:

    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 10, frame.size.width-100, 100)];
    titleLabel.font = [UIFont boldSystemFontOfSize:24.0];
    titleLabel.text = @"This text is very long and should get truncated at the end of …
Run Code Online (Sandbox Code Playgroud)

objective-c uibutton uilabel ios

79
推荐指数
5
解决办法
7万
查看次数

UILabel中的中心文本

你如何将文本置于UILabel中心?

iphone objective-c

78
推荐指数
3
解决办法
5万
查看次数

在iOS 6中,UITextAlignmentCenter有哪些好的替代方案?

UITextAlignmentCenter 似乎在iOS 6中被弃用了.我的替代方案是什么?

ios6

53
推荐指数
3
解决办法
3万
查看次数

调整文本的字体大小以适合UIButton

我想确保按钮文本适合UIButton,而UIButton具有固定大小.

当然我可以访问UIButton的titleLabel.在标签中,我将设置autoshrink为最小字体比例,似乎对应于

self.myButton.titleLabel.adjustsFontSizeToFitWidth = YES;
Run Code Online (Sandbox Code Playgroud)

,但实际上表现不一样,因为它只使文本水平地适合边界而不是垂直,从而不会改变字体大小.

如何以编程方式实际调整标签的字体大小以使文本适合标签边界(如下图中的 目标 所示)?

adjustsFontSizeToFitWidth实际上并不调整字体大小以适应框架,只调整宽度

我已经试过了

self.myButton.titleLabel.numberOfLines = 0;
self.myButton.titleLabel.minimumScaleFactor = 0.5f;
Run Code Online (Sandbox Code Playgroud)

没有成功,总是像adjustsFontSizeToFitWidth上面的图片左侧那样结束.

编辑:解决方案还必须符合ios7

objective-c uibutton uilabel ios swift

33
推荐指数
4
解决办法
3万
查看次数

在InterfaceBuilder中使用多行titleLabel的UIButton

我一直想知道,在Interface Builder中UIButtons有多种方法可以使用多行titleLabel吗?我发现没有,并且UILabel在每个按钮上单独放置并且在按下时跟踪它的颜色并且其他事件不是非常简单.

如果在IB中没有这样的方式,也许在代码中有更好的替代方案?

cocoa-touch interface-builder uibutton ios

22
推荐指数
1
解决办法
2万
查看次数

UIButton中的多行

嗨我有问题为我的Button设置多行,这是声明的:

button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.titleLabel.font            = [UIFont systemFontOfSize: 12];
button.titleLabel.lineBreakMode   = UILineBreakModeWordWrap;
button.titleLabel.numberOfLines   = 0;
button.titleLabel.shadowOffset    = CGSizeMake (1.0, 0.0);

[button addTarget:self 
           action:@selector(myButtonClick) 
 forControlEvents:UIControlEventTouchDown];

button.frame = CGRectMake(0.0, 100.0, 317.0, 100.0);
[button setTitle:string forState:UIControlStateNormal]; 
button.titleLabel.font            = [UIFont systemFontOfSize: 12];
button.titleLabel.text = @"ahoj";

NSMutableString *ObratString = [[NSMutableString alloc] initWithString:button.titleLabel.text];

[ObratString appendString:@"\n"];
[ObratString appendString:@"caw"];
[ObratString appendString:@"\n"];
[ObratString appendString:@"helllo"];
button.titleLabel.text = ObratString;
[ObratString release];
[self.view addSubview:button];
Run Code Online (Sandbox Code Playgroud)

但最后我才看到第一行.有没有办法使它工作?

xcode objective-c uibutton

10
推荐指数
1
解决办法
2万
查看次数

IOS:使用Autolayout调整UIButton高度取决于标题文本?

我有一个UIButton它可以在运行时更改标题.因此,我想通过使用增加UIButton高度取决于标题文本以显示全文AutoLayout.

我可以UILabel通过设置height constraint为"大于或等于" 来增加高度,但它不起作用UIButton.
我用过[myButton sizeToFit]但它只增加UIButon宽度(不增加高度).

我现在的UIButton属性是
- 约束高度:30 - 领先:15 - 尾随:15 - 顶部:5 - fontsize:12

更新
我为约束高度创建了一个IBOutlet,UIButton用于更改高度@NSNood.
然后我需要\n在标题文本中使用分割线.
但我不知道我应该把它放在哪里\n

这是Button我想要的肖像

在此输入图像描述

这是Button我想要的景观 在此输入图像描述

我该如何确定放置的位置\n

请指导我如何实现它AutoLayout.任何帮助,将不胜感激.

iphone objective-c uibutton ios autolayout

6
推荐指数
2
解决办法
4411
查看次数

以编程方式对UILabel touchUpInside执行操作?

我有一堆UILabel,我通过代码添加,如果用户的手指触及内部,我想为每个执行特定的操作(很像UIButton的IB中的touchUpInside).这样做的最佳方式是什么?

iphone uilabel

4
推荐指数
2
解决办法
7784
查看次数

2
推荐指数
1
解决办法
8329
查看次数

多线uibutton

如何将多行设置为UIButtoniPhone编程中的标题?

cocoa-touch objective-c uibutton ios

-6
推荐指数
1
解决办法
2482
查看次数