Objective c 如何使用NSMutableAttributeString呈现多行UILabel
我正在尝试使用NSMutableAttributedString创建多行UILabel。 当我将一个属性指定给完整的字符串时,这样做很好:Objective c 如何使用NSMutableAttributeString呈现多行UILabel,objective-c,ios6,uilabel,multiline,nsattributedstring,Objective C,Ios6,Uilabel,Multiline,Nsattributedstring,我正在尝试使用NSMutableAttributedString创建多行UILabel。 当我将一个属性指定给完整的字符串时,这样做很好: UILabel *contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,0,200,100)]; [contentLabel setLineBreakMode:NSLineBreakByWordWrapping]; [contentLabel setNumberOfLines:0]; [cont
UILabel *contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,0,200,100)];
[contentLabel setLineBreakMode:NSLineBreakByWordWrapping];
[contentLabel setNumberOfLines:0];
[contentLabel setFont:[UIFont systemFontOfSize:13];
NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"hello I am a long sentence that should break over multiple lines"];
[string addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:13] range:NSMakeRange(0, [string length])];
contentLabel.attributedText = string;
但我需要的是为NSAttributedString的不同子范围应用一些属性(对于某些粗体字)
我发现,如果我这样做,文本将不再呈现在标签中的多行上。它被渲染为一条直线,在标签的框架中垂直居中
这里缺少什么吗?我经常使用UITextView,并以类似的方式为其分配一个文本属性:
// creiamo textView descrizione
UITextView *description = [[UITextView alloc] init];
description.frame = CGRectMake(20, 453, 500, 190);
NSDictionary *attribute1 = @{NSForegroundColorAttributeName: [UIColor blackColor],
NSBackgroundColorAttributeName: [UIColor clearColor],
NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue-Bold" size:22.0],
};
NSDictionary *attribute2 = @{NSForegroundColorAttributeName: [UIColor blackColor],
NSBackgroundColorAttributeName: [UIColor clearColor],
NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue" size:10],
};
NSDictionary *attribute3 = @{NSForegroundColorAttributeName: [UIColor blackColor],
NSBackgroundColorAttributeName: [UIColor clearColor],
NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue" size:14.0]
};
NSMutableAttributedString *info = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@ %@ %@ \n\n%@ \n ", string1, string2 , string3, string4]];
[info setAttributes:attribute1 range:NSMakeRange(0, ([string1 length]))];
[info setAttributes:attribute2 range:NSMakeRange(([string1 length]),([string2 length]+[string3 length]))];
[info setAttributes:attribute3 range:NSMakeRange(([string1 length] [string2 length]+[string3 length]), [string4 length])];
description.attributedText = info;
description.backgroundColor = [UIColor clearColor];
description.editable = NO;
description.textColor = [UIColor blackColor];
[viewInfo addSubview:description];
[description sizeToFit];
正如问题的注释中所讨论的,问题中提供的代码实际上工作正常,并且按照预期呈现属性字符串。(问题出在我代码的其他地方)你把标签做得足够高,可以放第二行了吗?我无法在6.1上复制这个。我注意到您的代码有点不正确(setFont:行中缺少一个),您在一个地方使用了“label”而不是“contentLabel”)。你确定这是实际代码吗?@Kevin Ballard:是的,标签足够高。@KevinBallard:原来你是对的,我计算并分配给标签框架的所需高度不正确。谢谢你把我放在正确的轨道上。你可能应该写下你最后的评论作为一个答案,并接受它,这样其他用户(像我一样)会立即看到它。
// creiamo textView descrizione
UITextView *description = [[UITextView alloc] init];
description.frame = CGRectMake(20, 453, 500, 190);
NSDictionary *attribute1 = @{NSForegroundColorAttributeName: [UIColor blackColor],
NSBackgroundColorAttributeName: [UIColor clearColor],
NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue-Bold" size:22.0],
};
NSDictionary *attribute2 = @{NSForegroundColorAttributeName: [UIColor blackColor],
NSBackgroundColorAttributeName: [UIColor clearColor],
NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue" size:10],
};
NSDictionary *attribute3 = @{NSForegroundColorAttributeName: [UIColor blackColor],
NSBackgroundColorAttributeName: [UIColor clearColor],
NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue" size:14.0]
};
NSMutableAttributedString *info = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@ %@ %@ \n\n%@ \n ", string1, string2 , string3, string4]];
[info setAttributes:attribute1 range:NSMakeRange(0, ([string1 length]))];
[info setAttributes:attribute2 range:NSMakeRange(([string1 length]),([string2 length]+[string3 length]))];
[info setAttributes:attribute3 range:NSMakeRange(([string1 length] [string2 length]+[string3 length]), [string4 length])];
description.attributedText = info;
description.backgroundColor = [UIColor clearColor];
description.editable = NO;
description.textColor = [UIColor blackColor];
[viewInfo addSubview:description];
[description sizeToFit];