为什么NSLineBreakByWordWrapping分裂单词?

时间:2013-08-13 12:21:41

标签: ios line-breaks nsattributedstring

使用NSAttributedString,我尝试在三角形内创建文本,如下所示;

http://postimg.org/image/rp9fukgaj/

我使用“NSLineBreakByWordWrapping”方法在定义的形状内拟合文本。但是第一个文字分裂,我无法找到解决方案。我希望我的文本跳转到以下行而不是分裂。我怎么能这样做?

在我使用的代码下面;

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor blackColor];
    CTFontRef fontRef = CTFontCreateWithName((CFStringRef)@"HelveticaNeue-Regular", 15.0f, NULL);     
    NSMutableParagraphStyle* paragraph = [NSMutableParagraphStyle new];
    paragraph.headIndent = 10;
    paragraph.firstLineHeadIndent = 10;
    paragraph.tailIndent = -10;
    paragraph.lineBreakMode = NSLineBreakByWordWrapping;
    paragraph.alignment = NSTextAlignmentCenter;
    paragraph.paragraphSpacing = 15;    
    NSDictionary *attrDictionary = [NSDictionary dictionaryWithObjectsAndKeys: (__bridge id)fontRef, (NSString *)kCTFontAttributeName, (id)[[UIColor whiteColor] CGColor], (NSString *)(kCTForegroundColorAttributeName),paragraph,NSParagraphStyleAttributeName, nil];    
    CFRelease(fontRef);
    NSAttributedString *attString = [[NSAttributedString alloc] initWithString:@"Initially, now I create the paragraph style and apply it to the first character. Note how the margins are dictated: the tailIndent is negative, to bring the right margin leftward, and the firstLineHeadIndent must be set separately, as the headIndent does not automatically apply to the first line." attributes:attrDictionary];
    [(CustomView *)[self view] setAttString:attString];
}

1 个答案:

答案 0 :(得分:0)

检查documentation的NSLineBreakMode,显然如果单词不适合给定的行,该单词将被分解为适合。如果您的文本是静态的,您可能只需插入换行符就可以跳过三角形的第一部分;否则,你必须聪明地弄清楚如何克服这个限制。