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
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
48 additions
and
85 deletions
+48
-85
Sources/iOS/MaterialFont.swift
+28
-43
Sources/iOS/RobotoFont.swift
+20
-42
No files found.
Sources/iOS/MaterialFont.swift
View file @
0235e38f
...
@@ -33,72 +33,58 @@ import UIKit
...
@@ -33,72 +33,58 @@ import UIKit
public
protocol
MaterialFontType
{}
public
protocol
MaterialFontType
{}
public
struct
MaterialFont
:
MaterialFontType
{
public
struct
MaterialFont
:
MaterialFontType
{
/**
/// Size of font.
:name: pointSize
*/
public
static
let
pointSize
:
CGFloat
=
16
public
static
let
pointSize
:
CGFloat
=
16
/**
/// Retrieves the system font with a specified size.
:name: systemFontWithSize
*/
public
static
func
systemFontWithSize
(
size
:
CGFloat
)
->
UIFont
{
public
static
func
systemFontWithSize
(
size
:
CGFloat
)
->
UIFont
{
return
UIFont
.
systemFontOfSize
(
size
)
return
UIFont
.
systemFontOfSize
(
size
)
}
}
/**
/// Retrieves the bold system font with a specified size.
:name: boldSystemFontWithSize
*/
public
static
func
boldSystemFontWithSize
(
size
:
CGFloat
)
->
UIFont
{
public
static
func
boldSystemFontWithSize
(
size
:
CGFloat
)
->
UIFont
{
return
UIFont
.
boldSystemFontOfSize
(
size
)
return
UIFont
.
boldSystemFontOfSize
(
size
)
}
}
/**
/// Retrieves the italic system font with a specified size.
:name: italicSystemFontWithSize
*/
public
static
func
italicSystemFontWithSize
(
size
:
CGFloat
)
->
UIFont
{
public
static
func
italicSystemFontWithSize
(
size
:
CGFloat
)
->
UIFont
{
return
UIFont
.
italicSystemFontOfSize
(
size
)
return
UIFont
.
italicSystemFontOfSize
(
size
)
}
}
public
static
func
loadFontIfNeeded
(
fontName
:
String
)
{
/// Loads a font if it is needed.
FontLoader
.
loadFontIfNeeded
(
fontName
)
public
static
func
loadFontIfNeeded
(
fontName
:
String
)
{
MaterialFontLoader
.
loadFontIfNeeded
(
fontName
)
}
}
}
}
/// Loads fonts packaged with Material.
private
class
FontLoader
{
private
class
MaterialFontLoader
{
/// A Dictionary of the fonts already loaded.
static
var
loadedFonts
:[
String
:
String
]
=
[:]
static
var
loadedFonts
:
Dictionary
<
String
,
String
>
=
Dictionary
<
String
,
String
>
()
static
func
loadFontIfNeeded
(
fontName
:
String
)
{
/// Loads a font specified if needed.
let
loadedFont
=
FontLoader
.
loadedFonts
[
fontName
]
static
func
loadFontIfNeeded
(
fontName
:
String
)
{
if
(
loadedFont
==
nil
&&
UIFont
(
name
:
fontName
,
size
:
1
)
==
nil
)
{
let
loadedFont
:
String
?
=
MaterialFontLoader
.
loadedFonts
[
fontName
]
FontLoader
.
loadedFonts
[
fontName
]
=
fontName
if
nil
==
loadedFont
&&
nil
==
UIFont
(
name
:
fontName
,
size
:
1
)
{
let
bundle
=
NSBundle
(
forClass
:
FontLoader
.
self
)
MaterialFontLoader
.
loadedFonts
[
fontName
]
=
fontName
var
fontURL
:
NSURL
?
=
nil
let
identifier
=
bundle
.
bundleIdentifier
let
bundle
:
NSBundle
=
NSBundle
(
forClass
:
MaterialFontLoader
.
self
)
let
identifier
:
String
?
=
bundle
.
bundleIdentifier
if
identifier
?
.
hasPrefix
(
"org.cocoapods"
)
==
true
{
let
fontURL
:
NSURL
?
=
true
==
identifier
?
.
hasPrefix
(
"org.cocoapods"
)
?
bundle
.
URLForResource
(
fontName
,
withExtension
:
"ttf"
,
subdirectory
:
"io.cosmicmind.material.fonts.bundle"
)
:
bundle
.
URLForResource
(
fontName
,
withExtension
:
"ttf"
)
fontURL
=
bundle
.
URLForResource
(
fontName
,
withExtension
:
"ttf"
,
subdirectory
:
"io.cosmicmind.material.fonts.bundle"
)
if
let
v
:
NSURL
=
fontURL
{
}
else
{
let
data
:
NSData
=
NSData
(
contentsOfURL
:
v
)
!
fontURL
=
bundle
.
URLForResource
(
fontName
,
withExtension
:
"ttf"
)
let
provider
:
CGDataProvider
=
CGDataProviderCreateWithCFData
(
data
)
!
}
let
font
:
CGFont
=
CGFontCreateWithDataProvider
(
provider
)
!
if
fontURL
!=
nil
{
let
data
=
NSData
(
contentsOfURL
:
fontURL
!
)
!
let
provider
=
CGDataProviderCreateWithCFData
(
data
)
let
font
=
CGFontCreateWithDataProvider
(
provider
)
!
var
error
:
Unmanaged
<
CFError
>
?
var
error
:
Unmanaged
<
CFError
>
?
if
!
CTFontManagerRegisterGraphicsFont
(
font
,
&
error
)
{
if
!
CTFontManagerRegisterGraphicsFont
(
font
,
&
error
)
{
let
errorDescription
:
CFStringRef
=
CFErrorCopyDescription
(
error
!.
takeUnretainedValue
())
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
()
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,24 +31,18 @@
...
@@ -31,24 +31,18 @@
import
UIKit
import
UIKit
public
struct
RobotoFont
:
MaterialFontType
{
public
struct
RobotoFont
:
MaterialFontType
{
/**
/// Size of font.
:name: pointSize
*/
public
static
var
pointSize
:
CGFloat
{
public
static
var
pointSize
:
CGFloat
{
return
MaterialFont
.
pointSize
return
MaterialFont
.
pointSize
}
}
/**
/// Thin font.
:name: thin
*/
public
static
var
thin
:
UIFont
{
public
static
var
thin
:
UIFont
{
return
thinWithSize
(
MaterialFont
.
pointSize
)
return
thinWithSize
(
MaterialFont
.
pointSize
)
}
}
/**
/// Thin with size font.
:name: thinWithSize
public
static
func
thinWithSize
(
size
:
CGFloat
)
->
UIFont
{
*/
public
static
func
thinWithSize
(
size
:
CGFloat
)
->
UIFont
{
MaterialFont
.
loadFontIfNeeded
(
"Roboto-Thin"
)
MaterialFont
.
loadFontIfNeeded
(
"Roboto-Thin"
)
if
let
f
=
UIFont
(
name
:
"Roboto-Thin"
,
size
:
size
)
{
if
let
f
=
UIFont
(
name
:
"Roboto-Thin"
,
size
:
size
)
{
return
f
return
f
...
@@ -56,17 +50,13 @@ public struct RobotoFont : MaterialFontType {
...
@@ -56,17 +50,13 @@ public struct RobotoFont : MaterialFontType {
return
MaterialFont
.
systemFontWithSize
(
size
)
return
MaterialFont
.
systemFontWithSize
(
size
)
}
}
/**
/// Light font.
:name: light
*/
public
static
var
light
:
UIFont
{
public
static
var
light
:
UIFont
{
return
lightWithSize
(
MaterialFont
.
pointSize
)
return
lightWithSize
(
MaterialFont
.
pointSize
)
}
}
/**
/// Light with size font.
:name: lightWithSize
public
static
func
lightWithSize
(
size
:
CGFloat
)
->
UIFont
{
*/
public
static
func
lightWithSize
(
size
:
CGFloat
)
->
UIFont
{
MaterialFont
.
loadFontIfNeeded
(
"Roboto-Light"
)
MaterialFont
.
loadFontIfNeeded
(
"Roboto-Light"
)
if
let
f
=
UIFont
(
name
:
"Roboto-Light"
,
size
:
size
)
{
if
let
f
=
UIFont
(
name
:
"Roboto-Light"
,
size
:
size
)
{
return
f
return
f
...
@@ -74,17 +64,13 @@ public struct RobotoFont : MaterialFontType {
...
@@ -74,17 +64,13 @@ public struct RobotoFont : MaterialFontType {
return
MaterialFont
.
systemFontWithSize
(
size
)
return
MaterialFont
.
systemFontWithSize
(
size
)
}
}
/**
/// Regular font.
:name: regular
*/
public
static
var
regular
:
UIFont
{
public
static
var
regular
:
UIFont
{
return
regularWithSize
(
MaterialFont
.
pointSize
)
return
regularWithSize
(
MaterialFont
.
pointSize
)
}
}
/**
/// Regular with size font.
:name: regularWithSize
public
static
func
regularWithSize
(
size
:
CGFloat
)
->
UIFont
{
*/
public
static
func
regularWithSize
(
size
:
CGFloat
)
->
UIFont
{
MaterialFont
.
loadFontIfNeeded
(
"Roboto-Regular"
)
MaterialFont
.
loadFontIfNeeded
(
"Roboto-Regular"
)
if
let
f
=
UIFont
(
name
:
"Roboto-Regular"
,
size
:
size
)
{
if
let
f
=
UIFont
(
name
:
"Roboto-Regular"
,
size
:
size
)
{
return
f
return
f
...
@@ -92,34 +78,26 @@ public struct RobotoFont : MaterialFontType {
...
@@ -92,34 +78,26 @@ public struct RobotoFont : MaterialFontType {
return
MaterialFont
.
systemFontWithSize
(
size
)
return
MaterialFont
.
systemFontWithSize
(
size
)
}
}
/**
/// Medium font.
:name: mediumWithSize
public
static
var
medium
:
UIFont
{
*/
return
mediumWithSize
(
MaterialFont
.
pointSize
)
public
static
func
mediumWithSize
(
size
:
CGFloat
)
->
UIFont
{
}
MaterialFont
.
loadFontIfNeeded
(
"Roboto-Medium"
)
/// Medium with size font.
public
static
func
mediumWithSize
(
size
:
CGFloat
)
->
UIFont
{
MaterialFont
.
loadFontIfNeeded
(
"Roboto-Medium"
)
if
let
f
=
UIFont
(
name
:
"Roboto-Medium"
,
size
:
size
)
{
if
let
f
=
UIFont
(
name
:
"Roboto-Medium"
,
size
:
size
)
{
return
f
return
f
}
}
return
MaterialFont
.
boldSystemFontWithSize
(
size
)
return
MaterialFont
.
boldSystemFontWithSize
(
size
)
}
}
/**
/// Bold font.
:name: medium
*/
public
static
var
medium
:
UIFont
{
return
mediumWithSize
(
MaterialFont
.
pointSize
)
}
/**
:name: bold
*/
public
static
var
bold
:
UIFont
{
public
static
var
bold
:
UIFont
{
return
boldWithSize
(
MaterialFont
.
pointSize
)
return
boldWithSize
(
MaterialFont
.
pointSize
)
}
}
/**
/// Bold with size font.
:name: boldWithSize
*/
public
static
func
boldWithSize
(
size
:
CGFloat
)
->
UIFont
{
public
static
func
boldWithSize
(
size
:
CGFloat
)
->
UIFont
{
MaterialFont
.
loadFontIfNeeded
(
"Roboto-Bold"
)
MaterialFont
.
loadFontIfNeeded
(
"Roboto-Bold"
)
if
let
f
=
UIFont
(
name
:
"Roboto-Bold"
,
size
:
size
)
{
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