Ios NSAttributeString包装问题
我正在尝试将属性文本设置为标签。属性似乎是字体和颜色的一种工作方式 我面临的唯一问题是线条的缠绕。UILabel的大小为(200300),numberofLines=0。因此,它应该用这句话来概括,但事实并非如此Ios NSAttributeString包装问题,ios,objective-c,xcode4.6,nsattributedstring,Ios,Objective C,Xcode4.6,Nsattributedstring,我正在尝试将属性文本设置为标签。属性似乎是字体和颜色的一种工作方式 我面临的唯一问题是线条的缠绕。UILabel的大小为(200300),numberofLines=0。因此,它应该用这句话来概括,但事实并非如此 NSMutableString *title=[[NSMutableString alloc] init]; NSRange range1; NSRange range2; NSRange range3; NSString *str1
NSMutableString *title=[[NSMutableString alloc] init];
NSRange range1;
NSRange range2;
NSRange range3;
NSString *str1=@"ABCD EFGHI klm";
[title appendString:str1];
range1=NSMakeRange(0, str1.length);
NSString *str2=@"PQRSSSS ";
[title appendString:str2];
range2=NSMakeRange(range1.length, str2.length);
NSString *str3=@"1235 2347 989034 023490234 90";
[title appendString:str3];
range3=NSMakeRange(range2.location+range2.length, str3.length);
NSMutableAttributedString *attributeText=[[NSMutableAttributedString alloc] initWithString:title];
[attributeText setAttributes:[NSDictionary dictionaryWithObjectsAndKeys:color1,NSForegroundColorAttributeName,[self getStlylishItalicFont:13.0] ,NSFontAttributeName,nil] range:range1];
[attributeText setAttributes:[NSDictionary dictionaryWithObjectsAndKeys:color2,NSForegroundColorAttributeName,[self getStylishFont:13.0] ,NSFontAttributeName,nil] range:range2];
[attributeText setAttributes:[NSDictionary dictionaryWithObjectsAndKeys:color3,NSForegroundColorAttributeName,[self getStylishBoldFont:13.0] ,NSFontAttributeName,nil] range:range3];
self.myTextLabel.attributedText=attributeText;
UILabel如下所示,即使高度为300
ABCD EFGHI klm PQRSSSS1235 234…为什么不能将行数设置为1。因为换行有意义,但行数0没有意义。。而且你还必须为标签设置合适的框架。我认为你正面临uilabel增加高度的问题,如果你需要标签中的多行,那么你必须给出havin numberoflines=0的属性;之后,你必须根据你给标签的文本大小调整标签框的大小 请检查下面的代码,我可能对你有用
NSString *someText = [NSString stringWithFormat:@"%@",[[arrFulldetails objectAtIndex:indexPath.row]valueForKey:@"MessageText"]];
CGSize constraintSize;
constraintSize.width = 300.0f;
constraintSize.height =100000;
CGSize stringSize =[someText sizeWithFont: [UIFont boldSystemFontOfSize: 17] constrainedToSize: constraintSize lineBreakMode: UILineBreakModeWordWrap];
CGRect rect ;
rect = CGRectMake(10, 150, 210, 20+stringSize.height);
确保将UILabel的换行模式属性设置为所需的属性,如下所示:
UILabel.lineBreakMode = NSLineBreakByWordWrapping;
或者,如果您使用的是Interface Builder,则可以在那里执行此操作。您需要的是NSParagraphStyle属性:
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
paragraphStyle.alignment = NSTextAlignmentLeft;
paragraphStyle.lineSpacing = 1;
//Now add this to your attributes dictionary for the key NSParagraphStyleAttributeName eg,
@{NSParagraphStyleAttributeName:paragraphStyle,…}
在一个不相关的注释中,您知道最好使用现代Objective-c格式创建词典。每当我不这样做,我的导师就会生气。看起来是这样的:
[attributeText setAttributes:@{NSForegroundColorAttributeName:color1, NSFontAttributeName:[self getStlylishItalicFont:13.0], NSParagraphStyleAttributeName:paragraphStyle, }];
//The trailing comma in the dictionary definition is not at typo it is important.
随着设置:
self.myTextLabel.lineBreakMode = NSLineBreakByWordWrapping;
self.myTextLabel.numberOfLines = 0;
您可能还需要将标签的布局约束设置为其superview。当我忘记将标签的尾随约束固定到标签的superview时,我遇到了这种情况。将numberOfLines设置为零实际上是将其设置为“无限行数”的正确方法。。。如果在字符串中插入\n,是否将其换行?仅用于测试,您还需要执行
label.numberOfLines=0