【问题标题】:NSAttributeString Wrapping issueNSAttributedString 包装问题
【发布时间】:2013-07-17 04:24:17
【问题描述】:

我正在尝试将属性文本设置为标签。属性似乎是字体和颜色的工作。

我面临的唯一问题是换行。 UILabel 的大小为 (200,300),其中 numberofLines=0。因此,它应该换行,但事实并非如此。

   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 PQRSSSS 1235 234 ...

【问题讨论】:

  • 如果在字符串中插入 \n 是否会换行?仅供测试

标签: ios objective-c xcode4.6 nsattributedstring


【解决方案1】:

你需要的是 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.

【讨论】:

    【解决方案2】:

    确保将 UILabel 的换行模式属性设置为您想要的,如下所示:

    UILabel.lineBreakMode = NSLineBreakByWordWrapping;
    

    或者,如果您使用的是 Interface Builder,则可以在那里进行。

    【讨论】:

    • 你还需要做label.numberOfLines = 0
    【解决方案3】:

    为什么不能将行数设置为 1。因为换行有意义但行数 0 没有意义.. 而且您必须为标签设置适当的框架。

    【讨论】:

    【解决方案4】:

    我认为你正面临 uilabel 增加高度的问题,如果你需要 label 中的多行,那么你必须给出 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);
    

    【讨论】:

      【解决方案5】:

      连同设置:

          self.myTextLabel.lineBreakMode = NSLineBreakByWordWrapping;
          self.myTextLabel.numberOfLines = 0;
      

      您可能还需要将标签的布局约束设置为它的超级视图。当我忽略将标签的尾随约束固定到标签的超级视图时,我遇到了这个问题。

      【讨论】:

        猜你喜欢
        • 2011-11-14
        • 1970-01-01
        • 1970-01-01
        • 2011-08-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-09
        • 2016-03-13
        相关资源
        最近更新 更多