NSTextView Chinese/Unicode Characters - cocoa

I have a piece of text drawn in my cocoa app but I also need to draw it in a NSTextField and a NSTextView and look exactly the same, they all usually draw fine but if there are unicode or Chinese characters in the text then the NSTextView has large line spaces between the lines, it works perfectly in the nstextfield and when drawing normally.
I have tried every setting that I can find but none affect this issue, has anyone run into this issue before and know of a fix?
You can see the issue in a screenshot at http://cl.ly/3e0U1T2U2G2z341j3O3T
Here is my test code to show the issue
NSTextField *textField = [[NSTextField alloc] initWithFrame:NSMakeRect(5, 50, 100, 100)];
[textField setStringValue:#"え?移転!? TESTTestTer 友達の旦那の店 THIS IS A TEST THIS IS A TEST"];
[textField setBordered:NO];
[textField setBezeled:NO];
[textField setAllowsEditingTextAttributes:YES];
[textField setDelegate:self];
[textField setFont:[NSFont fontWithName:#"LucidaGrande" size:12.0]];
[textField setTextColor:[NSColor colorWithDeviceRed:(74.0/255.0) green:(74.0/255.0) blue:(74.0/255.0) alpha:1.0]];
[[window contentView] addSubview: textField];
NSTextView *textView = [[NSTextView alloc] initWithFrame:NSMakeRect(200, 50, 100, 100)];
[textView setString:#"え?移転!? TEST TestTer 友達の旦那の店 THIS IS A TEST THIS IS A TEST"];
[textView setDelegate:self];
[textView setFont:[NSFont fontWithName:#"LucidaGrande" size:12.0]];
[textView setTextColor:[NSColor colorWithDeviceRed:(74.0/255.0) green:(74.0/255.0) blue:(74.0/255.0) alpha:1.0]];
[[textView layoutManager] setTypesetterBehavior:NSTypesetterBehavior_10_2_WithCompatibility];
[[textView textContainer] setLineFragmentPadding:0.0];
[[window contentView] addSubview: textView];

Related

Auto Layout on OS X - make NSTextField fill superview

I can't get Auto Layout to work on OS X. What I'm trying to do is pretty simple, namely I have an NSTextField that is supposed to fill the entire space of its superview. Here's the minimal working example code I'm using:
#import AppKit;
int main() {
#autoreleasepool {
NSWindow *window = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, 300, 300)
styleMask:(NSTitledWindowMask | NSClosableWindowMask | NSResizableWindowMask)
backing:NSBackingStoreBuffered
defer:NO];
NSTextField *textField = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 100, 100)];
textField.stringValue = #"Lorem ipsum dolor sit atmet.";
[window.contentView addSubview:textField];
textField.translatesAutoresizingMaskIntoConstraints = NO;
window.contentView.translatesAutoresizingMaskIntoConstraints = NO;
[textField setContentHuggingPriority:1
forOrientation:NSLayoutConstraintOrientationHorizontal];
[textField setContentHuggingPriority:1
forOrientation:NSLayoutConstraintOrientationVertical];
[window.contentView addConstraints:
[NSLayoutConstraint constraintsWithVisualFormat:#"H:|-[textField]-|"
options:0
metrics:nil
views:#{#"textField": textField}]];
[window.contentView addConstraints:
[NSLayoutConstraint constraintsWithVisualFormat:#"V:|-[textField]-|"
options:0
metrics:nil
views:#{#"textField": textField}]];
window.contentView.wantsLayer = YES;
window.contentView.layer.borderWidth = 5;
[window makeKeyAndOrderFront:nil];
[[NSApplication sharedApplication] run];
[textField release];
[window release];
}
return EXIT_SUCCESS;
}
When I run this, the text field doesn't appear. When you resize the window, you can clearly see the border of the content view resizing properly. What am I doing wrong?
I believe you shouldn't alter NSWindow's contentView behavior, e.g. setting its translatesAutoresizingMaskIntoConstraints property.
Removing the following line works for me:
window.contentView.translatesAutoresizingMaskIntoConstraints = NO;

last row of uicollectionview is not visible while scrolling

I created UiCollectionView programmatically. This is my code snippet...
UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];
collectionV=[[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 320, 500) collectionViewLayout:layout];
[collectionV setDataSource:self];
[collectionV setDelegate:self];
[collectionV registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:#"cellIdentifier"];
[collectionV setBackgroundColor:[UIColor greenColor]];
collectionV.scrollEnabled = YES;
[self.view addSubView:CollectionV];
My problem is I could not see the last row while scrolling. Anybody help me to fix it.
Thanks in advance.

InputAccessoryView of a UITextField

I need to stick a toolBar with a UITextField inside to a keyboard.
What if I make the whole toolBar (which I think is the textField's super view) the inputAccessoryView of its textField?
I mean like this:
textField.inputAccessoryView = toolBar; // textField is inside the toolBar
I've been trying on this but I have not made it work yet.
Anyone can help?
Or is there another way to achieve what I want?
- (void)viewDidLoad
{
[self createAccessoryView];
[textField setDelegate:self];
[textField setKeyboardType:UIKeyboardTypeDefault];
[textField setInputAccessoryView:fieldAccessoryView];
}
- (void)createAccessoryView
{
CGRect frame = CGRectMake(0.0, self.view.bounds.size.height, self.view.bounds.size.width, 44.0);
fieldAccessoryView = [[UIToolbar alloc] initWithFrame:frame];
fieldAccessoryView.barStyle = UIBarStyleBlackOpaque;
fieldAccessoryView.tag = 200;
[fieldAccessoryView setBarStyle:UIBarStyleBlack];
UIBarButtonItem *spaceButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(done:)];
UISegmentedControl* segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:NSLocalizedString(#"Previous", #""), NSLocalizedString(#"Next", #""), nil]];
[segmentedControl addTarget:self action:#selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
[segmentedControl setMomentary:YES];
UIBarButtonItem *segmentButton = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
[fieldAccessoryView setItems:[NSArray arrayWithObjects:segmentButton, spaceButton, doneButton, nil] animated:NO];
[segmentButton release];
[spaceButton release];
[doneButton release];
[segmentedControl release];
}
When are you setting the property? You may be setting it after the view has already loaded.
Try setting it in
- (void) viewDidLoad

How does NSScrollView works?

How does NSScrollView works in mac applications? I've write the following code but the scrolling is not working.
NSDictionary *temp=[[ NSDictionary alloc] init ];
NSScrollView *scroll = [[NSScrollView alloc ] initWithFrame:CGRectMake(0, 0, 100, 100)];
NSArray *dicArray=[NSArray arrayWithObjects:dict,dict1,dict2,dict3,dict4, nil];
for (int i=0; i<[dicArray count]; i++)
{
int offset=100;
int x=10;
int y=20;
y=y+(i*offset);
temp= [dicArray objectAtIndex:i];
NSRect titleRect=NSMakeRect(x,y,100,30);
NSRect subtitleRect=NSMakeRect(x, y+20, 400, 20);
NSTextField *title=[[NSTextField alloc] initWithFrame:titleRect];
[[title cell] setStringValue:[temp objectForKey:key]];
[[title cell] setWraps:NO];
[[title cell] setScrollable:YES];
[[title cell] setEditable:NO];
[[title cell] setFont:[NSFont systemFontOfSize:[NSFont systemFontSizeForControlSize:NSMinusSetExpressionType]]];
[title setDrawsBackground:NO];
[title setBordered:NO];
NSTextField *subtitle=[[NSTextField alloc] initWithFrame:subtitleRect ];
[[subtitle cell] setStringValue:[temp objectForKey:subkey]];
[[subtitle cell] setWraps:NO];
[[subtitle cell] setScrollable:YES];
[[subtitle cell] setEditable:NO];
[[subtitle cell] setFont:[NSFont systemFontOfSize:[NSFont systemFontSizeForControlSize:NSMiniControlSize]]];
[subtitle setDrawsBackground:NO];
[subtitle setBordered:NO];
[scroll addSubview:title];
[scroll addSubview:categoryOkButton];
[scroll addSubview:subtitle];
}
[[self window] setContentView:scroll];
[scroll release];
My Cocoa is a bit rusty, but NSScrollView works in a different way than UIScrollView. You don't just add subviews, that doesn't work with NSScrollView.
What you need is a container view into which you add all your subviews. So you create an NSView of the appropriate size and add all your views to that container view. Then you set:
[myScrollView setDocumentView:myContainerView];
See the Scroll View Programming Guide for more details.

Custom NSView with NSTextField subview drawing issue

So I am drawing a custom window (transparent) with a custom NSView as the contentview, and wouldlike to add an NSTextField to the window as well, however, when I add the NSTextField, I get a weird resizing or redraw of the custom NSView, and I can't figure out what is causing the problem. Both the window and the contentview of the window are subclasses of NSWindow, and NSView, respectively. Also, I have tried to just layer the custom NSView (not set it as the contentview) with no change. Any ideas?
NSWindow *quickEntryWindow = [[TransparentWindow alloc] initWithContentRect:NSMakeRect([[NSScreen mainScreen] frame].size.width/2 - 250, [[NSScreen mainScreen] frame].size.height/2 + 50, 500, 100)
styleMask:NSBorderlessWindowMask
backing:NSBackingStoreBuffered
defer:NO
special:YES];
BorderView *quickEntryBorderView = [[BorderView alloc] initWithFrame:NSMakeRect(0, 0, [[quickEntryWindow contentView] frame].size.width, [[quickEntryWindow contentView] frame].size.height)];
[quickEntryBorderView canDrawConcurrently];
[quickEntryWindow setContentView:quickEntryBorderView];
NSTextField *quickEntryTextField = [[NSTextField alloc] initWithFrame:NSMakeRect(10, 10, [quickEntryBorderView frame].size.width-20, [quickEntryBorderView frame].size.height-20)];
[quickEntryTextField setAutoresizingMask:NSViewNotSizable];
[quickEntryTextField setBordered:NO];
[quickEntryTextField setDrawsBackground:NO];
[quickEntryTextField setFocusRingType:NSFocusRingTypeNone];
[quickEntryTextField setFont:[NSFont fontWithName:#"Helvetica" size:42]];
[quickEntryTextField setTextColor:[NSColor grayColor]];
[quickEntryBorderView addSubview:quickEntryTextField];
What I get is something that looks like this (it is normal when no text is entered into the NSTextField):
Oh yeah, I know I'm not managing my memory...I'm just trying to get this working. Thanks!

Resources