Ily*_*ski 438 line-breaks multiline uitextview uilabel ios
有没有办法让多行文本在UILabel中,UITextView或者我应该使用第二行?
Ily*_*ski 778
我找到了解决方案.
只需添加以下代码:
// 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;
Run Code Online (Sandbox Code Playgroud)
恢复旧答案(供参考,开发人员愿意支持iOS 6.0以下):
textLabel.lineBreakMode = UILineBreakModeWordWrap;
textLabel.numberOfLines = 0;
Run Code Online (Sandbox Code Playgroud)
在旁边:0无论如何,两个枚举值都会产生.
Ken*_*ner 138
在IB中,将行数设置为0(允许无限行)
使用IB在文本字段中键入时,使用"alt-return"插入一个返回并转到下一行(或者您可以复制已经由行分隔的文本).
小智 52
我发现的最佳解决方案(对于本应在框架中解决的令人沮丧的问题)与vaychick类似.
只需在IB或代码中将行数设置为0即可
myLabel.numberOfLines = 0;
Run Code Online (Sandbox Code Playgroud)
这将显示所需的行,但会重新定位标签,使其水平居中(以便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;
Run Code Online (Sandbox Code Playgroud)
小智 32
使用此选项可以包含多行文本UILabel:
textLabel.lineBreakMode = NSLineBreakByWordWrapping;
textLabel.numberOfLines = 0;
Run Code Online (Sandbox Code Playgroud)
迅速:
textLabel.lineBreakMode = .byWordWrapping
textLabel.numberOfLines = 0
Run Code Online (Sandbox Code Playgroud)
Gur*_*gam 20
myUILabel.numberOfLines = 0;
myUILabel.text = @"your long string here";
[myUILabel sizeToFit];
Run Code Online (Sandbox Code Playgroud)
Tim*_*yer 15
如果你必须使用:
myLabel.numberOfLines = 0;
Run Code Online (Sandbox Code Playgroud)
您还可以("\n")在代码中使用标准换行符来强制换行.
pra*_*jul 14
您可以使用\r转到下一行,同时填补了UILabel使用NSString.
UILabel * label;
label.text = [NSString stringWithFormat:@"%@ \r %@",@"first line",@"seconcd line"];
Run Code Online (Sandbox Code Playgroud)
use*_*910 12
试试看吧
textLabel.lineBreakMode = NSLineBreakModeWordWrap; // UILineBreakModeWordWrap deprecated
textLabel.numberOfLines = 0;
Run Code Online (Sandbox Code Playgroud)
bar*_*rit 10
textLabel.lineBreakMode = UILineBreakModeWordWrap;
textLabel.numberOfLines = 0;
Run Code Online (Sandbox Code Playgroud)
上面的解决方案在我的情况下不起作用.我这样做:
- (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);
//...
}
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)

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;
Run Code Online (Sandbox Code Playgroud)
由于某些原因,它在iOS 6中不适合我,不知道为什么.尝试使用和不使用归属文本.有什么建议.
方法一:
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
}
}
Run Code Online (Sandbox Code Playgroud)
现在就这样调用
myLbl.lblFunction()//Replace your label name
Run Code Online (Sandbox Code Playgroud)
前任:
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
}
}
Run Code Online (Sandbox Code Playgroud)
方法二:
以编程方式
yourLabel.numberOfLines = 0
yourLabel.lineBreakMode = .byWordWrapping//If you want word wraping
//OR
yourLabel.lineBreakMode = .byCharWrapping//If you want character wraping
Run Code Online (Sandbox Code Playgroud)
方法三:
通过故事板
要显示设置为 0(零)的多行,这将在您的标签中显示多于一行。
如果要显示 n 行,则设置 n。
请参阅下面的屏幕。
如果要设置标签的最小字体大小单击自动收缩并选择最小字体大小选项
请参阅以下屏幕
这里设置最小字体大小
例如:9(在这张图片中)
如果您的标签当时获得更多文本,您的标签文本将缩小至 9
| 归档时间: |
|
| 查看次数: |
354048 次 |
| 最近记录: |