Re: Controlling line-breaking in a text view
Re: Controlling line-breaking in a text view
- Subject: Re: Controlling line-breaking in a text view
- From: "Ken Ferry" <email@hidden>
- Date: Sat, 9 Aug 2008 21:00:35 -0700
Hi Andy,
I have never needed to work with this stuff, but with that caveat:
Have you tried subclassing NSTextStorage and implementing
-lineBreakBeforeIndex:withinRange:?
-Ken
On Sat, Aug 9, 2008 at 8:43 PM, Andy Kim <email@hidden> wrote:
> Hi,
>
> I'm having trouble figuring out how to make the text system not break words
> that begin with a '/', such as paths, when wrapping. Let's say that we're
> laying out the following line of text:
>
> =====================================
> Normally, you should never delete /Applications
> =====================================
>
> I want '/Applications' to stay intact when wrapping. By default, the text
> system wraps it the following way:
>
> =====================================
> Normally, you should never delete /
> Applications
> =====================================
>
> I'd like it to look like this:
>
> =====================================
> Normally, you should never delete
> /Applications
> =====================================
>
> After much searching, the best solution I've come up with so far is a
> subclass of NSATSTypesetter with the following method implementation in it:
>
> - (BOOL)shouldBreakLineByWordBeforeCharacterAtIndex:(NSUInteger)charIndex
> {
> NSString *string = [[[self layoutManager] textStorage] string];
>
> if (charIndex >= 1) {
> NSTextStorage *ts = [[self layoutManager] textStorage];
> if ([ts attribute:PFPathAttributeName atIndex:charIndex
> effectiveRange:NULL]) {
> // Only break if the previous character is not part
> of the path
> return [ts attribute:PFPathAttributeName
> atIndex:charIndex-1 effectiveRange:NULL] == nil;
> }
> }
>
> return YES;
> }
>
> I am setting the attribute PFPathAttributeName to the text storage to mark
> the path. This works somewhat, but now the problem is that after wrapping,
> the text looks like this:
>
> =====================================
> Normally, you should never
> delete /Applications
> =====================================
>
> This happens because -shouldBreakLineByWordBeforeCharacterAtIndex: never
> gets called for the '/' and the next time it gets called is for the 'd' in
> 'delete'.
>
> This modified wrapping behavior makes it seem like 'delete /Applications' is
> one word. I think it's better than the default behavior but still not ideal.
>
> So how can I make it wrap exactly the way I want?
>
> - Andy Kim
>
>
> _______________________________________________
>
> Cocoa-dev mailing list (email@hidden)
>
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
>
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden