拉长标题


<%@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;
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值