Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
M
Material
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Dmitriy Stepanets
Material
Commits
0235e38f
Commit
0235e38f
authored
Apr 07, 2016
by
Daniel Dahan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added MaterialFontLoader to aid in loading packaged fonts with Material
parent
bdeff5e3
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
39 additions
and
76 deletions
+39
-76
Sources/iOS/MaterialFont.swift
+24
-39
Sources/iOS/RobotoFont.swift
+15
-37
No files found.
Sources/iOS/MaterialFont.swift
View file @
0235e38f
...
...
@@ -33,72 +33,58 @@ import UIKit
public
protocol
MaterialFontType
{}
public
struct
MaterialFont
:
MaterialFontType
{
/**
:name: pointSize
*/
/// Size of font.
public
static
let
pointSize
:
CGFloat
=
16
/**
:name: systemFontWithSize
*/
/// Retrieves the system font with a specified size.
public
static
func
systemFontWithSize
(
size
:
CGFloat
)
->
UIFont
{
return
UIFont
.
systemFontOfSize
(
size
)
}
/**
:name: boldSystemFontWithSize
*/
/// Retrieves the bold system font with a specified size.
public
static
func
boldSystemFontWithSize
(
size
:
CGFloat
)
->
UIFont
{
return
UIFont
.
boldSystemFontOfSize
(
size
)
}
/**
:name: italicSystemFontWithSize
*/
/// Retrieves the italic system font with a specified size.
public
static
func
italicSystemFontWithSize
(
size
:
CGFloat
)
->
UIFont
{
return
UIFont
.
italicSystemFontOfSize
(
size
)
}
public
static
func
loadFontIfNeeded
(
fontName
:
String
)
{
FontLoader
.
loadFontIfNeeded
(
fontName
)
/// Loads a font if it is needed.
public
static
func
loadFontIfNeeded
(
fontName
:
String
)
{
MaterialFontLoader
.
loadFontIfNeeded
(
fontName
)
}
}
/// Loads fonts packaged with Material.
private
class
MaterialFontLoader
{
/// A Dictionary of the fonts already loaded.
static
var
loadedFonts
:
Dictionary
<
String
,
String
>
=
Dictionary
<
String
,
String
>
()
private
class
FontLoader
{
/// Loads a font specified if needed.
static
func
loadFontIfNeeded
(
fontName
:
String
)
{
let
loadedFont
:
String
?
=
MaterialFontLoader
.
loadedFonts
[
fontName
]
static
var
loadedFonts
:[
String
:
String
]
=
[:]
if
nil
==
loadedFont
&&
nil
==
UIFont
(
name
:
fontName
,
size
:
1
)
{
MaterialFontLoader
.
loadedFonts
[
fontName
]
=
fontName
static
func
loadFontIfNeeded
(
fontName
:
String
)
{
let
loadedFont
=
FontLoader
.
loadedFonts
[
fontName
]
if
(
loadedFont
==
nil
&&
UIFont
(
name
:
fontName
,
size
:
1
)
==
nil
)
{
FontLoader
.
loadedFonts
[
fontName
]
=
fontName
let
bundle
:
NSBundle
=
NSBundle
(
forClass
:
MaterialFontLoader
.
self
)
let
identifier
:
String
?
=
bundle
.
bundleIdentifier
let
fontURL
:
NSURL
?
=
true
==
identifier
?
.
hasPrefix
(
"org.cocoapods"
)
?
bundle
.
URLForResource
(
fontName
,
withExtension
:
"ttf"
,
subdirectory
:
"io.cosmicmind.material.fonts.bundle"
)
:
bundle
.
URLForResource
(
fontName
,
withExtension
:
"ttf"
)
let
bundle
=
NSBundle
(
forClass
:
FontLoader
.
self
)
var
fontURL
:
NSURL
?
=
nil
let
identifier
=
bundle
.
bundleIdentifier
if
identifier
?
.
hasPrefix
(
"org.cocoapods"
)
==
true
{
fontURL
=
bundle
.
URLForResource
(
fontName
,
withExtension
:
"ttf"
,
subdirectory
:
"io.cosmicmind.material.fonts.bundle"
)
}
else
{
fontURL
=
bundle
.
URLForResource
(
fontName
,
withExtension
:
"ttf"
)
}
if
fontURL
!=
nil
{
let
data
=
NSData
(
contentsOfURL
:
fontURL
!
)
!
let
provider
=
CGDataProviderCreateWithCFData
(
data
)
let
font
=
CGFontCreateWithDataProvider
(
provider
)
!
if
let
v
:
NSURL
=
fontURL
{
let
data
:
NSData
=
NSData
(
contentsOfURL
:
v
)
!
let
provider
:
CGDataProvider
=
CGDataProviderCreateWithCFData
(
data
)
!
let
font
:
CGFont
=
CGFontCreateWithDataProvider
(
provider
)
!
var
error
:
Unmanaged
<
CFError
>
?
if
!
CTFontManagerRegisterGraphicsFont
(
font
,
&
error
)
{
let
errorDescription
:
CFStringRef
=
CFErrorCopyDescription
(
error
!.
takeUnretainedValue
())
let
ns
Error
=
error
!.
takeUnretainedValue
()
as
AnyObject
as!
NSError
let
nsError
:
NS
Error
=
error
!.
takeUnretainedValue
()
as
AnyObject
as!
NSError
NSException
(
name
:
NSInternalInconsistencyException
,
reason
:
errorDescription
as
String
,
userInfo
:
[
NSUnderlyingErrorKey
:
nsError
])
.
raise
()
}
}
}
}
}
\ No newline at end of file
Sources/iOS/RobotoFont.swift
View file @
0235e38f
...
...
@@ -31,23 +31,17 @@
import
UIKit
public
struct
RobotoFont
:
MaterialFontType
{
/**
:name: pointSize
*/
/// Size of font.
public
static
var
pointSize
:
CGFloat
{
return
MaterialFont
.
pointSize
}
/**
:name: thin
*/
/// Thin font.
public
static
var
thin
:
UIFont
{
return
thinWithSize
(
MaterialFont
.
pointSize
)
}
/**
:name: thinWithSize
*/
/// Thin with size font.
public
static
func
thinWithSize
(
size
:
CGFloat
)
->
UIFont
{
MaterialFont
.
loadFontIfNeeded
(
"Roboto-Thin"
)
if
let
f
=
UIFont
(
name
:
"Roboto-Thin"
,
size
:
size
)
{
...
...
@@ -56,16 +50,12 @@ public struct RobotoFont : MaterialFontType {
return
MaterialFont
.
systemFontWithSize
(
size
)
}
/**
:name: light
*/
/// Light font.
public
static
var
light
:
UIFont
{
return
lightWithSize
(
MaterialFont
.
pointSize
)
}
/**
:name: lightWithSize
*/
/// Light with size font.
public
static
func
lightWithSize
(
size
:
CGFloat
)
->
UIFont
{
MaterialFont
.
loadFontIfNeeded
(
"Roboto-Light"
)
if
let
f
=
UIFont
(
name
:
"Roboto-Light"
,
size
:
size
)
{
...
...
@@ -74,16 +64,12 @@ public struct RobotoFont : MaterialFontType {
return
MaterialFont
.
systemFontWithSize
(
size
)
}
/**
:name: regular
*/
/// Regular font.
public
static
var
regular
:
UIFont
{
return
regularWithSize
(
MaterialFont
.
pointSize
)
}
/**
:name: regularWithSize
*/
/// Regular with size font.
public
static
func
regularWithSize
(
size
:
CGFloat
)
->
UIFont
{
MaterialFont
.
loadFontIfNeeded
(
"Roboto-Regular"
)
if
let
f
=
UIFont
(
name
:
"Roboto-Regular"
,
size
:
size
)
{
...
...
@@ -92,9 +78,12 @@ public struct RobotoFont : MaterialFontType {
return
MaterialFont
.
systemFontWithSize
(
size
)
}
/**
:name: mediumWithSize
*/
/// Medium font.
public
static
var
medium
:
UIFont
{
return
mediumWithSize
(
MaterialFont
.
pointSize
)
}
/// Medium with size font.
public
static
func
mediumWithSize
(
size
:
CGFloat
)
->
UIFont
{
MaterialFont
.
loadFontIfNeeded
(
"Roboto-Medium"
)
if
let
f
=
UIFont
(
name
:
"Roboto-Medium"
,
size
:
size
)
{
...
...
@@ -103,23 +92,12 @@ public struct RobotoFont : MaterialFontType {
return
MaterialFont
.
boldSystemFontWithSize
(
size
)
}
/**
:name: medium
*/
public
static
var
medium
:
UIFont
{
return
mediumWithSize
(
MaterialFont
.
pointSize
)
}
/**
:name: bold
*/
/// Bold font.
public
static
var
bold
:
UIFont
{
return
boldWithSize
(
MaterialFont
.
pointSize
)
}
/**
:name: boldWithSize
*/
/// Bold with size font.
public
static
func
boldWithSize
(
size
:
CGFloat
)
->
UIFont
{
MaterialFont
.
loadFontIfNeeded
(
"Roboto-Bold"
)
if
let
f
=
UIFont
(
name
:
"Roboto-Bold"
,
size
:
size
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment