拉长题目
拉长标题
<%@tag description="fixed width text" pageEncoding="UTF-8" trimDirectiveWhitespaces="true" %> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@attribute name="text" required="true" rtexprvalue="true" type="java.lang.String"%> <%@attribute name="widthInEm" required="true" rtexprvalue="true" type="java.lang.Float"%> <% String text = (String)jspContext.getAttribute("text"); Float widthInEm = (Float)jspContext.getAttribute("widthInEm"); String result = HtmlTagUtils.toFixedWidth(text, widthInEm); jspContext.setAttribute("result", result); %> ${result}
<%@ taglib tagdir="/WEB-INF/tags" prefix="t"%> <t:fixedWidth text="${user.realName}" widthInEm="3" />
public class HtmlTagUtils { public static String toFixedWidth(String text, Float widthInEm) { String result = text; if (widthInEm != null && text.length() < widthInEm && text.length() > 1) { Float space = (widthInEm - text.length()) / (text.length() - 1); Character[] characters = toCharacters(text); List list = (List) Arrays.asList(characters); result = join(list, "<span style='display: inline-block; zoom: 1; width:" + space + "em'></span>"); } return result; } public static String join(Iterable pColl, String separator) { Iterator oIter; if (pColl == null || (!(oIter = pColl.iterator()).hasNext())) { return ""; } StringBuilder oBuilder = new StringBuilder(String.valueOf(oIter.next())); while (oIter.hasNext()) { oBuilder.append(separator).append(oIter.next()); } return oBuilder.toString(); } public static Character[] toCharacters(String str) { char[] chars = str.toCharArray(); Character[] result = new Character[chars.length]; for (int i = 0; i < chars.length; i++) { result[i] = chars[i]; } return result; } }