开发者

Decreasing font size in rails

Im trying to achieve the following effect in rails:

If the text is bigger than x characters then make it smaller, the next x characters smaller, the next character smaller, ad infinitum

for example x = 7 would output the following html

Lorem i<small>psum do<small>lor sit<small> amet, <small>consecte
<small>tur adip<small>isicing</small></small></small></small></small></small>

and the css would be small {font-size: 95%}

开发者_StackOverflow中文版

What is an elegant way to achieve this?


hm. maybe some helper with some recursion?

def shrink(what)
  if ( what.length > 5)
    "#{what[0,4]}<small>#{shrink(what[5,what.length()-1])}</small>"
  else
    what
  end
end

there is a better way to write the recursive call for certain, but i don't know it right know.


moritz's answer seems fine, dry-code attempt at iterative version:

def shrink(what,chunk=5)
  result = ''
  0.step(what.length, chunk) do |i|
    if i<what.length
      result << '<small>' if i>0
      result << what[i,chunk]
    end
  end
  result << '</small>'*(what.length/chunk)
  result
end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