我正在开发一个应用程序,其中我将部分文本对齐到左边,而其他部分对齐在同一行上
以下是我的代码:
//text is "abc\n"
NSString *dateAndText = [text stringByAppendingString:[NSString stringWithFormat:@" %@",strDate]];
dateAndText = [dateAndText stringByReplacingOccurrencesOfString:@"\n" withString:@" "];
CGSize size = [dateAndText sizeWithFont:font constrainedToSize:CGSizeMake(220, 9999) lineBreakMode:NSLineBreakByWordWrapping];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height)];
label.numberOfLines = 0;
label.lineBreakMode = NSLineBreakByWordWrapping;
label.textAlignment = NSTextAlignmentLeft;
NSRange range = [dateAndText rangeOfString:[dateAndText substringFromIndex:text.length]];
NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:dateAndText];
[string addAttribute:NSFontAttributeName value:[UIFont italicSystemFontOfSize:10.0 ] range:NSMakeRange(range.location,range.length)];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init] ;
[paragraphStyle setAlignment:NSTextAlignmentRight];
[string addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(range.location,range.length)];
label.attributedText = string;
归因字符串未在同一行上对齐。如果我在代码中的任何地方出错,请有人帮助我
输出应如下图所示
答案 0 :(得分:0)
使用paragraphStyle.lineHeightMultiple = 0.001;
解决了我的问题。