我有一个多行标签,其中包含以下文本:
这里有很多文本 · $$$$
由于开头的文本是自由格式的,有时换行会看起来像这样:
Lots of text here · $$$
$
如何避免这种情况发生?我希望它看起来像这样:
Lots of text here ·
$$$$
我尝试了各种lineBreakMode,但效果不佳。自动换行无法解决问题,因为它不会将 $$$$ 视为一个单词。
UIFont *fontUsed = [UIFont systemFontOfSize:17];
NSDictionary *dictFont = [NSDictionary dictionaryWithObject:fontUsed forKey:NSFontAttributeName];
NSString *strTextToShow = @"text that has to be displayed but without $$$$";
CGRect rectForSimpleText = [strTextToShow boundingRectWithSize:CGSizeMake(154, 258) options:NSStringDrawingUsesLineFragmentOrigin attributes:dictFont context:nil];
NSString *strTextAdded = [NSString stringWithFormat:@"%@ $$$$", strTextToShow];
CGFloat oldHeight = rectForSimpleText.size.height;
CGRect rectForAppendedText = [strTextAdded boundingRectWithSize:CGSizeMake(154, 258) options:NSStringDrawingUsesLineFragmentOrigin attributes:dictFont context:nil];
CGFloat newHeight = rectForAppendedText.size.height;
if (oldHeight < newHeight) {
strTextAdded = [strTextAdded stringByReplacingOccurrencesOfString:@"$$$$" withString:@"\n$$$$"];
}
[lblLongText setText:strTextAdded];
lblLongText 是 UILabel
的 IBOutlet
,而 CGSizeMake(154, 258)
则是我使用的 UILabel
的大小。如果您有其他发现,请告诉我。
尝试在输入文本中插入换行符。
这里有很多文本·\n $$$
它应该会在下一行打印出$$$。