private void _applyFontStyleForSelection(Font font) { StyledDocument doc = mTextEditor.getStyledDocument(); MutableAttributeSet attrs = mTextEditor.getInputAttributes(); StyleConstants.setFontFamily(attrs, font.getFamily()); StyleConstants.setFontSize(attrs, font.getSize()); StyleConstants.setBold(attrs, ((font.getStyle() & Font.BOLD) != 0)); StyleConstants.setItalic(attrs, ((font.getStyle() & Font.ITALIC) != 0)); StyleConstants.setUnderline(attrs, ((font.getStyle() & Font.CENTER_BASELINE) != 0)); int start = mTextEditor.getSelectionStart(); int end = mTextEditor.getSelectionEnd(); doc.setCharacterAttributes(start, (end - start), attrs, false); }
/** * Sets the font of the specified attribute. * * @param attr attribute to apply this font to * @param f the font to use */ public static void setAttributeFont(MutableAttributeSet attr, Font f) { StyleConstants.setBold(attr, f.isBold()); StyleConstants.setItalic(attr, f.isItalic()); StyleConstants.setFontFamily(attr, f.getFamily()); StyleConstants.setFontSize(attr, f.getSize()); }