在Label中显示表情

6 篇文章 0 订阅
3 篇文章 0 订阅
NSMutableString * contentStr = [[ NSMutableString alloc ] initWithString : self . model . content ];
   
NSString * placeString = [[ NSMutableString alloc ] initWithString : self . model . content ];
   
NSArray * arr = ExpressionArray ;
   
BOOL haveLook = NO ;
   
NSMutableAttributedString * attStr = [[ NSMutableAttributedString alloc ] initWithString :contentStr attributes : nil ];
   
for ( int i = 0 ; i < arr. count ; i ++) {
        placeString = [placeString
stringByReplacingOccurrencesOfString :[ NSString stringWithFormat : @"[/%@]" ,arr[i]] withString : @" i" ];
       
NSString * lookStr = [ NSString stringWithFormat : @"[/%@]" ,arr[i]];
       
if ([contentStr rangeOfString :lookStr]. location != NSNotFound ) {
           
MMTextAttachment * textAttachment = [[ MMTextAttachment alloc ] initWithData : nil ofType : nil ] ;
           
UIImage * smileImage = [ UIImage imageNamed :arr[i]];
            textAttachment.
image = smileImage ;
           
NSAttributedString * textAttachmentString = [ NSAttributedString attributedStringWithAttachment :textAttachment] ;
            [attStr
replaceCharactersInRange :[contentStr rangeOfString :lookStr] withAttributedString :textAttachmentString];
           
            [contentStr
insertString : @" " atIndex :[contentStr rangeOfString :lookStr]. location ];
           
NSRange range = NSMakeRange ([contentStr rangeOfString :lookStr]. location , [contentStr rangeOfString :lookStr]. length );
            [contentStr
deleteCharactersInRange :range];
            haveLook =
YES ;
            i = -
1 ;
           
continue ;
        }
    }
   
NSAttributedString * st = [[ NSAttributedString alloc ] initWithString : @" " attributes : nil ];
    [attStr appendAttributedString:st];

#define ExpressionArray @[@"微笑",@"撇嘴",@"",@"发呆",@"得意",@"流泪",@"害羞",@"闭嘴",@"睡觉",@"大哭",@"尴尬",@"发怒",@"调皮",@"呲牙",@"惊讶",@"难过",@"",@"冷汗",@"抓狂",@"",@"偷笑",@"可爱",@"白眼",@"傲慢",@"饥饿",@"",@"惊恐",@"流汗",@"憨笑",@"大兵",@"奋斗",@"咒骂",@"疑问",@"..",@"",@"折磨",@"",@"骷髅",@"敲打",@"拜拜",@"擦汗",@"抠鼻",@"鼓掌",@"糗大了",@"坏笑",@"左哼哼",@"右哼哼",@"哈欠",@"鄙视",@"委屈",@"快哭了",@"坏笑",@"亲亲",@"",@"可怜",@"菜刀",@"西瓜",@"篮球",@"乒乓",@"咖啡",@"",@"猪头",@"玫瑰",@"凋谢",@"嘴唇",@"爱心",@"心碎",@"蛋糕",@"闪电",@"炸弹",@"炸弹",@"",@"足球",@"瓢虫",@"便便",@"月亮",@"太阳",@"礼物",@"拥抱",@"",@"",@"握手",@"",@"抱拳",@"勾引",@"拳头",@"差劲",@"爱你",@"NO",@"OK",@"爱情",@"飞吻",@"跳跳",@"发抖",@"怄火",@"转圈",@"磕头",@"回头",@"跳绳",@"挥手",@"激动",@"街舞",@"献吻",@"左太极",@"右太极"]
要在JComboBox下拉列表添加表情,可以使用Java Swing的JLabel显示表情,并将JLabel添加到JComboBox作为下拉列表项。 首先,需要准备好要添加表情图片。可以通过在本地或网络上找到相应的表情图片并下载到本地。 然后,创建一个JLabel对象来显示表情图片,并将其添加到JComboBox。具体代码如下所示: ```java // 创建一个JComboBox对象 JComboBox<String> comboBox = new JComboBox<>(); // 创建一个JLabel对象来显示表情图片 ImageIcon icon = new ImageIcon("path/to/emoji.png"); JLabel label = new JLabel(icon); // 将JLabel添加到JComboBox作为下拉列表项 comboBox.addItem("表情"); comboBox.setRenderer(new DefaultListCellRenderer() { @Override public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) { super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); if ("表情".equals(value)) { setIcon(icon); setText(""); } return this; } }); ``` 在上述代码,我们首先创建了一个JComboBox对象。然后,创建一个JLabel对象来显示表情图片,并将其添加到JComboBox作为下拉列表项。为了显示表情图片,我们需要将JLabel的图标设置为表情图片,并将文本设置为空。 最后,我们为JComboBox设置了一个渲染器,用于将表情图片显示在下拉列表。在渲染器,我们将"表情"字符串对应的列表项的文本设置为空,并将其图标设置为表情图片。 需要注意的是,这里只添加了一个表情图片作为示例,如果需要添加多个表情图片,可以将上述代码放在循环执行,将每个表情图片都添加到JComboBox
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值