是否有办法在UILabel
中像UITextView
那样有多行文字,或者我应该用第二行来代替?
我找到了一个解决方案。
人们只需添加以下代码。
// Swift
textLabel.lineBreakMode = .ByWordWrapping // or NSLineBreakMode.ByWordWrapping
textLabel.numberOfLines = 0
// For Swift >= 3
textLabel.lineBreakMode = .byWordWrapping // notice the 'b' instead of 'B'
textLabel.numberOfLines = 0
// Objective-C
textLabel.lineBreakMode = NSLineBreakByWordWrapping;
textLabel.numberOfLines = 0;
// C# (Xamarin.iOS)
textLabel.LineBreakMode = UILineBreakMode.WordWrap;
textLabel.Lines = 0;
恢复了旧的答案(供参考和愿意支持6.0以下的iOS的开发者)。
textLabel.lineBreakMode = UILineBreakModeWordWrap;
textLabel.numberOfLines = 0;
话说回来,这两个枚举值都是 "0"。
我找到的最好的解决方案(对于一个本来应该在框架中解决的令人沮丧的问题)与vaychick'的类似。
只要在IB或代码中把行数设置为0即可。
myLabel.numberOfLines = 0;
这将显示所需的行,但会重新定位标签,使其水平居中(因此,1行和3行标签在其水平位置对齐)。 要解决这个问题,请添加
CGRect currentFrame = myLabel.frame;
CGSize max = CGSizeMake(myLabel.frame.size.width, 500);
CGSize expected = [myString sizeWithFont:myLabel.font constrainedToSize:max lineBreakMode:myLabel.lineBreakMode];
currentFrame.size.height = expected.height;
myLabel.frame = currentFrame;
textLabel.lineBreakMode = UILineBreakModeWordWrap;
textLabel.numberOfLines = 0;
上面的解决方案在我的情况下并不奏效。我是这样做的。
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
// ...
CGSize size = [str sizeWithFont:[UIFont fontWithName:@"Georgia-Bold" size:18.0] constrainedToSize:CGSizeMake(240.0, 480.0) lineBreakMode:UILineBreakModeWordWrap];
return size.height + 20;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
// ...
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.textLabel.numberOfLines = 0;
cell.textLabel.font = [UIFont fontWithName:@"Georgia-Bold" size:18.0];
}
// ...
UILabel *textLabel = [cell textLabel];
CGSize size = [text sizeWithFont:[UIFont fontWithName:@"Georgia-Bold" size:18.0]
constrainedToSize:CGSizeMake(240.0, 480.0)
lineBreakMode:UILineBreakModeWordWrap];
cell.textLabel.frame = CGRectMake(0, 0, size.width + 20, size.height + 20);
//...
}
Swift 3
将动态文字信息的行数设置为零,对于变化的文字会有一定的帮助。
var label = UILabel()
let stringValue = "A label\nwith\nmultiline text."
label.text = stringValue
label.numberOfLines = 0
label.lineBreakMode = .byTruncatingTail // or .byWrappingWord
label.minimumScaleFactor = 0.8 . // It is not required but nice to have a minimum scale factor to fit text into label frame
使用故事栏 。 选择标签,将行数设置为零......[或参考这个][2] 。
[![在此输入图像描述][1]][1]
[1]: http://i.stack.imgur.com/CYBQL.png [2]: https://stackoverflow.com/questions/446405/adjust-uilabel-height-depending-on-the-text?rq=1
试试用这个。
lblName.numberOfLines = 0;
[lblName sizeToFit];
UILabel *helpLabel = [[UILabel alloc] init];
NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:label];
helpLabel.attributedText = attrString;
// helpLabel.text = label;
helpLabel.textAlignment = NSTextAlignmentCenter;
helpLabel.lineBreakMode = NSLineBreakByWordWrapping;
helpLabel.numberOfLines = 0;
由于某些原因,它不能为我在iOS 6不知道为什么。 尝试了有和没有归属文字。 任何建议。
你应该试试这个。
-(CGFloat)dynamicLblHeight:(UILabel *)lbl
{
CGFloat lblWidth = lbl.frame.size.width;
CGRect lblTextSize = [lbl.text boundingRectWithSize:CGSizeMake(lblWidth, MAXFLOAT)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:@{NSFontAttributeName:lbl.font}
context:nil];
return lblTextSize.size.height;
}
这些东西帮助我
改变UILabel的这些属性
label.numberOfLines = 0;
label.adjustsFontSizeToFitWidth = NO;
label.lineBreakMode = NSLineBreakByWordWrapping;
当输入字符串时,使用\n在不同的行中显示不同的单词。
例如 :
NSString *message = @"This \n is \n a demo \n message for \n stackoverflow" ;
方法1:。
extension UILabel {//Write this extension after close brackets of your class
func lblFunction() {
numberOfLines = 0
lineBreakMode = .byWordWrapping//If you want word wraping
//OR
lineBreakMode = .byCharWrapping//If you want character wraping
}
}
现在就这样简单的调用
myLbl.lblFunction()//Replace your label name
EX:
Import UIKit
class MyClassName: UIViewController {//For example this is your class.
override func viewDidLoad() {
super.viewDidLoad()
myLbl.lblFunction()//Replace your label name
}
}//After close of your class write this extension.
extension UILabel {//Write this extension after close brackets of your class
func lblFunction() {
numberOfLines = 0
lineBreakMode = .byWordWrapping//If you want word wraping
//OR
lineBreakMode = .byCharWrapping//If you want character wraping
}
}
方法2:
程序上
yourLabel.numberOfLines = 0
yourLabel.lineBreakMode = .byWordWrapping//If you want word wraping
//OR
yourLabel.lineBreakMode = .byCharWrapping//If you want character wraping
方法3:
通过故事板
要显示多行,设置0(Zero),这将在标签中显示多行。
如果您想显示n行,请设置n。
请看下面的画面。
[![在此输入图像描述][1]][1] 。
如果您想设置标签的最小字体大小,请点击自动收缩并选择最小字体大小选项。
请看下面的屏幕
[![在此输入图像描述][2]][2]
这里设置最小字体大小
EX.9 (在此图中) 9 (本图)
如果您的标签获得更多的文字,此时您的标签文字将被缩小到9
[![在此输入图像描述][3]][3]
[1]: https://i.stack.imgur.com/HHEm6.png [2]: https://i.stack.imgur.com/hGDFi.png [3]: https://i.stack.imgur.com/cHoi7.png