I wrote a small component supporting auto-ellipsing if the text
content doesn't fit into a label. This is done by using a fixed width
and a maximum height. If the text would render the label bigger than
"InMaxHeight" pixels, it will be truncated until it fits into the
label including the ellipsis itself. There are already solutions out
there but they don't handle ellipsis in the way it is expected and
useful and they also are not capable of handling large texts... This
code should work in all common browsers. Please let me know what you
think about it. There is no special license attached to it:
public class LabelEllipsis extends Label{
private int m_MaxHeight;
private String m_Content;
/**
*
* @param InContent
* The initial content.
* @param InStyleName
* The initial style sheet for the text.
* @param InWidth
* The width in pixels.
* @param InMaxHeight
* A positive value for pixel height.
*/
public LabelEllipsis(
String InContent,
String InStyleName,
int InWidth,
int InMaxHeight){
m_MaxHeight = InMaxHeight;
m_Content = InContent;
assert(InMaxHeight > 0);
setStyleName(InStyleName);
setWidth(InWidth + "px");
}
public LabelEllipsis(
String InContent,
String InStyleName,
int InWidth,
int InMaxHeight,
HorizontalAlignmentConstant InAlign){
this(InContent, InStyleName, InWidth, InMaxHeight);
this.setHorizontalAlignment(InAlign);
}
@Override
protected void onLoad(){
setText(m_Content);
}
@Override
public void setText(String InContent){
if(!isAttached())
return;
String OldContent = m_Content;
super.setText(m_Content);
int Height = getOffsetHeight();
if(Height > m_MaxHeight){
float Ratio = (float)m_MaxHeight / (float)Height; // <
1.0
m_Content = m_Content.substring(0,
Math.min(m_Content.length() - 4,
(int)(m_Content.length() *
Ratio))) + "...";
super.setText(m_Content);
Height = getOffsetHeight();
if(Height < m_MaxHeight){
// add chars until max height is reached
while(Height < m_MaxHeight){
m_Content = OldContent.substring(0,
m_Content.length() - 2) +
"...";
super.setText(m_Content);
Height = getOffsetHeight();
}
// remove last char
m_Content = m_Content.substring(0,
Math.min(m_Content.length() -
4, m_Content.length() *
m_MaxHeight)) + "...";
super.setText(m_Content);
}else if(Height > m_MaxHeight){
// remove chars until max height is reached
while((Height > m_MaxHeight) &&
(m_Content.length() >= 4)){
m_Content = m_Content.substring(0,
m_Content.length() - 4) +
"...";
super.setText(m_Content);
Height = getOffsetHeight();
}
}
}
}
}
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Google Web Toolkit" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/Google-Web-Toolkit?hl=en
-~----------~----~----~----~------~----~------~--~---