从标签中删除缩进

时间:2013-11-29 06:51:27

标签: ios iphone xcode uilabel

我有一个标签,第一个单词以缩进开头。我不希望段落以缩进开头。

我需要这样的东西:

LoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsum
LoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsum
LoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsum
LoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsum.

我在哪里:

   LoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsum
LoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsumm
LoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsum.

我使用以下代码:

UILabel *b1Lab1=[[UILabel alloc] initWithFrame:CGRectMake(8,108,124,90)];

NSString *strr =[aPostIntro objectAtIndex:indexPath.section*2];
if(strr.length > 270)
{
    strr =  [[strr substringToIndex:270] stringByAppendingString:@"..."];
}

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
paragraphStyle.paragraphSpacingBefore = 0;
paragraphStyle.alignment = NSTextAlignmentJustified;

NSAttributedString *string1 = [[NSAttributedString alloc] initWithString:strr 
                                                              attributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                              paragraphStyle, NSParagraphStyleAttributeName ,
                                                              [NSNumber numberWithFloat:0],NSBaselineOffsetAttributeName, 
                                                                                         nil]];
b1Lab1.attributedText = string1;

// NSString *stri = [aPostIntro objectAtIndex:indexPath.section];

b1Lab1.lineBreakMode = NSLineBreakByWordWrapping;
b1Lab1.numberOfLines = 9;
b1Lab1.font = [UIFont fontWithName:@"Helvetica" size:8];
b1Lab1.tag=111;
b1Lab1.textColor = [UIColor blackColor];

3 个答案:

答案 0 :(得分:1)

检查:

NSString *strr =[aPostIntro objectAtIndex:indexPath.section*2];
NSLog(@"%@",strr); //check strr

并确认strr本身不包含任何起始空格。


如果您将strr替换为:

  

strr = @“Lorem ipsum dolor sit amet,consectetuer adipiscing elit,sed   diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam   erat volutpat ......“

输出符合您的预期。

答案 1 :(得分:0)

使用此代码可能对您有帮助.....

UILabel * b1Lab1 = [[UILabel alloc] initWithFrame:CGRectMake(8,108,124,90)];

            NSString *strr =[aPostIntro objectAtIndex:indexPath.section*2];
            if(strr.length > 270)
            {
                strr =  [[strr substringToIndex:270] stringByAppendingString:@"..."];
            }

            NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
            paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
            paragraphStyle.paragraphSpacingBefore = 0;
            paragraphStyle.alignment = NSTextAlignmentJustified;

            NSAttributedString *string1 = [[NSAttributedString alloc] initWithString:strr
                                                                         attributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                                                     paragraphStyle, NSParagraphStyleAttributeName ,
                                                                                     [NSNumber numberWithFloat:0],NSBaselineOffsetAttributeName, 
                                                                                     nil]];
            string1.Trim();
            b1Lab1.attributedText = string1;

            // NSString *stri = [aPostIntro objectAtIndex:indexPath.section];

            b1Lab1.lineBreakMode = NSLineBreakByWordWrapping;
            b1Lab1.numberOfLines = 9;
            b1Lab1.font = [UIFont fontWithName:@"Helvetica" size:8];
            b1Lab1.tag=111;
            b1Lab1.textColor = [UIColor blackColor];

答案 2 :(得分:0)

评论此行:

b1Lab1.lineBreakMode = NSLineBreakByWordWrapping;