-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgui.cpccTextStylesMac.h
More file actions
95 lines (72 loc) · 3.52 KB
/
gui.cpccTextStylesMac.h
File metadata and controls
95 lines (72 loc) · 3.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/* *****************************************
* File: gui.cpccTextStylesMac.h
* Version: see function getClassVersion()
* Purpose: Portable (cross-platform), light-weight, c++ library
* *****************************************
* Library: Cross Platform C++ Classes (cpcc)
* Copyright: 2013 StarMessage software.
* License: Free for opensource projects.
* Commercial license for closed source projects.
* Web: http://www.StarMessageSoftware.com
* Download: https://code.google.com/p/cpcc/
* https://github.com/starmessage/cpcc
* email: sales -at- starmessage.info
* *****************************************
*/
#pragma once
#include "cpccUnicodeSupport.h"
#include "cpccColor.h"
class cpccTextStylesMac
{
public:
void setFont(NSMutableDictionary *aTextAttr, const cpcc_char *aFontName, const int aFontSize)
{
// assert( aTextAttr && "#9525: cpccTextStylesMac.setFont() NULL aTextAttr");
if (!aTextAttr)
return;
if (aTextAttr && aFontName)
{
/*
// convert font
NSFont* font = [NSFont fontWithName:@"Arial" size:30];
font = [[NSFontManager sharedFontManager] convertFont:font toHaveTrait:NSFontItalicTrait];
// create with traits and weight
NSFont* font = [[NSFontManager sharedFontManager] fontWithFamily:@"Arial" traits:NSFontItalicTrait weight:2 size:30];
*/
NSString *fontName = [[[NSString alloc] initWithUTF8String:aFontName] autorelease];
// works ok
// [aTextAttr setObject:[NSFont fontWithName:fontName size:aFontSize ] forKey:NSFontAttributeName];
// https://developer.apple.com/library/mac/documentation/cocoa/reference/applicationkit/classes/NSFontManager_Class/Reference/Reference.html#//apple_ref/doc/uid/20000383-16096
NSFont *font = [[NSFontManager sharedFontManager] fontWithFamily:fontName traits:0 weight:1 size:aFontSize];
if (font)
[aTextAttr setObject: font forKey:NSFontAttributeName] ;
// [aTextAttr setObject: [[NSFontManager sharedFontManager] fontWithFamily:fontName traits:0 weight:1 size:aFontSize] forKey:NSFontAttributeName] ;
}
}
void setColor(NSMutableDictionary *aTextAttr, const cpccColor *aColor)
{
if (aTextAttr && aColor)
[aTextAttr setObject:aColor->asNSColor() forKey:NSForegroundColorAttributeName];
}
void setKerning(NSMutableDictionary *aTextAttr, const float aKerning)
{
if (!aTextAttr)
return;
// NSKernAttributeName toValue:nil
[aTextAttr setObject:[NSNumber numberWithDouble:aKerning] forKey:NSKernAttributeName];
}
void setParagraph(NSMutableDictionary *aTextAttr, const int align)
{
if (aTextAttr)
{
NSMutableParagraphStyle *paragraphStyle = [[[NSParagraphStyle defaultParagraphStyle] mutableCopy] autorelease];
if (align==0)
[paragraphStyle setAlignment:NSCenterTextAlignment];
if (align==1)
[paragraphStyle setAlignment:NSRightTextAlignment];
if (align==-1)
[paragraphStyle setAlignment:NSLeftTextAlignment];
[aTextAttr setObject:paragraphStyle forKey:NSParagraphStyleAttributeName];
}
}
};