Saturday, July 21, 2018

#1 2008-08-04 04:17:12 pm

gecko
Member
Registered: 2006-04-27
Posts: 48

add horizontal scroll bar to text view

Hi,

Is there a way to add a horizontal scroll bar to a text view using applescript? If not, is there a way to do this using call method?

thanks,

gecko


“There is nothing like looking, if you want to find something. You certainly usually find something, if you look, but it is not always quite the something you were after."
-- J. R. R. Tolkien


Filed under: text, view, horizontal, scroll

Offline

 

#2 2008-08-04 09:02:46 pm

regulus6633
Member
From:: Taulov, Denmark
Registered: 2006-11-02
Posts: 1695
Website

Re: add horizontal scroll bar to text view

Text views by default come inside of a scroll view, which means they already have both horizontal and vertical scroll bars... so there's nothing to add.

You can change the properties of the scroll view in interface builder. Just select the text view in your window and initially on the first click you will have the scroll view selected (it takes a second click to actually select the text view). You can tell what you have selected by looking at the top of the inspector window. It should say 'Scroll View Attributes' when you have the scroll view selected. In that inspector there's choices so you can show or hide the scroll bars.

Offline

 

#3 2008-08-04 10:43:35 pm

gecko
Member
Registered: 2006-04-27
Posts: 48

Re: add horizontal scroll bar to text view

OK, sorry, I forgot to mention that I was using Xcode 2.3. I checked Xcode 3 and I found everything exactly how you said. Is there a work around for 2.3?

Thanks,

gecko


“There is nothing like looking, if you want to find something. You certainly usually find something, if you look, but it is not always quite the something you were after."
-- J. R. R. Tolkien

Offline

 

#4 2008-08-04 11:07:34 pm

regulus6633
Member
From:: Taulov, Denmark
Registered: 2006-11-02
Posts: 1695
Website

Re: add horizontal scroll bar to text view

I haven't used version 2.x of xcode for awhile but it should be very similar. I know text fields are still inside of scroll views by default so you should be able to see the scroll view attributes and make any needed adjustments.

Offline

 

#5 2008-08-05 07:18:12 am

gecko
Member
Registered: 2006-04-27
Posts: 48

Re: add horizontal scroll bar to text view

I understand that a text view is in a scroll view. And Interface Builder should have an option for a horizontal scroller. They do have it in Xcode 3. But THERE IS NO OPTION in the panel on Xcode 2.5 or earlier. Screenshot: http://picasaweb.google.com/natescahill … 8518854258  There must be a way to set a horizontal scroller from applescript. I tried everything that I could think of. When I was wading through the obj-C docs in Xcode, I found this:

- (void)setHasHorizontalScroller:(BOOL)flag

I used it in the on launched handeler:

Applescript:

call method "setHasHorizontalScroller:" of (scroll view "scroll" of window "main") with parameters {true}

It worked to some extent, since it showed a horizontal scroll bar, but it didn't let me scroll when a line of text reached the right side of the text view. Screenshot: http://picasaweb.google.com/natescahill … 4509227106   Is there any other option for setting a horizontal scroller? It can't be very hard.

Thanks,

gecko


“There is nothing like looking, if you want to find something. You certainly usually find something, if you look, but it is not always quite the something you were after."
-- J. R. R. Tolkien

Offline

 

#6 2008-08-05 07:28:27 am

regulus6633
Member
From:: Taulov, Denmark
Registered: 2006-11-02
Posts: 1695
Website

Re: add horizontal scroll bar to text view

Maybe this will help. Your command looks a little off... try this one:

Applescript:

call method "setHasHorizontalScroller:" of (scroll view "scroll" of window "main") with parameter YES

Scroll views should have the horizontal scroller by default. They only show when your text is wider than the field. They don't show normally. Before doing the above command you may want to check if your scroll view already has one with:

Applescript:

set doesItHaveOne to call method "hasHorizontalScroller" of (scroll view "scroll" of window "main")
log doesItHaveOne

I think also by default a text view will wrap the lines of text. If it does this then you will never see the horizontal scroller because the text wraps and thus you will only ever see a vertical scroller. But you can probably change the wrapping style in IB such that it doesn't wrap.

Last edited by regulus6633 (2008-08-05 07:37:55 am)

Offline

 

#7 2008-08-05 12:34:42 pm

regulus6633
Member
From:: Taulov, Denmark
Registered: 2006-11-02
Posts: 1695
Website

Re: add horizontal scroll bar to text view

regulus6633 wrote:

I think also by default a text view will wrap the lines of text. If it does this then you will never see the horizontal scroller because the text wraps and thus you will only ever see a vertical scroller. But you can probably change the wrapping style in IB such that it doesn't wrap.


