Skip to content

Commit

Permalink
Merge pull request #6 from pawelniewie/master
Browse files Browse the repository at this point in the history
Made it work with queries returning multiple elements, also it's possible to run box fit for the same element again (so you can update the text)
  • Loading branch information
michikono committed Aug 23, 2014
2 parents 5cc715a + b988ff6 commit 3f25885
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 171 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,6 @@ db/backup*
log/*.log
log/*.txt
tmp/*

lib
node_modules
8 changes: 8 additions & 0 deletions Gruntfile.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = (grunt) ->
grunt.initConfig
coffee:
compile:
files:
'lib/jquery.boxfit.js': ['src/*.coffee']
grunt.loadNpmTasks 'grunt-contrib-coffee'
grunt.registerTask 'default', ['coffee']
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ $('#my-big-box').boxfit({multiline: true});
---------------------------
```

What it's actually doing under the hood is converting
What it's actually doing under the hood is converting

```html
<div id="my-big-box" style="width: 500px; height: 500px">
Expand Down Expand Up @@ -84,3 +84,8 @@ This source code will be maintained in coffeescript and compiled over to JavaScr
License
=======
Copyright Michi Kono. Boxfit uses the MIT license. More info here: http://www.opensource.org/licenses/mit-license.php

Releases
----------------------------
* `v1.2.0` Added support for re-running Boxfit, added support for running Boxfit for multiple elements at once.
* `v1.01`
86 changes: 0 additions & 86 deletions jquery.boxfit.coffeee

This file was deleted.

84 changes: 0 additions & 84 deletions jquery.boxfit.js

This file was deleted.

38 changes: 38 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"name": "jquery.boxfit",
"title": "jQuery Boxfit",
"description": "Boxfit is a jQuery plugin for resizing text. It will scale text to fit inside a fixed width div.",
"version": "1.2.0",
"homepage": "http://michikono.github.io/boxfit",
"author": {
"name": "Michi Kono",
"url": "http://www.michikono.com/"
},
"contributors": [
"Pawel Niewiadomski <[email protected]> (http://twitter.com/devonsteroids)"
],
"main": "lib/jquery.boxfit.js",
"scripts": {
"test": "grunt"
},
"repository": {
"type": "git",
"url": "git://github.com/michikono/boxfit.git"
},
"bugs": {
"url": "https://github.com/michikono/boxfit/issues"
},
"licenses": [
{
"type": "MIT",
"url": "http://www.opensource.org/licenses/mit-license.php"
}
],
"devDependencies": {
"coffee-script": ">= 1.6",
"grunt-cli": "*",
"grunt-contrib-coffee": "*"
},
"dependencies": {},
"keywords": []
}
83 changes: 83 additions & 0 deletions src/jquery.boxfit.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
###
BoxFit v1.1 - jQuery Plugin
(c) 2012 Michi Kono (michikono.com); https://github.com/michikono/boxfit
License: http://www.opensource.org/licenses/mit-license.php
To use: $('#target-div').boxFit()
Will make the *text* content inside the div (or whatever tag) scale to fit that tag
###

(($) ->
$.fn.boxfit = (options) ->
return this.each () ->
settings =
# manually set a width if you haven't set one explicitly via CSS
width: null
# manually set a height if you haven't set one explicitly via CSS
height: null
# the amount to change each time - bigger numbers are faster but fit less perfectly
step_size: 1
# the number of font size iterations we should step through until we give up
step_limit: 200
# set to false to NOT align middle (vertically)
align_middle: true
# set to false this to NOT center the text (horizontally)
align_center: true
# set to false to allow the big text to wrap (useful for when you want text to fill a big vertical area)
multiline: false
# minimum font size (changing this may cause some "shrink" scenarios to overflow instead)
minimum_font_size: 5
# set to a number to limit the maximum font size allowed
maximum_font_size: null

$.extend( settings, options );

# take measurements
if settings.width
original_width = settings.width
$(this).width original_width + "px"
else
original_width = $(this).width()
if settings.height
original_height = settings.height
$(this).height original_height + "px"
else
original_height = $(this).height()

settings.maxFont = true if settings.maxFontSize isnt `undefined`

if !original_width || !original_height
console.info("Set static height/width on target DIV before using boxfit! Detected width: '#{original_width}'; height: '#{original_height}'") if window.console?
else
unless settings.multiline
$(this).css('white-space', 'nowrap')

original_text = $(this).html()

if $("<div>" + original_text + "</div>").find("span.boxfitted").length is 0
span = $($("<span></span>").addClass("boxfitted").html(original_text))
$(this).html span
else
span = $($(this).find('span.boxfitted')[0])

current_step = 0
inner_span = span

$(this).css "display", "table"
inner_span.css "display", "table-cell"
if settings.align_middle
inner_span.css "vertical-align", "middle"
if settings.align_center
$(this).css "text-align", "center"
inner_span.css "text-align", "center"

# keep growing the target so long as we haven't exceeded the width or height
inner_span.css("font-size", settings.minimum_font_size)
while $(this).width() <= original_width && $(this).height() <= original_height
break if current_step++ > settings.step_limit
next_font_size = parseInt(inner_span.css("font-size"), 10)
break if settings.maximum_font_size && next_font_size > settings.maximum_font_size
inner_span.css("font-size", next_font_size + settings.step_size)
# go back one step
inner_span.css("font-size", parseInt(inner_span.css("font-size"), 10) - settings.step_size)
return $(this)
)( jQuery )

0 comments on commit 3f25885

Please sign in to comment.