use*_*025 7 objective-c multiline nsattributedstring uilabel ios6
我在UILabel工作过.但是不推荐使用setLineBreakMode.我一直在使用NSAttributedString.但UILabel setLineBreakMode是之后UILabel setNumberOfLines else不起作用.
之前:
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(42.0f, 10.0f, 275.0f, 50.0f)];
label.text = @"XXXXXX";
memoLabel.textAlignment = UITextAlignmentLeft;
memoLabel.numberOfLines = 2;
memoLabel.lineBreakMode = UILineBreakModeTailTruncation;
memoLabel.font = [UIFont systemFontOfSize:11];
memoLabel.backgroundColor = [UIColor clearColor];
Run Code Online (Sandbox Code Playgroud)
IOS 6之后:
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
paragraphStyle.alignment = NSTextAlignmentLeft;
NSAttributedString *string
= [[NSAttributedString alloc] initWithString:text
attributes:[NSDictionary
dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:11],
NSFontAttributeName,
paragraphStyle, NSParagraphStyleAttributeName,nil]];
[paragraphStyle release];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(42.0f, 10.0f, 275.0f, 50.0f)];
label.attributedText = string;
[string relase];
Run Code Online (Sandbox Code Playgroud)
我希望在显示之前和之后都一样.如何显示多行?
该lineBreakMode
属性在 iOS 6 中并未被弃用。它只是更改了常量的名称。旧常量已弃用,但仍然可用。即使您要部署到较旧的 iOS,也可以使用新常量,因为常量只是枚举值。旧名称和新名称具有相同的值。所以,只需设置memoLabel.lineBreakMode = NSLineBreakByTruncatingTail
.
您的示例代码似乎没有利用任何归因于字符串的特定功能。如果您不需要属性字符串,只需继续使用纯字符串即可。这在 iOS 6 中仍然有效。
归档时间: |
|
查看次数: |
15583 次 |
最近记录: |