I was thinking about this today and looked. I couldn't find a way to turn off the automatic wrapping of a text view in IB. I think this is your ultimate problem. You'll never see a horizontal scroller with wrapping. So I made an objective-c class to turn wrapping off. It was a little more difficult than I thought but I got it to work. Here's what to do.

In Xcode add a new file to your project from the file menu. Choose "objective-c class" as the file type. I named mine TextViewWrap. You'll get an ".h" and ".m" file in your project. Add the following code to those two files:

==== TextViewWrap.h ====
#import <Cocoa/Cocoa.h>

@interface TextViewWrap : NSObject {

}
+(void)turnWrappingOff:(NSTextView *)textView;
@end

==== TextViewWrap.m ====
#import "TextViewWrap.h"

const float LargeNumberForText = 1.0e7;

@implementation TextViewWrap
+(void)turnWrappingOff:(NSTextView *)textView {
    // configure the scroll view
    NSScrollView *scrollView = [textView enclosingScrollView];
    [scrollView setHasVerticalScroller:YES];
    [scrollView setHasHorizontalScroller:YES];
   
    // configure the text container
    NSTextContainer *textContainer = [textView textContainer];
    [textContainer setWidthTracksTextView: NO];
    [textContainer setHeightTracksTextView:NO];
    NSSize size = [textContainer containerSize];
    size.width = LargeNumberForText;
    size.height = LargeNumberForText;
    [textContainer setContainerSize: size];
   
    // configure the text view
    NSRect frame = [textView frame];
    [textView setMinSize:frame.size];
    [textView setMaxSize:NSMakeSize(LargeNumberForText, LargeNumberForText)];
    [textView setHorizontallyResizable:YES];
    [textView setVerticallyResizable:YES];
}
@end


Then in IB connect your text view to this handler and add this applescript code...

Applescript:

on awake from nib theObject
   if name of theObject is "textView" then
       call method "turnWrappingOff:" of class "TextViewWrap" with parameter theObject
   end if
end awake from nib

That's it. See if it works.

Last edited by regulus6633 (2008-08-05 12:41:51 pm)

Offline

 

#8 2008-08-05 01:48:43 pm

gecko
Member
Registered: 2006-04-27
Posts: 48

Re: add horizontal scroll bar to text view

Excellent!!! big_smile It works! I'm really glad, I had a lot of the functionality of my project based on the horizontal scrolling. Thank you!
Before I saw your post, I found this in the apple mailing lists: http://lists.apple.com/archives/Cocoa-d … 01352.html I don't know if this is a plausible solution to my problem, it seems simpler, but I don't know obj-c...

Thanks again,

gecko


“There is nothing like looking, if you want to find something. You certainly usually find something, if you look, but it is not always quite the something you were after."
-- J. R. R. Tolkien

Offline

 

#9 2008-08-05 02:46:08 pm

regulus6633
Member
From:: Taulov, Denmark
Registered: 2006-11-02
Posts: 1695
Website

Re: add horizontal scroll bar to text view

That's basically the same thing I did but it's missing many details... which is why it looks easier. But once you add in the missing details it would pretty much be the same. It's also missing one important detail that affects the way the scrolling works. I set a min and max size of the text view and what that does is automatically adjusts to scroll bar based on the length of the actual text that is in the field. That code just makes a big text container and the effect of that would mean your text view would have a scroller whether or not one is needed and there would be lots of white space after your text. It wouldn't dynamically adjust itself to the width of your text.

Test my code. It scrolls exactly to the longest line and no more. Then if you make the text shorter or longer it auto-adjusts to fit the text. That code won't do that.

Anyway I'm glad it works. smile

Offline

 

#10 2010-10-18 04:28:10 am

philostein
Member
Registered: 2010-10-18
Posts: 2

Re: add horizontal scroll bar to text view

Thanks Hank, this works a treat! I don't understand why this feature isn't built in…

As a ASOC newbie, I was pleased to be able to convert this AppleScript code:
[code]on awake from nib theObject
   if name of theObject is "textView" then
       call method "turnWrappingOff:" of class "TextViewWrap" with parameter theObject
   end if
end awake from nib[/code]
to this:
[code]    on awakeFromNib()
tell class "TextViewWrap" of current application
            its turnWrappingOff_(textView)
        end tell
end awakeFromNib[/code]
I don't know if this is the best way, but it might help some other newbies out there…

P.S.

Hank, I recently had some correspondence with you regarding your excellent ChangeFileDates command line tool - cheers for all your help with that. You seem to have fingers in lots of coding pies. big_smile

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)