5

使用 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];
}
4

1 回答 1

0

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

于 2014-05-09T23:44:02.647 回答